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
78
79
80
81
82
83
84
include!(concat!(env!("OUT_DIR"), "/diff.rs"));

use crate::{
    sdk::{
        commit::{CommitHash, CommitProof},
        events::EventRecord,
    },
    Error, EventLogType, ProtoBinding, Result,
};

/// Request commit diff from an event log.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DiffRequest {
    /// Type of event log to load the diff from.
    pub log_type: EventLogType,
    /// Hash of the commit to diff from.
    pub from_hash: Option<CommitHash>,
}

impl ProtoBinding for DiffRequest {
    type Inner = WireDiffRequest;
}

impl TryFrom<WireDiffRequest> for DiffRequest {
    type Error = Error;

    fn try_from(value: WireDiffRequest) -> Result<Self> {
        let from_hash = if let Some(from_hash) = value.from_hash {
            Some(from_hash.try_into()?)
        } else {
            None
        };
        Ok(Self {
            log_type: value.log_type.unwrap().try_into()?,
            from_hash,
        })
    }
}

impl From<DiffRequest> for WireDiffRequest {
    fn from(value: DiffRequest) -> WireDiffRequest {
        Self {
            log_type: Some(value.log_type.into()),
            from_hash: value.from_hash.map(|h| h.into()),
        }
    }
}

/// Response with an event log commit diff.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DiffResponse {
    /// Collection of event records from the commit hash.
    pub patch: Vec<EventRecord>,
    /// Checkpoint of remote HEAD.
    pub checkpoint: CommitProof,
}

impl ProtoBinding for DiffResponse {
    type Inner = WireDiffResponse;
}

impl TryFrom<WireDiffResponse> for DiffResponse {
    type Error = Error;

    fn try_from(value: WireDiffResponse) -> Result<Self> {
        let mut events = Vec::with_capacity(value.patch.len());
        for patch in value.patch {
            events.push(patch.try_into()?);
        }
        Ok(Self {
            patch: events,
            checkpoint: value.checkpoint.unwrap().try_into()?,
        })
    }
}

impl From<DiffResponse> for WireDiffResponse {
    fn from(value: DiffResponse) -> WireDiffResponse {
        Self {
            patch: value.patch.into_iter().map(|p| p.into()).collect(),
            checkpoint: Some(value.checkpoint.into()),
        }
    }
}