openmassspec_core/error.rs
1//! Aggregate error type for the OpenMassSpec stack.
2//!
3//! Each vendor crate defines its own narrow `Error` enum. Code that handles
4//! errors from several vendors uniformly (the umbrella `openmassspec-io`
5//! crate, the `vendor2mzml` CLI, the mzML writer in this crate) works with
6//! [`Error`]: a [`SpectrumSource`](crate::SpectrumSource) yields
7//! `Result<_, Error>` items, and vendor errors travel inside
8//! [`Error::Vendor`].
9
10use std::io;
11
12/// Stack-wide error type.
13///
14/// Vendor errors are erased into a boxed trait object so this enum does not
15/// change as vendor crates evolve. The enum is `#[non_exhaustive]`; match it
16/// with a wildcard arm.
17#[derive(Debug, thiserror::Error)]
18#[non_exhaustive]
19pub enum Error {
20 /// I/O failure (file not found, permission denied, short read, ...).
21 #[error("I/O error: {0}")]
22 Io(#[from] io::Error),
23
24 /// Vendor parser failed. The inner error is the vendor crate's own error
25 /// type, preserved via `Box<dyn std::error::Error>` so callers can
26 /// downcast if they need vendor-specific context.
27 #[error("vendor parser error: {0}")]
28 Vendor(Box<dyn std::error::Error + Send + Sync + 'static>),
29
30 /// Format detection failed or the input does not match a supported vendor.
31 #[error("unsupported or unrecognized format: {0}")]
32 Format(String),
33
34 /// Conformance harness rejected a record stream.
35 #[error("conformance violation: {0}")]
36 Conformance(String),
37}
38
39/// Convenience alias.
40pub type Result<T> = std::result::Result<T, Error>;
41
42impl Error {
43 /// Wrap a vendor-crate error into [`Error::Vendor`].
44 ///
45 /// Usable directly as a `map_err` argument:
46 /// `decode(scan).map_err(Error::vendor)`.
47 pub fn vendor<E>(err: E) -> Self
48 where
49 E: std::error::Error + Send + Sync + 'static,
50 {
51 Error::Vendor(Box::new(err))
52 }
53}