use thiserror::Error;
#[derive(Debug, Error)]
pub enum MimeError {
#[error("invalid MIME glob weight: {weight}")]
InvalidGlobWeight {
weight: u16,
},
#[error("invalid MIME magic matcher: {reason}")]
InvalidMagicMatcher {
reason: String,
},
#[error("invalid XML attribute '{attribute}' on <{element}>: '{value}' ({reason})")]
InvalidXmlAttribute {
element: String,
attribute: String,
value: String,
reason: String,
},
#[error("invalid XML element <{element}>: {reason}")]
InvalidXmlElement {
element: String,
reason: String,
},
#[error("invalid MIME classifier input: {reason}")]
InvalidClassifierInput {
reason: String,
},
#[error("duplicate MIME detector name or alias: {name}")]
DuplicateDetectorName {
name: String,
},
#[error("unknown MIME detector: {name}")]
UnknownDetector {
name: String,
},
#[error("MIME detector '{name}' is unavailable: {reason}")]
DetectorUnavailable {
name: String,
reason: String,
},
#[error("no available MIME detector: {reason}")]
NoAvailableDetector {
reason: String,
},
#[error("MIME detector backend '{backend}' failed: {reason}")]
DetectorBackend {
backend: String,
reason: String,
},
#[error("failed to parse MIME XML: {0}")]
Xml(#[from] roxmltree::Error),
#[error("I/O error while detecting MIME type: {0}")]
Io(#[from] std::io::Error),
#[error("command error while detecting MIME type: {0}")]
Command(#[from] qubit_command::CommandError),
#[error("configuration error while loading MIME settings: {0}")]
Config(#[from] qubit_config::ConfigError),
}
impl MimeError {
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(),
}
}
pub(crate) fn invalid_element(element: &str, reason: impl Into<String>) -> Self {
Self::InvalidXmlElement {
element: element.to_owned(),
reason: reason.into(),
}
}
pub(crate) fn invalid_matcher(reason: impl Into<String>) -> Self {
Self::InvalidMagicMatcher {
reason: reason.into(),
}
}
pub(crate) fn invalid_classifier_input(reason: impl Into<String>) -> Self {
Self::InvalidClassifierInput {
reason: reason.into(),
}
}
pub fn detector_backend(backend: impl Into<String>, reason: impl Into<String>) -> Self {
Self::DetectorBackend {
backend: backend.into(),
reason: reason.into(),
}
}
}