mirror of
https://github.com/leptos-rs/leptos
synced 2024-11-10 14:54:16 +00:00
fix rkyv
This commit is contained in:
parent
7d45e6bb13
commit
6a1685936b
1 changed files with 28 additions and 15 deletions
|
@ -1,14 +1,16 @@
|
|||
use super::{Encoding, FromReq, FromRes, IntoReq, IntoRes};
|
||||
use crate::{
|
||||
error::ServerFnError,
|
||||
request::{ClientReq, Req},
|
||||
response::{ClientRes, Res},
|
||||
};
|
||||
use bytes::Bytes;
|
||||
use rkyv::{
|
||||
de::deserializers::SharedDeserializeMap, ser::serializers::AllocSerializer,
|
||||
validation::validators::DefaultValidator, Archive, CheckBytes, Deserialize, Serialize,
|
||||
validation::validators::DefaultValidator, Archive, CheckBytes, Deserialize,
|
||||
Serialize,
|
||||
};
|
||||
|
||||
use super::{Encoding, FromReq, FromRes, IntoReq, IntoRes};
|
||||
use crate::error::ServerFnError;
|
||||
use crate::request::{ClientReq, Req};
|
||||
use crate::response::{ClientRes, Res};
|
||||
use bytes::Bytes;
|
||||
|
||||
/// Pass arguments and receive responses using `rkyv` in a `POST` request.
|
||||
pub struct Rkyv;
|
||||
|
||||
|
@ -21,10 +23,16 @@ where
|
|||
Request: ClientReq<CustErr>,
|
||||
T: Serialize<AllocSerializer<1024>> + Send,
|
||||
T: Archive,
|
||||
T::Archived: for<'a> CheckBytes<DefaultValidator<'a>> + Deserialize<T, SharedDeserializeMap>,
|
||||
T::Archived: for<'a> CheckBytes<DefaultValidator<'a>>
|
||||
+ Deserialize<T, SharedDeserializeMap>,
|
||||
{
|
||||
fn into_req(self, path: &str, accepts: &str) -> Result<Request, ServerFnError<CustErr>> {
|
||||
let encoded = rkyv::to_bytes::<T, 1024>(&self)?;
|
||||
fn into_req(
|
||||
self,
|
||||
path: &str,
|
||||
accepts: &str,
|
||||
) -> Result<Request, ServerFnError<CustErr>> {
|
||||
let encoded = rkyv::to_bytes::<T, 1024>(&self)
|
||||
.map_err(|e| ServerFnError::Serialization(e.to_string()))?;
|
||||
let bytes = Bytes::copy_from_slice(encoded.as_ref());
|
||||
Request::try_new_post_bytes(path, accepts, Rkyv::CONTENT_TYPE, bytes)
|
||||
}
|
||||
|
@ -35,11 +43,13 @@ where
|
|||
Request: Req<CustErr> + Send + 'static,
|
||||
T: Serialize<AllocSerializer<1024>> + Send,
|
||||
T: Archive,
|
||||
T::Archived: for<'a> CheckBytes<DefaultValidator<'a>> + Deserialize<T, SharedDeserializeMap>,
|
||||
T::Archived: for<'a> CheckBytes<DefaultValidator<'a>>
|
||||
+ Deserialize<T, SharedDeserializeMap>,
|
||||
{
|
||||
async fn from_req(req: Request) -> Result<Self, ServerFnError<CustErr>> {
|
||||
let body_bytes = req.try_into_bytes().await?;
|
||||
rkyv::from_bytes::<T>(&body_bytes).map_err(|e| ServerFnError::Args(e.to_string()))
|
||||
rkyv::from_bytes::<T>(&body_bytes)
|
||||
.map_err(|e| ServerFnError::Args(e.to_string()))
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -48,7 +58,8 @@ where
|
|||
Response: Res<CustErr>,
|
||||
T: Serialize<AllocSerializer<1024>> + Send,
|
||||
T: Archive,
|
||||
T::Archived: for<'a> CheckBytes<DefaultValidator<'a>> + Deserialize<T, SharedDeserializeMap>,
|
||||
T::Archived: for<'a> CheckBytes<DefaultValidator<'a>>
|
||||
+ Deserialize<T, SharedDeserializeMap>,
|
||||
{
|
||||
async fn into_res(self) -> Result<Response, ServerFnError<CustErr>> {
|
||||
let encoded = rkyv::to_bytes::<T, 1024>(&self)
|
||||
|
@ -63,10 +74,12 @@ where
|
|||
Response: ClientRes<CustErr> + Send,
|
||||
T: Serialize<AllocSerializer<1024>> + Send,
|
||||
T: Archive,
|
||||
T::Archived: for<'a> CheckBytes<DefaultValidator<'a>> + Deserialize<T, SharedDeserializeMap>,
|
||||
T::Archived: for<'a> CheckBytes<DefaultValidator<'a>>
|
||||
+ Deserialize<T, SharedDeserializeMap>,
|
||||
{
|
||||
async fn from_res(res: Response) -> Result<Self, ServerFnError<CustErr>> {
|
||||
let data = res.try_into_bytes().await?;
|
||||
rkyv::from_bytes::<T>(&data).map_err(|e| ServerFnError::Deserialization(e.to_string()))
|
||||
rkyv::from_bytes::<T>(&data)
|
||||
.map_err(|e| ServerFnError::Deserialization(e.to_string()))
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue