1use alloc::string::String;
4use core::fmt;
5
6pub type Result<T> = core::result::Result<T, PolicyError>;
8
9#[derive(Debug)]
11pub enum PolicyError {
12 PolicyNotFound(String),
14
15 InvalidRule(String),
17
18 PermissionDenied {
20 peer_id: String,
22 reason: String,
24 },
25
26 InvalidPeerId(String),
28
29 SerializationError(String),
31
32 #[cfg(feature = "toml")]
34 TomlError(toml::de::Error),
35
36 TooManyRules {
43 max: usize,
45 attempted: usize,
47 },
48
49 PatternTooLong {
51 max: usize,
53 length: usize,
55 },
56
57 NameTooLong {
59 max: usize,
61 length: usize,
63 },
64
65 ExpressionTooDeep {
68 max: usize,
70 },
71
72 ExpressionTooLong {
74 max: usize,
76 length: usize,
78 },
79
80 InvalidExpression(String),
82
83 InternalError(String),
88}
89
90impl fmt::Display for PolicyError {
91 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
92 match self {
93 Self::PolicyNotFound(msg) => write!(f, "Policy not found: {}", msg),
94 Self::InvalidRule(msg) => write!(f, "Invalid policy rule: {}", msg),
95 Self::PermissionDenied { peer_id, reason } => {
96 write!(f, "Permission denied for peer {}: {}", peer_id, reason)
97 }
98 Self::InvalidPeerId(msg) => write!(f, "Invalid peer ID: {}", msg),
99 Self::SerializationError(msg) => write!(f, "Serialization error: {}", msg),
100 #[cfg(feature = "toml")]
101 Self::TomlError(e) => write!(f, "TOML parsing error: {}", e),
102 Self::TooManyRules { max, attempted } => write!(
103 f,
104 "Policy exceeds maximum {} rules (attempted: {})",
105 max, attempted
106 ),
107 Self::PatternTooLong { max, length } => write!(
108 f,
109 "Resource pattern exceeds maximum {} characters (length: {})",
110 max, length
111 ),
112 Self::NameTooLong { max, length } => write!(
113 f,
114 "Policy name exceeds maximum {} characters (length: {})",
115 max, length
116 ),
117 Self::ExpressionTooDeep { max } => write!(
118 f,
119 "Context expression exceeds maximum depth of {} (prevents stack overflow)",
120 max
121 ),
122 Self::ExpressionTooLong { max, length } => write!(
123 f,
124 "Context expression exceeds maximum {} characters (length: {})",
125 max, length
126 ),
127 Self::InvalidExpression(msg) => write!(f, "Invalid context expression: {}", msg),
128 Self::InternalError(msg) => write!(f, "Internal error: {}", msg),
129 }
130 }
131}
132
133#[cfg(feature = "toml")]
134impl From<toml::de::Error> for PolicyError {
135 fn from(err: toml::de::Error) -> Self {
136 Self::TomlError(err)
137 }
138}
139
140impl core::error::Error for PolicyError {}