codee/binary/
prost.rs

1use crate::{Decoder, Encoder};
2
3/// A codec for storing ProtoBuf messages that relies on [`prost`](https://github.com/tokio-rs/prost) to parse.
4///
5/// [Protocol buffers](https://protobuf.dev/overview/) is a serialisation format useful for
6/// long-term storage. It provides semantics for versioning that are not present in JSON or other
7/// formats. [`prost`] is a Rust implementation of Protocol Buffers.
8///
9/// This codec uses [`prost`](https://github.com/tokio-rs/prost) to encode the message into a byte stream.
10/// To use it with local storage in the example below we wrap it with [`Base64`] to represent the bytes as a string.
11///
12/// ## Example
13/// ```ignore
14/// # use leptos::*;
15/// # use leptos_use::storage::{StorageType, use_local_storage, use_session_storage, use_storage, UseStorageOptions};
16/// # use codee::{string::Base64, binary::ProstCodec};
17/// #
18/// # #[component]
19/// # pub fn Demo() -> impl IntoView {
20/// // Primitive types:
21/// let (get, set, remove) = use_local_storage::<i32, Base64<ProstCodec>>("my-key");
22///
23/// // Structs:
24/// #[derive(Clone, PartialEq, prost::Message)]
25/// pub struct MyState {
26///     #[prost(string, tag = "1")]
27///     pub hello: String,
28/// }
29/// let (get, set, remove) = use_local_storage::<MyState, Base64<ProstCodec>>("my-struct-key");
30/// #    view! { }
31/// # }
32/// ```
33///
34/// Note: we've defined and used the `prost` attribute here for brevity. Alternate usage would be to
35/// describe the message in a .proto file and use [`prost_build`](https://docs.rs/prost-build) to
36/// auto-generate the Rust code.
37pub struct ProstCodec;
38
39impl<T: prost::Message> Encoder<T> for ProstCodec {
40    type Error = ();
41    type Encoded = Vec<u8>;
42
43    fn encode(val: &T) -> Result<Self::Encoded, Self::Error> {
44        let buf = val.encode_to_vec();
45        Ok(buf)
46    }
47}
48
49impl<T: prost::Message + Default> Decoder<T> for ProstCodec {
50    type Error = prost::DecodeError;
51    type Encoded = [u8];
52
53    fn decode(val: &Self::Encoded) -> Result<T, Self::Error> {
54        T::decode(val)
55    }
56}
57
58#[cfg(test)]
59mod tests {
60    use super::*;
61
62    #[test]
63    fn test_prost_codec() {
64        #[derive(Clone, PartialEq, prost::Message)]
65        struct Test {
66            #[prost(string, tag = "1")]
67            s: String,
68            #[prost(int32, tag = "2")]
69            i: i32,
70        }
71        let t = Test {
72            s: String::from("party time 🎉"),
73            i: 42,
74        };
75        assert_eq!(ProstCodec::decode(&ProstCodec::encode(&t).unwrap()), Ok(t));
76    }
77}