Skip to main content

server_fn/codec/
bitcode.rs

1use super::{Patch, Post, Put};
2use crate::{ContentType, Decodes, Encodes, Format, FormatType};
3use bytes::Bytes;
4
5/// Serializes and deserializes with [`bitcode`].
6pub struct BitcodeEncoding;
7
8impl ContentType for BitcodeEncoding {
9    const CONTENT_TYPE: &'static str = "application/bitcode";
10}
11
12impl FormatType for BitcodeEncoding {
13    const FORMAT_TYPE: Format = Format::Binary;
14}
15
16impl<T> Encodes<T> for BitcodeEncoding
17where
18    T: bitcode::Encode,
19{
20    type Error = std::convert::Infallible;
21
22    fn encode(value: &T) -> Result<Bytes, Self::Error> {
23        Ok(Bytes::from(bitcode::encode(value)))
24    }
25}
26
27impl<T> Decodes<T> for BitcodeEncoding
28where
29    T: bitcode::DecodeOwned,
30{
31    type Error = bitcode::Error;
32
33    fn decode(bytes: Bytes) -> Result<T, Self::Error> {
34        bitcode::decode(bytes.as_ref())
35    }
36}
37
38/// Pass arguments and receive responses using `bitcode` in a `POST` request.
39pub type Bitcode = Post<BitcodeEncoding>;
40
41/// Pass arguments and receive responses using `bitcode` in the body of a `PATCH` request.
42/// **Note**: Browser support for `PATCH` requests without JS/WASM may be poor.
43/// Consider using a `POST` request if functionality without JS/WASM is required.
44pub type PatchBitcode = Patch<BitcodeEncoding>;
45
46/// Pass arguments and receive responses using `bitcode` in the body of a `PUT` request.
47/// **Note**: Browser support for `PUT` requests without JS/WASM may be poor.
48/// Consider using a `POST` request if functionality without JS/WASM is required.
49pub type PutBitcode = Put<BitcodeEncoding>;