1use core::fmt;
7use rvm_types::RvmError;
8
9#[derive(Debug, Clone, Copy, PartialEq, Eq)]
11pub enum CapError {
12 InvalidHandle,
14 StaleHandle,
16 TableFull,
18 Revoked,
20 DelegationDepthExceeded,
22 GrantNotPermitted,
24 RightsEscalation,
26 TreeFull,
28 TypeMismatch,
30 Consumed,
32}
33
34impl fmt::Display for CapError {
35 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
36 match self {
37 Self::InvalidHandle => write!(f, "invalid capability handle"),
38 Self::StaleHandle => write!(f, "stale capability handle (generation mismatch)"),
39 Self::TableFull => write!(f, "capability table full"),
40 Self::Revoked => write!(f, "capability revoked"),
41 Self::DelegationDepthExceeded => write!(f, "delegation depth limit exceeded"),
42 Self::GrantNotPermitted => write!(f, "GRANT right not held"),
43 Self::RightsEscalation => write!(f, "rights escalation attempted"),
44 Self::TreeFull => write!(f, "derivation tree full"),
45 Self::TypeMismatch => write!(f, "capability type mismatch"),
46 Self::Consumed => write!(f, "capability consumed (GRANT_ONCE)"),
47 }
48 }
49}
50
51impl From<CapError> for RvmError {
52 fn from(e: CapError) -> Self {
53 match e {
54 CapError::InvalidHandle | CapError::GrantNotPermitted | CapError::RightsEscalation => {
55 RvmError::InsufficientCapability
56 }
57 CapError::StaleHandle | CapError::Revoked => RvmError::StaleCapability,
58 CapError::TableFull | CapError::TreeFull => RvmError::ResourceLimitExceeded,
59 CapError::DelegationDepthExceeded => RvmError::DelegationDepthExceeded,
60 CapError::TypeMismatch => RvmError::CapabilityTypeMismatch,
61 CapError::Consumed => RvmError::CapabilityConsumed,
62 }
63 }
64}
65
66pub type CapResult<T> = core::result::Result<T, CapError>;
68
69#[derive(Debug, Clone, Copy, PartialEq, Eq)]
71pub enum ProofError {
72 InvalidHandle,
74 StaleCapability,
76 InsufficientRights,
78 PolicyViolation,
83 P3NotImplemented,
85 DerivationChainBroken,
88}
89
90impl fmt::Display for ProofError {
91 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
92 match self {
93 Self::InvalidHandle => write!(f, "P1: invalid capability handle"),
94 Self::StaleCapability => write!(f, "P1: stale capability (epoch mismatch)"),
95 Self::InsufficientRights => write!(f, "P1: insufficient rights"),
96 Self::PolicyViolation => write!(f, "P2: policy violation"),
97 Self::P3NotImplemented => write!(f, "P3: not implemented in v1"),
98 Self::DerivationChainBroken => write!(f, "P3: derivation chain broken"),
99 }
100 }
101}
102
103impl From<ProofError> for RvmError {
104 fn from(e: ProofError) -> Self {
105 match e {
106 ProofError::InvalidHandle | ProofError::InsufficientRights => {
107 RvmError::InsufficientCapability
108 }
109 ProofError::StaleCapability => RvmError::StaleCapability,
110 ProofError::PolicyViolation | ProofError::DerivationChainBroken => {
111 RvmError::ProofInvalid
112 }
113 ProofError::P3NotImplemented => RvmError::Unsupported,
114 }
115 }
116}