sos_protocol/bindings/
notifications.rs

1include!(concat!(env!("OUT_DIR"), "/notifications.rs"));
2
3use crate::{Error, ProtoBinding, Result};
4use sos_core::{commit::CommitHash, AccountId};
5use sos_sync::MergeOutcome;
6
7/// Notification sent by the server when changes were made.
8#[derive(Debug, Clone, PartialEq, Eq)]
9pub struct NetworkChangeEvent {
10    /// Account identifier.
11    account_id: AccountId,
12    /// Connection identifier that made the change.
13    connection_id: String,
14    /// Root commit for the entire account.
15    root: CommitHash,
16    /// Merge outcome.
17    outcome: MergeOutcome,
18}
19
20impl NetworkChangeEvent {
21    /// Create a new change notification.
22    pub fn new(
23        account_id: &AccountId,
24        connection_id: String,
25        root: CommitHash,
26        outcome: MergeOutcome,
27    ) -> Self {
28        Self {
29            account_id: *account_id,
30            connection_id,
31            root,
32            outcome,
33        }
34    }
35
36    /// AccountId of the account owner.
37    pub fn account_id(&self) -> &AccountId {
38        &self.account_id
39    }
40
41    /// Connection identifier.
42    pub fn connection_id(&self) -> &str {
43        &self.connection_id
44    }
45
46    /// Account root commit hash.
47    pub fn root(&self) -> &CommitHash {
48        &self.root
49    }
50
51    /// Merge outcome.
52    pub fn outcome(&self) -> &MergeOutcome {
53        &self.outcome
54    }
55}
56
57impl From<NetworkChangeEvent>
58    for (AccountId, String, CommitHash, MergeOutcome)
59{
60    fn from(value: NetworkChangeEvent) -> Self {
61        (
62            value.account_id,
63            value.connection_id,
64            value.root,
65            value.outcome,
66        )
67    }
68}
69
70impl ProtoBinding for NetworkChangeEvent {
71    type Inner = WireNetworkChangeEvent;
72}
73
74impl TryFrom<WireNetworkChangeEvent> for NetworkChangeEvent {
75    type Error = Error;
76
77    fn try_from(value: WireNetworkChangeEvent) -> Result<Self> {
78        let account_id: [u8; 20] = value.account_id.as_slice().try_into()?;
79        Ok(Self {
80            account_id: account_id.into(),
81            connection_id: value.connection_id,
82            root: value.root.unwrap().try_into()?,
83            outcome: value.outcome.unwrap().try_into()?,
84        })
85    }
86}
87
88impl From<NetworkChangeEvent> for WireNetworkChangeEvent {
89    fn from(value: NetworkChangeEvent) -> WireNetworkChangeEvent {
90        Self {
91            account_id: value.account_id().as_ref().to_vec(),
92            connection_id: value.connection_id,
93            root: Some(value.root.into()),
94            outcome: Some(value.outcome.into()),
95        }
96    }
97}