matchy_format/
error.rs

1//! Error types for matchy format operations
2
3use std::fmt;
4
5/// Errors that can occur during database format operations
6#[derive(Debug, Clone)]
7pub enum FormatError {
8    /// Invalid IP address or CIDR notation
9    InvalidIpAddress(String),
10    /// Invalid pattern syntax
11    InvalidPattern(String),
12    /// IP tree building error
13    IpTreeError(String),
14    /// Pattern matching error
15    PatternError(String),
16    /// Literal hash error
17    LiteralHashError(String),
18    /// I/O error
19    IoError(String),
20    /// Entry validation error (schema validation failed)
21    ValidationError(String),
22    /// Generic error
23    Other(String),
24}
25
26impl fmt::Display for FormatError {
27    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
28        match self {
29            Self::InvalidIpAddress(msg) => write!(f, "Invalid IP address: {msg}"),
30            Self::InvalidPattern(msg) => write!(f, "Invalid pattern: {msg}"),
31            Self::IpTreeError(msg) => write!(f, "IP tree error: {msg}"),
32            Self::PatternError(msg) => write!(f, "Pattern error: {msg}"),
33            Self::LiteralHashError(msg) => write!(f, "Literal hash error: {msg}"),
34            Self::IoError(msg) => write!(f, "I/O error: {msg}"),
35            Self::ValidationError(msg) => write!(f, "Validation error: {msg}"),
36            Self::Other(msg) => write!(f, "{msg}"),
37        }
38    }
39}
40
41impl std::error::Error for FormatError {}
42
43// Conversions from component errors
44impl From<matchy_paraglob::error::ParaglobError> for FormatError {
45    fn from(err: matchy_paraglob::error::ParaglobError) -> Self {
46        Self::PatternError(err.to_string())
47    }
48}
49
50impl From<matchy_literal_hash::LiteralHashError> for FormatError {
51    fn from(err: matchy_literal_hash::LiteralHashError) -> Self {
52        Self::LiteralHashError(err.to_string())
53    }
54}
55
56impl From<matchy_ip_trie::IpTreeError> for FormatError {
57    fn from(err: matchy_ip_trie::IpTreeError) -> Self {
58        Self::IpTreeError(err.to_string())
59    }
60}
61
62impl From<std::io::Error> for FormatError {
63    fn from(err: std::io::Error) -> Self {
64        Self::IoError(err.to_string())
65    }
66}
67
68impl From<String> for FormatError {
69    fn from(s: String) -> Self {
70        Self::Other(s)
71    }
72}
73
74impl From<&str> for FormatError {
75    fn from(s: &str) -> Self {
76        Self::Other(s.to_string())
77    }
78}
79
80// Conversion: FormatError -> ParaglobError
81// This allows code that uses ParaglobError to also accept FormatError.
82// When matchy-format operations fail, they can be converted into ParaglobError
83// so the caller can handle both types uniformly.
84impl From<FormatError> for matchy_paraglob::error::ParaglobError {
85    fn from(err: FormatError) -> Self {
86        match err {
87            FormatError::InvalidIpAddress(msg) | FormatError::InvalidPattern(msg) => {
88                Self::InvalidPattern(msg)
89            }
90            FormatError::IoError(msg) => Self::Io(msg),
91            FormatError::IpTreeError(msg)
92            | FormatError::PatternError(msg)
93            | FormatError::LiteralHashError(msg)
94            | FormatError::ValidationError(msg)
95            | FormatError::Other(msg) => Self::Other(msg),
96        }
97    }
98}