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("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(),
}
}
}
#[cfg(coverage)]
pub(crate) mod coverage_support {
use super::MimeError;
pub(crate) fn exercise_error_builders() -> Vec<String> {
let command_error = qubit_command::CommandError::SpawnFailed {
command: "file --mime-type --brief missing".to_owned(),
source: std::io::Error::new(std::io::ErrorKind::NotFound, "missing"),
};
let config_error = qubit_config::ConfigError::Other("bad config".to_owned());
vec![
MimeError::invalid_attr("match", "value", "bad", "invalid").to_string(),
MimeError::invalid_element("magic", "missing match").to_string(),
MimeError::invalid_matcher("bad matcher").to_string(),
MimeError::invalid_classifier_input("bad input").to_string(),
MimeError::Command(command_error).to_string(),
MimeError::Config(config_error).to_string(),
]
}
}