-
-
Notifications
You must be signed in to change notification settings - Fork 692
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add
serde-lite
codec for server functions (#2168)
- Loading branch information
1 parent
d8514c1
commit b84ff68
Showing
7 changed files
with
119 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
use super::{Encoding, FromReq, FromRes}; | ||
use crate::{ | ||
error::ServerFnError, | ||
request::{ClientReq, Req}, | ||
response::{ClientRes, Res}, | ||
IntoReq, IntoRes, | ||
}; | ||
use http::Method; | ||
use serde_lite::{Deserialize, Serialize}; | ||
/// Pass arguments and receive responses as JSON in the body of a `POST` request. | ||
pub struct SerdeLite; | ||
|
||
impl Encoding for SerdeLite { | ||
const CONTENT_TYPE: &'static str = "application/json"; | ||
const METHOD: Method = Method::POST; | ||
} | ||
|
||
impl<CustErr, T, Request> IntoReq<CustErr, Request, SerdeLite> for T | ||
where | ||
Request: ClientReq<CustErr>, | ||
T: Serialize + Send, | ||
{ | ||
fn into_req( | ||
self, | ||
path: &str, | ||
accepts: &str, | ||
) -> Result<Request, ServerFnError<CustErr>> { | ||
let data = serde_json::to_string( | ||
&self | ||
.serialize() | ||
.map_err(|e| ServerFnError::Serialization(e.to_string()))?, | ||
) | ||
.map_err(|e| ServerFnError::Serialization(e.to_string()))?; | ||
Request::try_new_post(path, accepts, SerdeLite::CONTENT_TYPE, data) | ||
} | ||
} | ||
|
||
impl<CustErr, T, Request> FromReq<CustErr, Request, SerdeLite> for T | ||
where | ||
Request: Req<CustErr> + Send + 'static, | ||
T: Deserialize, | ||
{ | ||
async fn from_req(req: Request) -> Result<Self, ServerFnError<CustErr>> { | ||
let string_data = req.try_into_string().await?; | ||
Self::deserialize( | ||
&serde_json::from_str(&string_data) | ||
.map_err(|e| ServerFnError::Args(e.to_string()))?, | ||
) | ||
.map_err(|e| ServerFnError::Args(e.to_string())) | ||
} | ||
} | ||
|
||
impl<CustErr, T, Response> IntoRes<CustErr, Response, SerdeLite> for T | ||
where | ||
Response: Res<CustErr>, | ||
T: Serialize + Send, | ||
{ | ||
async fn into_res(self) -> Result<Response, ServerFnError<CustErr>> { | ||
let data = serde_json::to_string( | ||
&self | ||
.serialize() | ||
.map_err(|e| ServerFnError::Serialization(e.to_string()))?, | ||
) | ||
.map_err(|e| ServerFnError::Serialization(e.to_string()))?; | ||
Response::try_from_string(SerdeLite::CONTENT_TYPE, data) | ||
} | ||
} | ||
|
||
impl<CustErr, T, Response> FromRes<CustErr, Response, SerdeLite> for T | ||
where | ||
Response: ClientRes<CustErr> + Send, | ||
T: Deserialize + Send, | ||
{ | ||
async fn from_res(res: Response) -> Result<Self, ServerFnError<CustErr>> { | ||
let data = res.try_into_string().await?; | ||
Self::deserialize( | ||
&&serde_json::from_str(&data) | ||
.map_err(|e| ServerFnError::Args(e.to_string()))?, | ||
) | ||
.map_err(|e| ServerFnError::Deserialization(e.to_string())) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters