openmassspec-core 3.0.0

Shared types, traits, and mzML writer for open Rust mass-spec parsers.
Documentation
//! Aggregate error type for the OpenMassSpec stack.
//!
//! Each vendor crate defines its own narrow `Error` enum. Code that handles
//! errors from several vendors uniformly (the umbrella `openmassspec-io`
//! crate, the `vendor2mzml` CLI, the mzML writer in this crate) works with
//! [`Error`]: a [`SpectrumSource`](crate::SpectrumSource) yields
//! `Result<_, Error>` items, and vendor errors travel inside
//! [`Error::Vendor`].

use std::io;

/// Stack-wide error type.
///
/// Vendor errors are erased into a boxed trait object so this enum does not
/// change as vendor crates evolve. The enum is `#[non_exhaustive]`; match it
/// with a wildcard arm.
#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
pub enum Error {
    /// I/O failure (file not found, permission denied, short read, ...).
    #[error("I/O error: {0}")]
    Io(#[from] io::Error),

    /// Vendor parser failed. The inner error is the vendor crate's own error
    /// type, preserved via `Box<dyn std::error::Error>` so callers can
    /// downcast if they need vendor-specific context.
    #[error("vendor parser error: {0}")]
    Vendor(Box<dyn std::error::Error + Send + Sync + 'static>),

    /// Format detection failed or the input does not match a supported vendor.
    #[error("unsupported or unrecognized format: {0}")]
    Format(String),

    /// Conformance harness rejected a record stream.
    #[error("conformance violation: {0}")]
    Conformance(String),
}

/// Convenience alias.
pub type Result<T> = std::result::Result<T, Error>;

impl Error {
    /// Wrap a vendor-crate error into [`Error::Vendor`].
    ///
    /// Usable directly as a `map_err` argument:
    /// `decode(scan).map_err(Error::vendor)`.
    pub fn vendor<E>(err: E) -> Self
    where
        E: std::error::Error + Send + Sync + 'static,
    {
        Error::Vendor(Box::new(err))
    }
}