use std::fmt;
use crate::seal::KEY_ID_SIZE;
use super::Stream;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Report {
pub sealed: u8,
pub provider: [u8; 32],
pub seal_key_id: Option<[u8; KEY_ID_SIZE]>,
}
impl Report {
pub(crate) fn of(provider: [u8; 32], key_id: Option<[u8; KEY_ID_SIZE]>) -> Report {
Report {
sealed: u8::from(key_id.is_some()),
provider,
seal_key_id: key_id,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ReportError {
NotSettled,
NotACaller,
}
impl ReportError {
pub fn name(self) -> &'static str {
match self {
ReportError::NotSettled => "not_settled",
ReportError::NotACaller => "not_a_caller",
}
}
}
impl fmt::Display for ReportError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.name())
}
}
impl std::error::Error for ReportError {}
impl Stream {
pub fn report(&self) -> Result<Report, ReportError> {
if !self.inner.caller {
return Err(ReportError::NotACaller);
}
if !self.inner.side().settled {
return Err(ReportError::NotSettled);
}
let key_id = self.inner.sealing.as_ref().map(|s| s.key_id());
Ok(Report::of(self.inner.open.target, key_id))
}
}