use alloc::string::String;
use thiserror::Error;
pub type Result<T> = core::result::Result<T, LeafError>;
#[derive(Debug, Clone, PartialEq, Eq, Error)]
pub enum LeafError {
#[error("Storage failed: {0}")]
Storage(String),
#[error("Device is not provisioned")]
NotProvisioned,
#[error("Device is already provisioned")]
AlreadyProvisioned,
#[error("Crypto failed: {0}")]
Crypto(String),
#[error("MLS failed: {0}")]
Mls(String),
#[error("Malformed frame: {0}")]
MalformedFrame(String),
#[error("Control frame refused: {0}")]
ControlFrameRefused(String),
#[error("Control frame refused as stale: {0}")]
StaleControlFrame(String),
#[error("Identity binding failed: {0}")]
IdentityBinding(String),
#[error("No session with {0}")]
NoSession(String),
#[error("Unsolicited welcome: {0}")]
UnsolicitedWelcome(String),
#[error("Stale key package: {0}")]
StaleKeyPackage(String),
#[error("Peer table is full")]
TooManyPeers,
#[error("{0}")]
Sealed(String),
}
impl From<offline_protocol_sealed::SealedError> for LeafError {
fn from(e: offline_protocol_sealed::SealedError) -> Self {
use offline_protocol_sealed::SealedError as S;
match e {
S::Serialization(m) => LeafError::Sealed(alloc::format!("Serialization failed: {m}")),
S::Deserialization(m) => {
LeafError::Sealed(alloc::format!("Deserialization failed: {m}"))
}
S::InvalidGroupId(m) => LeafError::Sealed(alloc::format!("Invalid group id: {m}")),
S::InvalidPublicKey(m) => LeafError::Sealed(alloc::format!("Invalid public key: {m}")),
S::FieldTooLarge(n) => LeafError::Sealed(alloc::format!(
"Field too large for canonical payload length prefix: {n} bytes"
)),
}
}
}