use core::fmt;
use rvm_types::RvmError;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CapError {
InvalidHandle,
StaleHandle,
TableFull,
Revoked,
DelegationDepthExceeded,
GrantNotPermitted,
RightsEscalation,
TreeFull,
TypeMismatch,
Consumed,
}
impl fmt::Display for CapError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::InvalidHandle => write!(f, "invalid capability handle"),
Self::StaleHandle => write!(f, "stale capability handle (generation mismatch)"),
Self::TableFull => write!(f, "capability table full"),
Self::Revoked => write!(f, "capability revoked"),
Self::DelegationDepthExceeded => write!(f, "delegation depth limit exceeded"),
Self::GrantNotPermitted => write!(f, "GRANT right not held"),
Self::RightsEscalation => write!(f, "rights escalation attempted"),
Self::TreeFull => write!(f, "derivation tree full"),
Self::TypeMismatch => write!(f, "capability type mismatch"),
Self::Consumed => write!(f, "capability consumed (GRANT_ONCE)"),
}
}
}
impl From<CapError> for RvmError {
fn from(e: CapError) -> Self {
match e {
CapError::InvalidHandle | CapError::GrantNotPermitted | CapError::RightsEscalation => {
RvmError::InsufficientCapability
}
CapError::StaleHandle | CapError::Revoked => RvmError::StaleCapability,
CapError::TableFull | CapError::TreeFull => RvmError::ResourceLimitExceeded,
CapError::DelegationDepthExceeded => RvmError::DelegationDepthExceeded,
CapError::TypeMismatch => RvmError::CapabilityTypeMismatch,
CapError::Consumed => RvmError::CapabilityConsumed,
}
}
}
pub type CapResult<T> = core::result::Result<T, CapError>;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ProofError {
InvalidHandle,
StaleCapability,
InsufficientRights,
PolicyViolation,
P3NotImplemented,
DerivationChainBroken,
}
impl fmt::Display for ProofError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::InvalidHandle => write!(f, "P1: invalid capability handle"),
Self::StaleCapability => write!(f, "P1: stale capability (epoch mismatch)"),
Self::InsufficientRights => write!(f, "P1: insufficient rights"),
Self::PolicyViolation => write!(f, "P2: policy violation"),
Self::P3NotImplemented => write!(f, "P3: not implemented in v1"),
Self::DerivationChainBroken => write!(f, "P3: derivation chain broken"),
}
}
}
impl From<ProofError> for RvmError {
fn from(e: ProofError) -> Self {
match e {
ProofError::InvalidHandle | ProofError::InsufficientRights => {
RvmError::InsufficientCapability
}
ProofError::StaleCapability => RvmError::StaleCapability,
ProofError::PolicyViolation | ProofError::DerivationChainBroken => {
RvmError::ProofInvalid
}
ProofError::P3NotImplemented => RvmError::Unsupported,
}
}
}