1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
use crate::{Decoder, Encoder};

/// A codec for storing ProtoBuf messages that relies on [`prost`](https://github.com/tokio-rs/prost) to parse.
///
/// [Protocol buffers](https://protobuf.dev/overview/) is a serialisation format useful for
/// long-term storage. It provides semantics for versioning that are not present in JSON or other
/// formats. [`prost`] is a Rust implementation of Protocol Buffers.
///
/// This codec uses [`prost`](https://github.com/tokio-rs/prost) to encode the message into a byte stream.
/// To use it with local storage in the example below we wrap it with [`Base64`] to represent the bytes as a string.
///
/// ## Example
/// ```ignore
/// # use leptos::*;
/// # use leptos_use::storage::{StorageType, use_local_storage, use_session_storage, use_storage, UseStorageOptions};
/// # use codee::{string::Base64, binary::ProstCodec};
/// #
/// # #[component]
/// # pub fn Demo() -> impl IntoView {
/// // Primitive types:
/// let (get, set, remove) = use_local_storage::<i32, Base64<ProstCodec>>("my-key");
///
/// // Structs:
/// #[derive(Clone, PartialEq, prost::Message)]
/// pub struct MyState {
///     #[prost(string, tag = "1")]
///     pub hello: String,
/// }
/// let (get, set, remove) = use_local_storage::<MyState, Base64<ProstCodec>>("my-struct-key");
/// #    view! { }
/// # }
/// ```
///
/// Note: we've defined and used the `prost` attribute here for brevity. Alternate usage would be to
/// describe the message in a .proto file and use [`prost_build`](https://docs.rs/prost-build) to
/// auto-generate the Rust code.
pub struct ProstCodec;

impl<T: prost::Message> Encoder<T> for ProstCodec {
    type Error = ();
    type Encoded = Vec<u8>;

    fn encode(val: &T) -> Result<Self::Encoded, Self::Error> {
        let buf = val.encode_to_vec();
        Ok(buf)
    }
}

impl<T: prost::Message + Default> Decoder<T> for ProstCodec {
    type Error = prost::DecodeError;
    type Encoded = [u8];

    fn decode(val: &Self::Encoded) -> Result<T, Self::Error> {
        T::decode(val)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_prost_codec() {
        #[derive(Clone, PartialEq, prost::Message)]
        struct Test {
            #[prost(string, tag = "1")]
            s: String,
            #[prost(int32, tag = "2")]
            i: i32,
        }
        let t = Test {
            s: String::from("party time 🎉"),
            i: 42,
        };
        assert_eq!(ProstCodec::decode(&ProstCodec::encode(&t).unwrap()), Ok(t));
    }
}