use std::path::PathBuf;
pub type BoxError = Box<dyn std::error::Error + Send + Sync + 'static>;
#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
pub enum Error {
#[error("unsupported vendor format at {0}")]
UnsupportedFormat(PathBuf),
#[error("openmassspec-io was built without the '{vendor}' feature")]
FeatureDisabled {
vendor: &'static str,
},
#[error("{vendor} reader: {source}")]
#[non_exhaustive]
Reader {
vendor: &'static str,
#[source]
source: BoxError,
},
#[error(transparent)]
Io(#[from] std::io::Error),
#[error(transparent)]
Core(openmassspec_core::Error),
#[error("mzML error: {0}")]
Mzml(String),
#[error("streaming was cancelled")]
Cancelled,
}
impl Error {
pub fn reader<E>(vendor: &'static str, err: E) -> Self
where
E: std::error::Error + Send + Sync + 'static,
{
Error::Reader {
vendor,
source: Box::new(err),
}
}
pub fn reader_error(&self) -> Option<&(dyn std::error::Error + Send + Sync + 'static)> {
match self {
Error::Reader { source, .. } => Some(source.as_ref()),
Error::Core(openmassspec_core::Error::Vendor(source)) => Some(source.as_ref()),
_ => None,
}
}
pub fn reader_vendor(&self) -> Option<&'static str> {
match self {
Error::Reader { vendor, .. } => Some(vendor),
_ => None,
}
}
pub(crate) fn attribute_to(self, vendor: &'static str) -> Self {
match self {
Error::Core(openmassspec_core::Error::Vendor(source)) => {
Error::Reader { vendor, source }
}
other => other,
}
}
}
impl From<openmassspec_core::Error> for Error {
fn from(e: openmassspec_core::Error) -> Self {
Error::Core(e)
}
}
pub type Result<T> = std::result::Result<T, Error>;