mirror of
https://github.com/leptos-rs/leptos.git
synced 2025-12-27 09:54:41 -05:00
feat: add bitcode encoding/decoding to server functions (#4376)
This commit is contained in:
@@ -64,6 +64,7 @@ http-body-util = { optional = true, workspace = true, default-features = true }
|
||||
rkyv = { optional = true, workspace = true, default-features = true }
|
||||
rmp-serde = { optional = true, workspace = true, default-features = true }
|
||||
base64 = { workspace = true, default-features = true }
|
||||
bitcode = { optional = true, workspace = true, default-features = true }
|
||||
|
||||
# client
|
||||
gloo-net = { optional = true, workspace = true, default-features = true }
|
||||
@@ -126,6 +127,7 @@ cbor = ["dep:ciborium"]
|
||||
rkyv = ["dep:rkyv"]
|
||||
msgpack = ["dep:rmp-serde"]
|
||||
postcard = ["dep:postcard"]
|
||||
bitcode = ["dep:bitcode"]
|
||||
default-tls = ["reqwest?/default-tls"]
|
||||
rustls = ["reqwest?/rustls-tls", "tokio-tungstenite?/rustls"]
|
||||
reqwest = ["dep:reqwest", "dep:tokio-tungstenite", "dep:tokio"]
|
||||
|
||||
49
server_fn/src/codec/bitcode.rs
Normal file
49
server_fn/src/codec/bitcode.rs
Normal file
@@ -0,0 +1,49 @@
|
||||
use super::{Patch, Post, Put};
|
||||
use crate::{ContentType, Decodes, Encodes, Format, FormatType};
|
||||
use bytes::Bytes;
|
||||
|
||||
/// Serializes and deserializes with [`bitcode`].
|
||||
pub struct BitcodeEncoding;
|
||||
|
||||
impl ContentType for BitcodeEncoding {
|
||||
const CONTENT_TYPE: &'static str = "application/bitcode";
|
||||
}
|
||||
|
||||
impl FormatType for BitcodeEncoding {
|
||||
const FORMAT_TYPE: Format = Format::Binary;
|
||||
}
|
||||
|
||||
impl<T> Encodes<T> for BitcodeEncoding
|
||||
where
|
||||
T: bitcode::Encode,
|
||||
{
|
||||
type Error = std::convert::Infallible;
|
||||
|
||||
fn encode(value: &T) -> Result<Bytes, Self::Error> {
|
||||
Ok(Bytes::from(bitcode::encode(value)))
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Decodes<T> for BitcodeEncoding
|
||||
where
|
||||
T: bitcode::DecodeOwned,
|
||||
{
|
||||
type Error = bitcode::Error;
|
||||
|
||||
fn decode(bytes: Bytes) -> Result<T, Self::Error> {
|
||||
bitcode::decode(bytes.as_ref())
|
||||
}
|
||||
}
|
||||
|
||||
/// Pass arguments and receive responses using `bitcode` in a `POST` request.
|
||||
pub type Bitcode = Post<BitcodeEncoding>;
|
||||
|
||||
/// Pass arguments and receive responses using `bitcode` in the body of a `PATCH` request.
|
||||
/// **Note**: Browser support for `PATCH` requests without JS/WASM may be poor.
|
||||
/// Consider using a `POST` request if functionality without JS/WASM is required.
|
||||
pub type PatchBitcode = Patch<BitcodeEncoding>;
|
||||
|
||||
/// Pass arguments and receive responses using `bitcode` in the body of a `PUT` request.
|
||||
/// **Note**: Browser support for `PUT` requests without JS/WASM may be poor.
|
||||
/// Consider using a `POST` request if functionality without JS/WASM is required.
|
||||
pub type PutBitcode = Put<BitcodeEncoding>;
|
||||
@@ -50,6 +50,11 @@ mod postcard;
|
||||
#[cfg(feature = "postcard")]
|
||||
pub use postcard::*;
|
||||
|
||||
#[cfg(feature = "bitcode")]
|
||||
mod bitcode;
|
||||
#[cfg(feature = "bitcode")]
|
||||
pub use bitcode::*;
|
||||
|
||||
mod patch;
|
||||
pub use patch::*;
|
||||
mod post;
|
||||
|
||||
@@ -133,6 +133,8 @@ pub use ::bytes as bytes_export;
|
||||
#[doc(hidden)]
|
||||
pub use ::http as http_export;
|
||||
use base64::{engine::general_purpose::STANDARD_NO_PAD, DecodeError, Engine};
|
||||
#[cfg(feature = "bitcode")]
|
||||
pub use bitcode;
|
||||
// re-exported to make it possible to implement a custom Client without adding a separate
|
||||
// dependency on `bytes`
|
||||
pub use bytes::Bytes;
|
||||
|
||||
@@ -10,7 +10,8 @@
|
||||
feature = "multipart",
|
||||
feature = "serde-lite",
|
||||
feature = "cbor",
|
||||
feature = "msgpack"
|
||||
feature = "msgpack",
|
||||
feature = "bitcode",
|
||||
))
|
||||
))]
|
||||
|
||||
|
||||
Reference in New Issue
Block a user