keyforkd: rename Serde middleware Bincode

This commit is contained in:
Ryan Heywood 2023-09-07 15:20:15 -05:00
parent 705b0ad826
commit aad990ab71
Signed by: ryan
GPG Key ID: 8E401478A3FBEF72
2 changed files with 15 additions and 15 deletions

View File

@ -16,7 +16,7 @@ use service::Keyforkd;
pub async fn start_and_run_server(mnemonic: Mnemonic) -> Result<(), Box<dyn std::error::Error>> {
let service = ServiceBuilder::new()
.layer(middleware::SerdeLayer::new())
.layer(middleware::BincodeLayer::new())
.service(Keyforkd::new(mnemonic));
let runtime_vars = std::env::vars()

View File

@ -5,12 +5,12 @@ use serde::{de::DeserializeOwned, Serialize};
use thiserror::Error;
use tower::{Layer, Service};
pub struct SerdeLayer<'a, Request> {
pub struct BincodeLayer<'a, Request> {
phantom: PhantomData<&'a ()>,
phantom_request: PhantomData<&'a Request>,
}
impl<'a, Request> SerdeLayer<'a, Request> {
impl<'a, Request> BincodeLayer<'a, Request> {
pub fn new() -> Self {
Self {
phantom: PhantomData,
@ -19,11 +19,11 @@ impl<'a, Request> SerdeLayer<'a, Request> {
}
}
impl<'a, S: 'a, Request> Layer<S> for SerdeLayer<'a, Request> {
type Service = SerdeService<S, Request>;
impl<'a, S: 'a, Request> Layer<S> for BincodeLayer<'a, Request> {
type Service = BincodeService<S, Request>;
fn layer(&self, service: S) -> Self::Service {
SerdeService {
BincodeService {
service,
phantom_request: PhantomData,
}
@ -31,13 +31,13 @@ impl<'a, S: 'a, Request> Layer<S> for SerdeLayer<'a, Request> {
}
#[derive(Clone)]
pub struct SerdeService<S, Request> {
pub struct BincodeService<S, Request> {
service: S,
phantom_request: PhantomData<Request>,
}
#[derive(Debug, Error)]
pub enum SerdeServiceError {
pub enum BincodeServiceError {
#[error("Error while polling: {0}")]
Poll(String),
@ -48,7 +48,7 @@ pub enum SerdeServiceError {
Convert(String),
}
impl<S, Request> Service<Vec<u8>> for SerdeService<S, Request>
impl<S, Request> Service<Vec<u8>> for BincodeService<S, Request>
where
S: Service<Request>,
Request: DeserializeOwned,
@ -57,7 +57,7 @@ where
<S as Service<Request>>::Future: Send + 'static,
{
type Response = Vec<u8>;
type Error = SerdeServiceError;
type Error = BincodeServiceError;
type Future = Pin<Box<dyn Future<Output = Result<Self::Response, Self::Error>> + Send>>;
fn poll_ready(
@ -66,7 +66,7 @@ where
) -> std::task::Poll<Result<(), Self::Error>> {
self.service
.poll_ready(cx)
.map_err(|e| SerdeServiceError::Poll(e.to_string()))
.map_err(|e| BincodeServiceError::Poll(e.to_string()))
}
fn call(&mut self, req: Vec<u8>) -> Self::Future {
@ -74,7 +74,7 @@ where
Ok(r) => r,
Err(e) => {
return Box::pin(
async move { Err(SerdeServiceError::Convert(e.to_string())) },
async move { Err(BincodeServiceError::Convert(e.to_string())) },
)
}
};
@ -84,8 +84,8 @@ where
Box::pin(async move {
let response = response
.await
.map_err(|e| SerdeServiceError::Call(e.to_string()))?;
serialize(&response).map_err(|e| SerdeServiceError::Convert(e.to_string()))
.map_err(|e| BincodeServiceError::Call(e.to_string()))?;
serialize(&response).map_err(|e| BincodeServiceError::Convert(e.to_string()))
})
}
}
@ -125,7 +125,7 @@ mod tests {
async fn can_serde_responses() {
let content = serialize(&Test { field: "hello world!".to_string() }).unwrap();
let mut service = ServiceBuilder::new()
.layer(SerdeLayer::<Test>::new())
.layer(BincodeLayer::<Test>::new())
.service(App);
let result = service.ready().await.unwrap().call(content.clone()).await.unwrap();
assert_eq!(result, content);