1#[derive(Debug)]
7pub enum LogError {
8 Io(std::io::Error),
10
11 Format(std::fmt::Error),
13
14 Config { message: String },
16
17 FileOperation { path: String, reason: String },
19
20 InvalidLogLevel { level: String },
22
23 InitializationError { message: String },
25
26 Parse(std::num::ParseIntError),
28
29 Custom { message: String },
31}
32
33impl std::fmt::Display for LogError {
34 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
35 match self {
36 LogError::Io(err) => write!(f, "IO error: {}", err),
37 LogError::Format(err) => write!(f, "Format error: {}", err),
38 LogError::Config { message } => write!(f, "Configuration error: {}", message),
39 LogError::FileOperation { path, reason } => write!(f, "File operation error: {} - {}", path, reason),
40 LogError::InvalidLogLevel { level } => write!(f, "Invalid log level: {}", level),
41 LogError::InitializationError { message } => write!(f, "Initialization error: {}", message),
42 LogError::Parse(e) => write!(f, "Parse error: {}", e),
43 LogError::Custom { message } => write!(f, "Custom error: {}", message),
44 }
45 }
46}
47
48impl std::error::Error for LogError {}
49
50impl From<std::io::Error> for LogError {
52 fn from(err: std::io::Error) -> Self {
53 LogError::Io(err)
54 }
55}
56
57impl From<std::fmt::Error> for LogError {
58 fn from(err: std::fmt::Error) -> Self {
59 LogError::Format(err)
60 }
61}
62
63impl From<std::num::ParseIntError> for LogError {
64 fn from(err: std::num::ParseIntError) -> Self {
65 LogError::Parse(err)
66 }
67}
68
69impl From<String> for LogError {
70 fn from(message: String) -> Self {
71 LogError::Custom { message }
72 }
73}
74
75impl From<&str> for LogError {
76 fn from(message: &str) -> Self {
77 LogError::Custom { message: message.to_string() }
78 }
79}
80
81pub type LogResult<T> = Result<T, LogError>;
83
84impl LogError {
86 pub fn config<S: Into<String>>(message: S) -> Self {
88 LogError::Config {
89 message: message.into(),
90 }
91 }
92
93 pub fn file_operation<P: Into<String>, R: Into<String>>(path: P, reason: R) -> Self {
95 LogError::FileOperation {
96 path: path.into(),
97 reason: reason.into(),
98 }
99 }
100
101 pub fn invalid_log_level<S: Into<String>>(level: S) -> Self {
103 LogError::InvalidLogLevel {
104 level: level.into(),
105 }
106 }
107
108 pub fn initialization_error<S: Into<String>>(message: S) -> Self {
110 LogError::InitializationError {
111 message: message.into(),
112 }
113 }
114
115 pub fn custom<S: Into<String>>(message: S) -> Self {
117 LogError::Custom {
118 message: message.into(),
119 }
120 }
121
122
123}