use std::fmt;
#[derive(Debug, Clone)]
pub enum FormatError {
InvalidIpAddress(String),
InvalidPattern(String),
IpTreeError(String),
PatternError(String),
LiteralHashError(String),
IoError(String),
ValidationError(String),
Other(String),
}
impl fmt::Display for FormatError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::InvalidIpAddress(msg) => write!(f, "Invalid IP address: {msg}"),
Self::InvalidPattern(msg) => write!(f, "Invalid pattern: {msg}"),
Self::IpTreeError(msg) => write!(f, "IP tree error: {msg}"),
Self::PatternError(msg) => write!(f, "Pattern error: {msg}"),
Self::LiteralHashError(msg) => write!(f, "Literal hash error: {msg}"),
Self::IoError(msg) => write!(f, "I/O error: {msg}"),
Self::ValidationError(msg) => write!(f, "Validation error: {msg}"),
Self::Other(msg) => write!(f, "{msg}"),
}
}
}
impl std::error::Error for FormatError {}
impl From<matchy_paraglob::error::ParaglobError> for FormatError {
fn from(err: matchy_paraglob::error::ParaglobError) -> Self {
Self::PatternError(err.to_string())
}
}
impl From<matchy_literal_hash::LiteralHashError> for FormatError {
fn from(err: matchy_literal_hash::LiteralHashError) -> Self {
Self::LiteralHashError(err.to_string())
}
}
impl From<matchy_ip_trie::IpTreeError> for FormatError {
fn from(err: matchy_ip_trie::IpTreeError) -> Self {
Self::IpTreeError(err.to_string())
}
}
impl From<std::io::Error> for FormatError {
fn from(err: std::io::Error) -> Self {
Self::IoError(err.to_string())
}
}
impl From<String> for FormatError {
fn from(s: String) -> Self {
Self::Other(s)
}
}
impl From<&str> for FormatError {
fn from(s: &str) -> Self {
Self::Other(s.to_string())
}
}
impl From<FormatError> for matchy_paraglob::error::ParaglobError {
fn from(err: FormatError) -> Self {
match err {
FormatError::InvalidIpAddress(msg) | FormatError::InvalidPattern(msg) => {
Self::InvalidPattern(msg)
}
FormatError::IoError(msg) => Self::Io(msg),
FormatError::IpTreeError(msg)
| FormatError::PatternError(msg)
| FormatError::LiteralHashError(msg)
| FormatError::ValidationError(msg)
| FormatError::Other(msg) => Self::Other(msg),
}
}
}