sos_protocol/bindings/
patch.rs

1include!(concat!(env!("OUT_DIR"), "/patch.rs"));
2
3use crate::{Error, ProtoBinding, Result};
4use sos_core::{
5    commit::{CommitHash, CommitProof},
6    events::{patch::CheckedPatch, EventLogType, EventRecord},
7};
8
9/// Request to patch an event log from a specific commit.
10///
11/// Used during auto merge to force push a combined collection
12/// of events.
13#[derive(Debug, Clone, PartialEq, Eq)]
14pub struct PatchRequest {
15    /// Type of event log to patch.
16    pub log_type: EventLogType,
17    /// Hash of a commit to rewind to before
18    /// applying the patch.
19    pub commit: Option<CommitHash>,
20    /// Proof for HEAD of the event log before the
21    /// events are applied.
22    pub proof: CommitProof,
23    /// Patch of events to apply.
24    pub patch: Vec<EventRecord>,
25}
26
27impl ProtoBinding for PatchRequest {
28    type Inner = WirePatchRequest;
29}
30
31impl TryFrom<WirePatchRequest> for PatchRequest {
32    type Error = Error;
33
34    fn try_from(value: WirePatchRequest) -> Result<Self> {
35        let commit = if let Some(commit) = value.commit {
36            Some(commit.try_into()?)
37        } else {
38            None
39        };
40
41        let mut patch = Vec::with_capacity(value.patch.len());
42        for event in value.patch {
43            patch.push(event.try_into()?);
44        }
45
46        Ok(Self {
47            log_type: value.log_type.unwrap().try_into()?,
48            commit,
49            proof: value.proof.unwrap().try_into()?,
50            patch,
51        })
52    }
53}
54
55impl From<PatchRequest> for WirePatchRequest {
56    fn from(value: PatchRequest) -> WirePatchRequest {
57        Self {
58            log_type: Some(value.log_type.into()),
59            commit: value.commit.map(|c| c.into()),
60            proof: Some(value.proof.into()),
61            patch: value.patch.into_iter().map(|e| e.into()).collect(),
62        }
63    }
64}
65
66/// Response from a patch request.
67#[derive(Debug, Clone, PartialEq, Eq)]
68pub struct PatchResponse {
69    /// Checked patch status.
70    pub checked_patch: CheckedPatch,
71}
72
73impl ProtoBinding for PatchResponse {
74    type Inner = WirePatchResponse;
75}
76
77impl TryFrom<WirePatchResponse> for PatchResponse {
78    type Error = Error;
79
80    fn try_from(value: WirePatchResponse) -> Result<Self> {
81        Ok(Self {
82            checked_patch: value.checked_patch.unwrap().try_into()?,
83        })
84    }
85}
86
87impl From<PatchResponse> for WirePatchResponse {
88    fn from(value: PatchResponse) -> WirePatchResponse {
89        Self {
90            checked_patch: Some(value.checked_patch.into()),
91        }
92    }
93}