sos_protocol/bindings/
diff.rs

1include!(concat!(env!("OUT_DIR"), "/diff.rs"));
2
3use crate::{Error, ProtoBinding, Result};
4use sos_core::{
5    commit::{CommitHash, CommitProof},
6    events::{EventLogType, EventRecord},
7};
8
9/// Request commit diff from an event log.
10#[derive(Debug, Clone, PartialEq, Eq)]
11pub struct DiffRequest {
12    /// Type of event log to load the diff from.
13    pub log_type: EventLogType,
14    /// Hash of the commit to diff from.
15    pub from_hash: Option<CommitHash>,
16}
17
18impl ProtoBinding for DiffRequest {
19    type Inner = WireDiffRequest;
20}
21
22impl TryFrom<WireDiffRequest> for DiffRequest {
23    type Error = Error;
24
25    fn try_from(value: WireDiffRequest) -> Result<Self> {
26        let from_hash = if let Some(from_hash) = value.from_hash {
27            Some(from_hash.try_into()?)
28        } else {
29            None
30        };
31        Ok(Self {
32            log_type: value.log_type.unwrap().try_into()?,
33            from_hash,
34        })
35    }
36}
37
38impl From<DiffRequest> for WireDiffRequest {
39    fn from(value: DiffRequest) -> WireDiffRequest {
40        Self {
41            log_type: Some(value.log_type.into()),
42            from_hash: value.from_hash.map(|h| h.into()),
43        }
44    }
45}
46
47/// Response with an event log commit diff.
48#[derive(Debug, Clone, PartialEq, Eq)]
49pub struct DiffResponse {
50    /// Collection of event records from the commit hash.
51    pub patch: Vec<EventRecord>,
52    /// Checkpoint of remote HEAD.
53    pub checkpoint: CommitProof,
54}
55
56impl ProtoBinding for DiffResponse {
57    type Inner = WireDiffResponse;
58}
59
60impl TryFrom<WireDiffResponse> for DiffResponse {
61    type Error = Error;
62
63    fn try_from(value: WireDiffResponse) -> Result<Self> {
64        let mut events = Vec::with_capacity(value.patch.len());
65        for patch in value.patch {
66            events.push(patch.try_into()?);
67        }
68        Ok(Self {
69            patch: events,
70            checkpoint: value.checkpoint.unwrap().try_into()?,
71        })
72    }
73}
74
75impl From<DiffResponse> for WireDiffResponse {
76    fn from(value: DiffResponse) -> WireDiffResponse {
77        Self {
78            patch: value.patch.into_iter().map(|p| p.into()).collect(),
79            checkpoint: Some(value.checkpoint.into()),
80        }
81    }
82}