1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
/*******************************************************************************
*
* Copyright (c) 2026 Haixing Hu.
*
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0.
*
******************************************************************************/
//! Error type used by MIME database parsing and detection.
//!
use thiserror::Error;
use crate::ProviderRegistryError;
/// Error type for MIME repository parsing and I/O backed detection.
#[derive(Debug, Error)]
pub enum MimeError {
/// A glob weight was outside the freedesktop MIME range `0..=100`.
#[error("invalid MIME glob weight: {weight}")]
InvalidGlobWeight {
/// Invalid glob weight.
weight: u16,
},
/// A magic matcher definition is internally inconsistent.
#[error("invalid MIME magic matcher: {reason}")]
InvalidMagicMatcher {
/// Human-readable validation failure.
reason: String,
},
/// An XML attribute is missing or malformed.
#[error("invalid XML attribute '{attribute}' on <{element}>: '{value}' ({reason})")]
InvalidXmlAttribute {
/// Element carrying the invalid attribute.
element: String,
/// Invalid attribute name.
attribute: String,
/// Invalid attribute value.
value: String,
/// Human-readable validation failure.
reason: String,
},
/// An XML element is missing required content or has invalid children.
#[error("invalid XML element <{element}>: {reason}")]
InvalidXmlElement {
/// Invalid element name.
element: String,
/// Human-readable validation failure.
reason: String,
},
/// A detector or classifier input cannot be processed.
#[error("invalid MIME classifier input: {reason}")]
InvalidClassifierInput {
/// Human-readable validation failure.
reason: String,
},
/// A detector provider name or alias is already registered.
#[error("duplicate MIME detector name or alias: {name}")]
DuplicateDetectorName {
/// Duplicate provider name or alias.
name: String,
},
/// A detector provider name or alias is empty.
#[error("MIME detector name must not be empty")]
EmptyDetectorName,
/// A detector provider name or alias is malformed.
#[error("invalid MIME detector name '{name}': {reason}")]
InvalidDetectorName {
/// Invalid provider name.
name: String,
/// Human-readable validation failure.
reason: String,
},
/// A detector provider could not be found.
#[error("unknown MIME detector: {name}")]
UnknownDetector {
/// Requested provider name or alias.
name: String,
},
/// A detector provider exists but is not available in this environment.
#[error("MIME detector '{name}' is unavailable: {reason}")]
DetectorUnavailable {
/// Requested provider name or alias.
name: String,
/// Human-readable unavailability reason.
reason: String,
},
/// No configured detector provider could be created.
#[error("no available MIME detector: {reason}")]
NoAvailableDetector {
/// Human-readable failure summary.
reason: String,
},
/// A detector backend failed with an implementation-specific error.
#[error("MIME detector backend '{backend}' failed: {reason}")]
DetectorBackend {
/// Backend identifier.
backend: String,
/// Human-readable failure reason.
reason: String,
},
/// A media stream classifier provider name or alias is already registered.
#[error("duplicate media stream classifier name or alias: {name}")]
DuplicateClassifierName {
/// Duplicate provider name or alias.
name: String,
},
/// A media stream classifier provider name or alias is empty.
#[error("media stream classifier name must not be empty")]
EmptyClassifierName,
/// A media stream classifier provider name or alias is malformed.
#[error("invalid media stream classifier name '{name}': {reason}")]
InvalidClassifierName {
/// Invalid provider name.
name: String,
/// Human-readable validation failure.
reason: String,
},
/// A media stream classifier provider could not be found.
#[error("unknown media stream classifier: {name}")]
UnknownClassifier {
/// Requested provider name or alias.
name: String,
},
/// A media stream classifier provider exists but is not available in this environment.
#[error("media stream classifier '{name}' is unavailable: {reason}")]
ClassifierUnavailable {
/// Requested provider name or alias.
name: String,
/// Human-readable unavailability reason.
reason: String,
},
/// No configured media stream classifier provider could be created.
#[error("no available media stream classifier: {reason}")]
NoAvailableClassifier {
/// Human-readable failure summary.
reason: String,
},
/// A media stream classifier backend failed with an implementation-specific error.
#[error("media stream classifier backend '{backend}' failed: {reason}")]
ClassifierBackend {
/// Backend identifier.
backend: String,
/// Human-readable failure reason.
reason: String,
},
/// The XML document could not be parsed.
#[error("failed to parse MIME XML: {0}")]
Xml(#[from] roxmltree::Error),
/// Detection from a path or reader failed due to I/O.
#[error("I/O error while detecting MIME type: {0}")]
Io(#[from] std::io::Error),
/// Detection using an external command failed.
#[error("command error while detecting MIME type: {0}")]
Command(#[from] qubit_command::CommandError),
/// Loading MIME configuration failed.
#[error("configuration error while loading MIME settings: {0}")]
Config(#[from] qubit_config::ConfigError),
}
impl From<ProviderRegistryError> for MimeError {
/// Converts a generic SPI registry error into a MIME-domain error.
fn from(error: ProviderRegistryError) -> Self {
match error {
ProviderRegistryError::EmptyProviderName => Self::EmptyDetectorName,
ProviderRegistryError::InvalidProviderName { name, reason } => {
Self::InvalidDetectorName { name, reason }
}
ProviderRegistryError::DuplicateProviderName { name } => Self::DuplicateDetectorName {
name: name.as_str().to_owned(),
},
ProviderRegistryError::UnknownProvider { name } => Self::UnknownDetector {
name: name.as_str().to_owned(),
},
ProviderRegistryError::ProviderUnavailable { name, source } => {
Self::DetectorUnavailable {
name: name.as_str().to_owned(),
reason: source.reason().to_owned(),
}
}
ProviderRegistryError::ProviderCreate { name, source } => Self::DetectorBackend {
backend: name.as_str().to_owned(),
reason: source.reason().to_owned(),
},
ProviderRegistryError::NoAvailableProvider { failures } => Self::NoAvailableDetector {
reason: failures
.iter()
.map(ToString::to_string)
.collect::<Vec<_>>()
.join("; "),
},
ProviderRegistryError::EmptyRegistry => Self::NoAvailableDetector {
reason: "detector registry is empty".to_owned(),
},
}
}
}
impl MimeError {
/// Builds an invalid XML attribute error.
///
/// # Parameters
/// - `element`: Element carrying the attribute.
/// - `attribute`: Attribute name.
/// - `value`: Attribute value.
/// - `reason`: Why the value is invalid.
///
/// # Returns
/// A [`MimeError::InvalidXmlAttribute`](crate::MimeError::InvalidXmlAttribute) value.
pub(crate) fn invalid_attr(
element: &str,
attribute: &str,
value: &str,
reason: impl Into<String>,
) -> Self {
Self::InvalidXmlAttribute {
element: element.to_owned(),
attribute: attribute.to_owned(),
value: value.to_owned(),
reason: reason.into(),
}
}
/// Builds an invalid XML element error.
///
/// # Parameters
/// - `element`: Invalid element name.
/// - `reason`: Why the element is invalid.
///
/// # Returns
/// A [`MimeError::InvalidXmlElement`](crate::MimeError::InvalidXmlElement) value.
pub(crate) fn invalid_element(element: &str, reason: impl Into<String>) -> Self {
Self::InvalidXmlElement {
element: element.to_owned(),
reason: reason.into(),
}
}
/// Builds an invalid magic matcher error.
///
/// # Parameters
/// - `reason`: Why the matcher is invalid.
///
/// # Returns
/// A [`MimeError::InvalidMagicMatcher`](crate::MimeError::InvalidMagicMatcher) value.
pub(crate) fn invalid_matcher(reason: impl Into<String>) -> Self {
Self::InvalidMagicMatcher {
reason: reason.into(),
}
}
/// Builds an invalid classifier input error.
///
/// # Parameters
/// - `reason`: Why the input cannot be classified.
///
/// # Returns
/// A [`MimeError::InvalidClassifierInput`](crate::MimeError::InvalidClassifierInput) value.
pub(crate) fn invalid_classifier_input(reason: impl Into<String>) -> Self {
Self::InvalidClassifierInput {
reason: reason.into(),
}
}
/// Builds a detector backend error.
///
/// # Parameters
/// - `backend`: Detector backend identifier.
/// - `reason`: Why the backend failed.
///
/// # Returns
/// A [`MimeError::DetectorBackend`](crate::MimeError::DetectorBackend) value.
pub fn detector_backend(backend: impl Into<String>, reason: impl Into<String>) -> Self {
Self::DetectorBackend {
backend: backend.into(),
reason: reason.into(),
}
}
/// Converts a generic SPI registry error into a classifier-domain error.
///
/// # Parameters
/// - `error`: Provider registry error returned by `qubit-spi`.
///
/// # Returns
/// Classifier-specific MIME error.
pub(crate) fn classifier_registry_error(error: ProviderRegistryError) -> Self {
match error {
ProviderRegistryError::EmptyProviderName => Self::EmptyClassifierName,
ProviderRegistryError::InvalidProviderName { name, reason } => {
Self::InvalidClassifierName { name, reason }
}
ProviderRegistryError::DuplicateProviderName { name } => {
Self::DuplicateClassifierName {
name: name.as_str().to_owned(),
}
}
ProviderRegistryError::UnknownProvider { name } => Self::UnknownClassifier {
name: name.as_str().to_owned(),
},
ProviderRegistryError::ProviderUnavailable { name, source } => {
Self::ClassifierUnavailable {
name: name.as_str().to_owned(),
reason: source.reason().to_owned(),
}
}
ProviderRegistryError::ProviderCreate { name, source } => Self::ClassifierBackend {
backend: name.as_str().to_owned(),
reason: source.reason().to_owned(),
},
ProviderRegistryError::NoAvailableProvider { failures } => {
Self::NoAvailableClassifier {
reason: failures
.iter()
.map(ToString::to_string)
.collect::<Vec<_>>()
.join("; "),
}
}
ProviderRegistryError::EmptyRegistry => Self::NoAvailableClassifier {
reason: "classifier registry is empty".to_owned(),
},
}
}
}