Skip to main content

datum_persistence/serialization/
protobuf.rs

1use std::marker::PhantomData;
2
3use prost::Message;
4
5use super::{PersistenceSerializer, SerializerId};
6use crate::{PersistenceError, PersistenceResult};
7
8/// Protobuf serializer for hand-written [`prost::Message`] journal types.
9///
10/// The caller supplies both durable protocol identifiers. Existing serializer
11/// ids and manifests must remain registered and decodable after schema changes.
12pub struct ProstSerializer<J> {
13    serializer_id: SerializerId,
14    manifest: String,
15    marker: PhantomData<fn() -> J>,
16}
17
18impl<J> ProstSerializer<J> {
19    /// Creates a protobuf serializer with a stable id and manifest.
20    #[must_use]
21    pub fn new(serializer_id: SerializerId, manifest: impl Into<String>) -> Self {
22        Self {
23            serializer_id,
24            manifest: manifest.into(),
25            marker: PhantomData,
26        }
27    }
28}
29
30impl<J> PersistenceSerializer<J> for ProstSerializer<J>
31where
32    J: Message + Default + 'static,
33{
34    fn serializer_id(&self) -> SerializerId {
35        self.serializer_id
36    }
37
38    fn manifest(&self, _value: &J) -> String {
39        self.manifest.clone()
40    }
41
42    fn serialize(&self, value: &J) -> PersistenceResult<Vec<u8>> {
43        Ok(value.encode_to_vec())
44    }
45
46    fn deserialize(&self, _manifest: &str, payload: &[u8]) -> PersistenceResult<J> {
47        J::decode(payload).map_err(|error| PersistenceError::Serialization(error.to_string()))
48    }
49}
50
51#[cfg(test)]
52mod tests {
53    use super::*;
54    use crate::SerializerCatalog;
55
56    #[derive(Clone, PartialEq, Message)]
57    struct AccountOpened {
58        #[prost(string, tag = "1")]
59        account_id: String,
60        #[prost(uint64, tag = "2")]
61        opening_balance: u64,
62        // Added after the v1 fixture was committed. Protobuf supplies the
63        // default when decoding the old bytes.
64        #[prost(string, tag = "3")]
65        source: String,
66    }
67
68    const ACCOUNT_OPENED_V1: &[u8] = &[0x0a, 0x06, b'a', b'c', b'c', b't', b'-', b'7', 0x10, 0x7d];
69
70    #[test]
71    fn prost_serializer_round_trips() {
72        let serializer =
73            ProstSerializer::<AccountOpened>::new(SerializerId::new(41), "account-opened-v2");
74        let event = AccountOpened {
75            account_id: "acct-9".to_owned(),
76            opening_balance: 500,
77            source: "api".to_owned(),
78        };
79
80        let bytes = serializer.serialize(&event).unwrap();
81        assert_eq!(
82            serializer.deserialize("account-opened-v2", &bytes).unwrap(),
83            event
84        );
85    }
86
87    #[test]
88    fn committed_v1_fixture_decodes_with_current_schema() {
89        let serializer =
90            ProstSerializer::<AccountOpened>::new(SerializerId::new(41), "account-opened-v2");
91        let event = serializer
92            .deserialize("account-opened-v1", ACCOUNT_OPENED_V1)
93            .unwrap();
94
95        assert_eq!(event.account_id, "acct-7");
96        assert_eq!(event.opening_balance, 125);
97        assert_eq!(event.source, "");
98    }
99
100    #[test]
101    fn prost_serializer_works_through_catalog() {
102        let catalog = SerializerCatalog::new(ProstSerializer::<AccountOpened>::new(
103            SerializerId::new(41),
104            "account-opened-v2",
105        ));
106        let event = AccountOpened {
107            account_id: "acct-10".to_owned(),
108            opening_balance: 12,
109            source: "batch".to_owned(),
110        };
111
112        let frame = catalog.encode(&event).unwrap();
113        assert_eq!(catalog.decode(&frame).unwrap(), event);
114    }
115}