use std::fmt;
pub type Result<T> = std::result::Result<T, ChunkingError>;
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ChunkingError {
InvalidConfig(String),
InvalidInput(String),
ProcessingError(String),
IoError(String),
}
impl ChunkingError {
pub fn invalid_config<S: Into<String>>(msg: S) -> Self {
ChunkingError::InvalidConfig(msg.into())
}
pub fn invalid_input<S: Into<String>>(msg: S) -> Self {
ChunkingError::InvalidInput(msg.into())
}
pub fn processing_error<S: Into<String>>(msg: S) -> Self {
ChunkingError::ProcessingError(msg.into())
}
pub fn io_error<S: Into<String>>(msg: S) -> Self {
ChunkingError::IoError(msg.into())
}
}
impl fmt::Display for ChunkingError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
ChunkingError::InvalidConfig(msg) => write!(f, "Invalid configuration: {}", msg),
ChunkingError::InvalidInput(msg) => write!(f, "Invalid input: {}", msg),
ChunkingError::ProcessingError(msg) => write!(f, "Processing error: {}", msg),
ChunkingError::IoError(msg) => write!(f, "I/O error: {}", msg),
}
}
}
impl std::error::Error for ChunkingError {}
impl From<std::io::Error> for ChunkingError {
fn from(err: std::io::Error) -> Self {
ChunkingError::IoError(err.to_string())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_error_display() {
let err = ChunkingError::invalid_config("test message");
assert_eq!(err.to_string(), "Invalid configuration: test message");
}
#[test]
fn test_error_from_io() {
let io_err = std::io::Error::new(std::io::ErrorKind::NotFound, "file not found");
let chunk_err: ChunkingError = io_err.into();
match chunk_err {
ChunkingError::IoError(_) => (),
_ => panic!("Expected IoError"),
}
}
}