include!(concat!(env!("OUT_DIR"), "/notifications.rs"));
use crate::{Error, MergeOutcome, ProtoBinding, Result};
use sos_sdk::{commit::CommitHash, signer::ecdsa::Address};
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ChangeNotification {
address: Address,
connection_id: String,
root: CommitHash,
outcome: MergeOutcome,
}
impl ChangeNotification {
pub fn new(
address: &Address,
connection_id: String,
root: CommitHash,
outcome: MergeOutcome,
) -> Self {
Self {
address: *address,
connection_id,
root,
outcome,
}
}
pub fn address(&self) -> &Address {
&self.address
}
pub fn connection_id(&self) -> &str {
&self.connection_id
}
pub fn root(&self) -> &CommitHash {
&self.root
}
pub fn outcome(&self) -> &MergeOutcome {
&self.outcome
}
}
impl ProtoBinding for ChangeNotification {
type Inner = WireChangeNotification;
}
impl TryFrom<WireChangeNotification> for ChangeNotification {
type Error = Error;
fn try_from(value: WireChangeNotification) -> Result<Self> {
let address: [u8; 20] = value.address.as_slice().try_into()?;
Ok(Self {
address: address.into(),
connection_id: value.connection_id,
root: value.root.unwrap().try_into()?,
outcome: value.outcome.unwrap().try_into()?,
})
}
}
impl From<ChangeNotification> for WireChangeNotification {
fn from(value: ChangeNotification) -> WireChangeNotification {
Self {
address: value.address().as_ref().to_vec(),
connection_id: value.connection_id,
root: Some(value.root.into()),
outcome: Some(value.outcome.into()),
}
}
}