use thiserror::Error;
pub type Result<T> = std::result::Result<T, FastDFSError>;
#[derive(Error, Debug)]
pub enum FastDFSError {
#[error("Client is closed")]
ClientClosed,
#[error("File not found: {0}")]
FileNotFound(String),
#[error("No storage server available")]
NoStorageServer,
#[error("Connection timeout to {0}")]
ConnectionTimeout(String),
#[error("Network timeout during {0}")]
NetworkTimeout(String),
#[error("Invalid file ID: {0}")]
InvalidFileId(String),
#[error("Invalid response from server: {0}")]
InvalidResponse(String),
#[error("Storage server is offline: {0}")]
StorageServerOffline(String),
#[error("Tracker server is offline: {0}")]
TrackerServerOffline(String),
#[error("Insufficient storage space")]
InsufficientSpace,
#[error("File already exists: {0}")]
FileAlreadyExists(String),
#[error("Invalid metadata: {0}")]
InvalidMetadata(String),
#[error("Operation not supported: {0}")]
OperationNotSupported(String),
#[error("Invalid argument: {0}")]
InvalidArgument(String),
#[error("Protocol error (code {code}): {message}")]
Protocol { code: u8, message: String },
#[error("Network error during {operation} to {addr}: {source}")]
Network {
operation: String,
addr: String,
#[source]
source: std::io::Error,
},
#[error("I/O error: {0}")]
Io(#[from] std::io::Error),
#[error("UTF-8 error: {0}")]
Utf8(#[from] std::string::FromUtf8Error),
}
pub fn map_status_to_error(status: u8) -> Option<FastDFSError> {
match status {
0 => None,
2 => Some(FastDFSError::FileNotFound(String::new())),
6 => Some(FastDFSError::FileAlreadyExists(String::new())),
22 => Some(FastDFSError::InvalidArgument(String::new())),
28 => Some(FastDFSError::InsufficientSpace),
_ => Some(FastDFSError::Protocol {
code: status,
message: format!("Unknown error code: {}", status),
}),
}
}