1use std::fmt;
4
5#[derive(Debug, Clone)]
7pub enum FormatError {
8 InvalidIpAddress(String),
10 InvalidPattern(String),
12 IpTreeError(String),
14 PatternError(String),
16 LiteralHashError(String),
18 IoError(String),
20 ValidationError(String),
22 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
43impl 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
80impl 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}