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 TomlError(toml::de::Error),
34
35 TooManyRules {
42 max: usize,
44 attempted: usize,
46 },
47
48 PatternTooLong {
50 max: usize,
52 length: usize,
54 },
55
56 NameTooLong {
58 max: usize,
60 length: usize,
62 },
63
64 ExpressionTooDeep {
67 max: usize,
69 },
70
71 ExpressionTooLong {
73 max: usize,
75 length: usize,
77 },
78
79 InvalidExpression(String),
81
82 InternalError(String),
87}
88
89impl fmt::Display for PolicyError {
90 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
91 match self {
92 Self::PolicyNotFound(msg) => write!(f, "Policy not found: {}", msg),
93 Self::InvalidRule(msg) => write!(f, "Invalid policy rule: {}", msg),
94 Self::PermissionDenied { peer_id, reason } => {
95 write!(f, "Permission denied for peer {}: {}", peer_id, reason)
96 }
97 Self::InvalidPeerId(msg) => write!(f, "Invalid peer ID: {}", msg),
98 Self::SerializationError(msg) => write!(f, "Serialization error: {}", msg),
99 Self::TomlError(e) => write!(f, "TOML parsing error: {}", e),
100 Self::TooManyRules { max, attempted } => write!(
101 f,
102 "Policy exceeds maximum {} rules (attempted: {})",
103 max, attempted
104 ),
105 Self::PatternTooLong { max, length } => write!(
106 f,
107 "Resource pattern exceeds maximum {} characters (length: {})",
108 max, length
109 ),
110 Self::NameTooLong { max, length } => write!(
111 f,
112 "Policy name exceeds maximum {} characters (length: {})",
113 max, length
114 ),
115 Self::ExpressionTooDeep { max } => write!(
116 f,
117 "Context expression exceeds maximum depth of {} (prevents stack overflow)",
118 max
119 ),
120 Self::ExpressionTooLong { max, length } => write!(
121 f,
122 "Context expression exceeds maximum {} characters (length: {})",
123 max, length
124 ),
125 Self::InvalidExpression(msg) => write!(f, "Invalid context expression: {}", msg),
126 Self::InternalError(msg) => write!(f, "Internal error: {}", msg),
127 }
128 }
129}
130
131impl From<toml::de::Error> for PolicyError {
132 fn from(err: toml::de::Error) -> Self {
133 Self::TomlError(err)
134 }
135}
136
137impl core::error::Error for PolicyError {}