add serde-wasm-bindgen encoding for resources

This commit is contained in:
Greg Johnston 2024-03-24 15:59:35 -04:00
parent 54fd74839a
commit 20c29cab89
3 changed files with 78 additions and 1 deletions

View File

@ -22,6 +22,9 @@ futures = "0.3"
# serialization formats
serde = { version = "1", optional = true }
serde-wasm-bindgen = { version = "0.6", optional = true }
js-sys = { version = "0.3", optional = true }
wasm-bindgen = { version = "0.2", optional = true }
serde_json = { version = "1", optional = true }
miniserde = { version = "0.1", optional = true }
rkyv = { version = "0.7", optional = true, features = [
@ -40,6 +43,7 @@ default-tls = ["server_fn/default-tls"]
rustls = ["server_fn/rustls"]
hydration = ["reactive_graph/hydration", "dep:serde", "dep:serde_json"]
rkyv = ["dep:rkyv", "dep:base64"]
serde-wasm-bindgen = ["dep:serde-wasm-bindgen", "dep:js-sys", "dep:wasm-bindgen"]
tracing = ["dep:tracing"]
[package.metadata.cargo-all-features]

View File

@ -4,6 +4,8 @@ use crate::serializers::Miniserde;
use crate::serializers::Rkyv;
#[cfg(feature = "serde-lite")]
use crate::serializers::SerdeLite;
#[cfg(feature = "serde-wasm-bindgen")]
use crate::serializers::SerdeWasmBindgen;
use crate::serializers::{SerdeJson, SerializableData, Serializer, Str};
use core::{fmt::Debug, marker::PhantomData};
use futures::Future;
@ -55,7 +57,7 @@ where
T::SerErr: Debug,
T::DeErr: Debug,
{
pub fn new<S, Fut>(
pub fn new_serde<S, Fut>(
source: impl Fn() -> S + Send + Sync + 'static,
fetcher: impl Fn(S) -> Fut + Send + Sync + 'static,
) -> Self
@ -68,6 +70,25 @@ where
}
}
#[cfg(feature = "serde-wasm-bindgen")]
impl<T> ArcResource<T, SerdeWasmBindgen>
where
T: Debug + SerializableData<SerdeWasmBindgen>,
T::SerErr: Debug,
T::DeErr: Debug,
{
pub fn new_serde_wb<S, Fut>(
source: impl Fn() -> S + Send + Sync + 'static,
fetcher: impl Fn(S) -> Fut + Send + Sync + 'static,
) -> Self
where
S: PartialEq + Clone + Send + Sync + 'static,
T: Send + Sync + 'static,
Fut: Future<Output = T> + Send + 'static,
{
ArcResource::new_with_encoding(source, fetcher)
}
}
#[cfg(feature = "miniserde")]
impl<T> ArcResource<T, Miniserde>
where
@ -282,6 +303,26 @@ where
}
}
#[cfg(feature = "serde-wasm-bindgen")]
impl<T> Resource<T, SerdeWasmBindgen>
where
T: Debug + SerializableData<SerdeWasmBindgen> + Send + Sync + 'static,
T::SerErr: Debug,
T::DeErr: Debug,
{
pub fn new_serde_wb<S, Fut>(
source: impl Fn() -> S + Send + Sync + 'static,
fetcher: impl Fn(S) -> Fut + Send + Sync + 'static,
) -> Self
where
S: PartialEq + Clone + Send + Sync + 'static,
T: Send + Sync + 'static,
Fut: Future<Output = T> + Send + 'static,
{
Resource::new_with_encoding(source, fetcher)
}
}
#[cfg(feature = "miniserde")]
impl<T> Resource<T, Miniserde>
where

View File

@ -200,3 +200,35 @@ mod rkyv {
#[cfg(feature = "rkyv")]
pub use rkyv::*;
#[cfg(feature = "serde-wasm-bindgen")]
mod serde_wasm_bindgen {
use super::{SerializableData, Serializer};
use serde::{de::DeserializeOwned, Serialize};
/// A [`Serializer`] that serializes using [`serde_json`] and deserializes using
/// [`serde-wasm-bindgen`].
pub struct SerdeWasmBindgen;
impl Serializer for SerdeWasmBindgen {}
impl<T> SerializableData<SerdeWasmBindgen> for T
where
T: DeserializeOwned + Serialize,
{
type SerErr = serde_json::Error;
type DeErr = wasm_bindgen::JsValue;
fn ser(&self) -> Result<String, Self::SerErr> {
serde_json::to_string(&self)
}
fn de(data: &str) -> Result<Self, Self::DeErr> {
let json = js_sys::JSON::parse(data)?;
serde_wasm_bindgen::from_value(json).map_err(Into::into)
}
}
}
#[cfg(feature = "serde-wasm-bindgen")]
pub use serde_wasm_bindgen::*;