bytes_radar/core/
error.rs

1use std::io;
2use thiserror::Error;
3
4#[derive(Error, Debug)]
5pub enum AnalysisError {
6    #[error("Failed to read file: {path}")]
7    FileReadError {
8        path: String,
9        #[source]
10        source: io::Error,
11    },
12
13    #[error("Unsupported file extension: {extension}")]
14    UnsupportedExtension { extension: String },
15
16    #[error("Language not found: {language}")]
17    LanguageNotFound { language: String },
18
19    #[error("Invalid file statistics: {reason}")]
20    InvalidStatistics { reason: String },
21
22    #[error("Directory traversal failed: {path}")]
23    DirectoryTraversalError {
24        path: String,
25        #[source]
26        source: io::Error,
27    },
28
29    #[error("Language detection failed for file: {file_path}")]
30    LanguageDetectionError { file_path: String },
31
32    #[error("Configuration error: {message}")]
33    ConfigurationError { message: String },
34
35    #[error("JSON serialization error")]
36    JsonSerializationError {
37        #[from]
38        source: serde_json::Error,
39    },
40
41    #[error("XML serialization error: {message}")]
42    XmlSerializationError { message: String },
43
44    #[error("Aggregation error: {operation}")]
45    AggregationError { operation: String },
46
47    #[error("Network error: {message}")]
48    NetworkError { message: String },
49
50    #[error("Archive processing error: {message}")]
51    ArchiveError { message: String },
52
53    #[error("URL parsing error: {url}")]
54    UrlParsingError { url: String },
55}
56
57pub type Result<T> = std::result::Result<T, AnalysisError>;
58
59impl AnalysisError {
60    pub fn file_read<P: AsRef<str>>(path: P, source: io::Error) -> Self {
61        Self::FileReadError {
62            path: path.as_ref().to_string(),
63            source,
64        }
65    }
66
67    pub fn unsupported_extension<E: AsRef<str>>(extension: E) -> Self {
68        Self::UnsupportedExtension {
69            extension: extension.as_ref().to_string(),
70        }
71    }
72
73    pub fn language_not_found<L: AsRef<str>>(language: L) -> Self {
74        Self::LanguageNotFound {
75            language: language.as_ref().to_string(),
76        }
77    }
78
79    pub fn invalid_statistics<R: AsRef<str>>(reason: R) -> Self {
80        Self::InvalidStatistics {
81            reason: reason.as_ref().to_string(),
82        }
83    }
84
85    pub fn directory_traversal<P: AsRef<str>>(path: P, source: io::Error) -> Self {
86        Self::DirectoryTraversalError {
87            path: path.as_ref().to_string(),
88            source,
89        }
90    }
91
92    pub fn language_detection<P: AsRef<str>>(file_path: P) -> Self {
93        Self::LanguageDetectionError {
94            file_path: file_path.as_ref().to_string(),
95        }
96    }
97
98    pub fn configuration<M: AsRef<str>>(message: M) -> Self {
99        Self::ConfigurationError {
100            message: message.as_ref().to_string(),
101        }
102    }
103
104    pub fn aggregation<O: AsRef<str>>(operation: O) -> Self {
105        Self::AggregationError {
106            operation: operation.as_ref().to_string(),
107        }
108    }
109
110    pub fn network<M: AsRef<str>>(message: M) -> Self {
111        Self::NetworkError {
112            message: message.as_ref().to_string(),
113        }
114    }
115
116    pub fn archive<M: AsRef<str>>(message: M) -> Self {
117        Self::ArchiveError {
118            message: message.as_ref().to_string(),
119        }
120    }
121
122    pub fn url_parsing<U: AsRef<str>>(url: U) -> Self {
123        Self::UrlParsingError {
124            url: url.as_ref().to_string(),
125        }
126    }
127
128    pub fn xml_serialization<M: AsRef<str>>(message: M) -> Self {
129        Self::XmlSerializationError {
130            message: message.as_ref().to_string(),
131        }
132    }
133}