use serde::{Deserialize, Serialize};
use std::fmt;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum ProtocolError {
NotFound {
id: String,
},
InvalidState {
action: String,
current_state: String,
},
InvalidInput {
field: String,
message: String,
},
Network {
message: String,
retryable: bool,
},
Storage {
message: String,
},
Shutdown,
Internal {
message: String,
},
}
impl fmt::Display for ProtocolError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::NotFound { id } => write!(f, "Download not found: {}", id),
Self::InvalidState {
action,
current_state,
} => write!(f, "Cannot {} while {}", action, current_state),
Self::InvalidInput { field, message } => {
write!(f, "Invalid input for '{}': {}", field, message)
}
Self::Network { message, .. } => write!(f, "Network error: {}", message),
Self::Storage { message } => write!(f, "Storage error: {}", message),
Self::Shutdown => write!(f, "Engine is shutting down"),
Self::Internal { message } => write!(f, "Internal error: {}", message),
}
}
}
impl std::error::Error for ProtocolError {}
impl ProtocolError {
pub fn is_retryable(&self) -> bool {
match self {
Self::Network { retryable, .. } => *retryable,
_ => false,
}
}
}
pub type ProtocolResult<T> = std::result::Result<T, ProtocolError>;