use thiserror::Error;
pub type Result<T> = std::result::Result<T, ContextError>;
pub type ContextResult<T> = std::result::Result<T, ContextError>;
#[derive(Error, Debug)]
pub enum ContextError {
#[error("Context not found: {0}")]
NotFound(String),
#[error("Storage error: {0}")]
Storage(String),
#[error("Serialization error: {0}")]
Serialization(#[from] serde_json::Error),
#[error("Invalid query: {0}")]
InvalidQuery(String),
#[error("Context has expired: {0}")]
Expired(String),
#[error("Security screening failed: {0}")]
ScreeningFailed(String),
#[error("Context blocked: {0}")]
Blocked(String),
#[error("IO error: {0}")]
Io(#[from] std::io::Error),
#[error("Operation timed out: {0}")]
Timeout(String),
#[error("Configuration error: {0}")]
Config(String),
#[error("Protocol error: {0}")]
Protocol(String),
#[error("Internal error: {0}")]
Internal(String),
}
impl ContextError {
pub fn is_not_found(&self) -> bool {
matches!(self, Self::NotFound(_))
}
pub fn is_security_error(&self) -> bool {
matches!(self, Self::ScreeningFailed(_) | Self::Blocked(_))
}
}
#[cfg(feature = "persistence")]
impl From<sled::Error> for ContextError {
fn from(err: sled::Error) -> Self {
Self::Storage(err.to_string())
}
}