use alloc::string::String;
use core::fmt;
pub type Result<T> = core::result::Result<T, PolicyError>;
#[derive(Debug)]
pub enum PolicyError {
PolicyNotFound(String),
InvalidRule(String),
PermissionDenied {
peer_id: String,
reason: String,
},
InvalidPeerId(String),
SerializationError(String),
#[cfg(feature = "toml")]
TomlError(toml::de::Error),
TooManyRules {
max: usize,
attempted: usize,
},
PatternTooLong {
max: usize,
length: usize,
},
NameTooLong {
max: usize,
length: usize,
},
ExpressionTooDeep {
max: usize,
},
ExpressionTooLong {
max: usize,
length: usize,
},
InvalidExpression(String),
InternalError(String),
}
impl fmt::Display for PolicyError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::PolicyNotFound(msg) => write!(f, "Policy not found: {}", msg),
Self::InvalidRule(msg) => write!(f, "Invalid policy rule: {}", msg),
Self::PermissionDenied { peer_id, reason } => {
write!(f, "Permission denied for peer {}: {}", peer_id, reason)
}
Self::InvalidPeerId(msg) => write!(f, "Invalid peer ID: {}", msg),
Self::SerializationError(msg) => write!(f, "Serialization error: {}", msg),
#[cfg(feature = "toml")]
Self::TomlError(e) => write!(f, "TOML parsing error: {}", e),
Self::TooManyRules { max, attempted } => write!(
f,
"Policy exceeds maximum {} rules (attempted: {})",
max, attempted
),
Self::PatternTooLong { max, length } => write!(
f,
"Resource pattern exceeds maximum {} characters (length: {})",
max, length
),
Self::NameTooLong { max, length } => write!(
f,
"Policy name exceeds maximum {} characters (length: {})",
max, length
),
Self::ExpressionTooDeep { max } => write!(
f,
"Context expression exceeds maximum depth of {} (prevents stack overflow)",
max
),
Self::ExpressionTooLong { max, length } => write!(
f,
"Context expression exceeds maximum {} characters (length: {})",
max, length
),
Self::InvalidExpression(msg) => write!(f, "Invalid context expression: {}", msg),
Self::InternalError(msg) => write!(f, "Internal error: {}", msg),
}
}
}
#[cfg(feature = "toml")]
impl From<toml::de::Error> for PolicyError {
fn from(err: toml::de::Error) -> Self {
Self::TomlError(err)
}
}
impl core::error::Error for PolicyError {}