use std::fmt;
pub type Result<T> = std::result::Result<T, EdgeError>;
#[derive(Debug, thiserror::Error)]
pub enum EdgeError {
#[error("I/O error: {0}")]
Io(#[from] std::io::Error),
#[error("Serialization error: {0}")]
Serialization(String),
#[error("Deserialization error: {0}")]
Deserialization(String),
#[error("Compression error: {0}")]
Compression(String),
#[error("Decompression error: {0}")]
Decompression(String),
#[error("Cache error: {0}")]
Cache(String),
#[error("Sync error: {0}")]
Sync(String),
#[error("Conflict resolution error: {0}")]
Conflict(String),
#[error("Resource constraint violated: {0}")]
ResourceConstraint(String),
#[error("Runtime error: {0}")]
Runtime(String),
#[error("Invalid configuration: {0}")]
InvalidConfig(String),
#[error("Network error: {0}")]
Network(String),
#[error("Storage error: {0}")]
Storage(String),
#[error("Operation not supported: {0}")]
NotSupported(String),
#[error("Operation timed out: {0}")]
Timeout(String),
#[error("{0}")]
Other(String),
}
impl EdgeError {
pub fn serialization<S: fmt::Display>(msg: S) -> Self {
Self::Serialization(msg.to_string())
}
pub fn deserialization<S: fmt::Display>(msg: S) -> Self {
Self::Deserialization(msg.to_string())
}
pub fn compression<S: fmt::Display>(msg: S) -> Self {
Self::Compression(msg.to_string())
}
pub fn decompression<S: fmt::Display>(msg: S) -> Self {
Self::Decompression(msg.to_string())
}
pub fn cache<S: fmt::Display>(msg: S) -> Self {
Self::Cache(msg.to_string())
}
pub fn sync<S: fmt::Display>(msg: S) -> Self {
Self::Sync(msg.to_string())
}
pub fn conflict<S: fmt::Display>(msg: S) -> Self {
Self::Conflict(msg.to_string())
}
pub fn resource_constraint<S: fmt::Display>(msg: S) -> Self {
Self::ResourceConstraint(msg.to_string())
}
pub fn runtime<S: fmt::Display>(msg: S) -> Self {
Self::Runtime(msg.to_string())
}
pub fn invalid_config<S: fmt::Display>(msg: S) -> Self {
Self::InvalidConfig(msg.to_string())
}
pub fn network<S: fmt::Display>(msg: S) -> Self {
Self::Network(msg.to_string())
}
pub fn storage<S: fmt::Display>(msg: S) -> Self {
Self::Storage(msg.to_string())
}
pub fn not_supported<S: fmt::Display>(msg: S) -> Self {
Self::NotSupported(msg.to_string())
}
pub fn timeout<S: fmt::Display>(msg: S) -> Self {
Self::Timeout(msg.to_string())
}
pub fn other<S: fmt::Display>(msg: S) -> Self {
Self::Other(msg.to_string())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_error_creation() {
let err = EdgeError::cache("test error");
assert_eq!(err.to_string(), "Cache error: test error");
let err = EdgeError::sync("sync failed");
assert_eq!(err.to_string(), "Sync error: sync failed");
let err = EdgeError::resource_constraint("memory limit exceeded");
assert_eq!(
err.to_string(),
"Resource constraint violated: memory limit exceeded"
);
}
#[test]
fn test_error_conversion() {
let io_err = std::io::Error::new(std::io::ErrorKind::NotFound, "file not found");
let edge_err: EdgeError = io_err.into();
assert!(matches!(edge_err, EdgeError::Io(_)));
}
}