include!(concat!(env!("OUT_DIR"), "/patch.rs"));
use crate::{
    sdk::{
        commit::{CommitHash, CommitProof},
        events::{CheckedPatch, EventRecord},
    },
    Error, EventLogType, ProtoBinding, Result,
};
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PatchRequest {
    pub log_type: EventLogType,
    pub commit: Option<CommitHash>,
    pub proof: CommitProof,
    pub patch: Vec<EventRecord>,
}
impl ProtoBinding for PatchRequest {
    type Inner = WirePatchRequest;
}
impl TryFrom<WirePatchRequest> for PatchRequest {
    type Error = Error;
    fn try_from(value: WirePatchRequest) -> Result<Self> {
        let commit = if let Some(commit) = value.commit {
            Some(commit.try_into()?)
        } else {
            None
        };
        let mut patch = Vec::with_capacity(value.patch.len());
        for event in value.patch {
            patch.push(event.try_into()?);
        }
        Ok(Self {
            log_type: value.log_type.unwrap().try_into()?,
            commit,
            proof: value.proof.unwrap().try_into()?,
            patch,
        })
    }
}
impl From<PatchRequest> for WirePatchRequest {
    fn from(value: PatchRequest) -> WirePatchRequest {
        Self {
            log_type: Some(value.log_type.into()),
            commit: value.commit.map(|c| c.into()),
            proof: Some(value.proof.into()),
            patch: value.patch.into_iter().map(|e| e.into()).collect(),
        }
    }
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PatchResponse {
    pub checked_patch: CheckedPatch,
}
impl ProtoBinding for PatchResponse {
    type Inner = WirePatchResponse;
}
impl TryFrom<WirePatchResponse> for PatchResponse {
    type Error = Error;
    fn try_from(value: WirePatchResponse) -> Result<Self> {
        Ok(Self {
            checked_patch: value.checked_patch.unwrap().try_into()?,
        })
    }
}
impl From<PatchResponse> for WirePatchResponse {
    fn from(value: PatchResponse) -> WirePatchResponse {
        Self {
            checked_patch: Some(value.checked_patch.into()),
        }
    }
}