use std::fmt;
use std::result;
use serde::Deserialize;
use serde::Serialize;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum DriverError {
Validation { field: String, message: String },
MissingParameter { field: String },
InvalidType { field: String, expected: String, actual: String },
InvalidEnumValue { field: String, value: String, allowed: Vec<String> },
Execution { message: String },
DriverNotFound { name: String },
Io { message: String },
Config { message: String },
Internal { message: String },
Context { message: String },
Timeout { duration: Option<String> },
PermissionDenied { resource: String },
Generic { message: String, source: Option<String> },
}
impl fmt::Display for DriverError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Validation { field, message } => {
write!(f, "Validation error for '{}': {}", field, message)?;
}
Self::MissingParameter { field } => {
write!(f, "Required parameter '{}' is missing", field)?;
}
Self::InvalidType { field, expected, actual } => {
write!(f, "Parameter '{}' expects type '{}', got '{}'", field, expected, actual)?;
}
Self::InvalidEnumValue { field, value, allowed } => {
write!(f, "Value '{}' for '{}' not in allowed values: {:?}", value, field, allowed)?;
}
Self::Execution { message } => {
write!(f, "Execution failed: {}", message)?;
}
Self::DriverNotFound { name } => {
write!(f, "Driver '{}' not found", name)?;
}
Self::Io { message } => {
write!(f, "I/O error: {}", message)?;
}
Self::Config { message } => {
write!(f, "Configuration error: {}", message)?;
}
Self::Internal { message } => {
write!(f, "Internal error: {}", message)?;
}
Self::Context { message } => {
write!(f, "Context error: {}", message)?;
}
Self::Timeout { duration } => {
if let Some(d) = duration {
write!(f, "Timeout after {}", d)?;
} else {
write!(f, "Timeout occurred")?;
}
}
Self::PermissionDenied { resource } => {
write!(f, "Permission denied for '{}'", resource)?;
}
Self::Generic { message, source } => {
if let Some(src) = source {
write!(f, "{}: {}", message, src)?;
} else {
write!(f, "{}", message)?;
}
}
}
return Ok(());
}
}
impl std::error::Error for DriverError {}
pub type DriverResult<T> = result::Result<T, DriverError>;
impl DriverError {
pub fn validation(field: impl Into<String>, message: impl Into<String>) -> Self {
return Self::Validation { field: field.into(), message: message.into() };
}
pub fn missing_parameter(field: impl Into<String>) -> Self {
return Self::MissingParameter { field: field.into() };
}
pub fn invalid_type(field: impl Into<String>, expected: impl Into<String>, actual: impl Into<String>) -> Self {
return Self::InvalidType { field: field.into(), expected: expected.into(), actual: actual.into() };
}
pub fn execution(message: impl Into<String>) -> Self {
return Self::Execution { message: message.into() };
}
pub fn not_found(name: impl Into<String>) -> Self {
return Self::DriverNotFound { name: name.into() };
}
pub fn io(message: impl Into<String>) -> Self {
return Self::Io { message: message.into() };
}
pub fn invalid_enum_value(field: impl Into<String>, value: impl Into<String>, allowed: Vec<String>) -> Self {
return Self::InvalidEnumValue { field: field.into(), value: value.into(), allowed };
}
pub fn internal(message: impl Into<String>) -> Self {
return Self::Internal { message: message.into() };
}
pub fn timeout(duration: Option<impl Into<String>>) -> Self {
return Self::Timeout { duration: duration.map(|d| d.into()) };
}
}