Skip to main content

mib_rs/
error.rs

1//! Error types for the MIB loading pipeline.
2//!
3//! The primary error type is [`LoadError`], returned by [`Loader::load`](crate::Loader::load)
4//! and the free function [`load`](crate::load::load).
5
6/// Errors returned by [`Loader::load`](crate::Loader::load) and the
7/// free function [`load`](crate::load::load).
8///
9/// All variants carry enough context for callers to present useful error
10/// messages. The [`Display`](std::fmt::Display) implementation produces
11/// human-readable text for each case.
12#[derive(Debug, thiserror::Error)]
13pub enum LoadError {
14    /// No [`Source`](crate::Source)s were configured on the [`Loader`](crate::Loader).
15    #[error("no MIB sources provided")]
16    NoSources,
17
18    /// One or more explicitly requested modules were not found after resolution.
19    ///
20    /// The contained `Vec` lists the missing module names.
21    #[error("requested modules not found: {}", .0.join(", "))]
22    MissingModules(Vec<String>),
23
24    /// A diagnostic exceeded the configured fail-at severity threshold.
25    ///
26    /// See [`DiagnosticConfig`](crate::DiagnosticConfig) for threshold configuration.
27    #[error("diagnostic threshold exceeded")]
28    DiagnosticThreshold,
29
30    /// A [`Source`](crate::Source) implementation returned a custom error.
31    ///
32    /// Use [`LoadError::from_source`] to construct this variant from an
33    /// arbitrary error type.
34    #[error("source error")]
35    Source(#[source] Box<dyn std::error::Error + Send + Sync>),
36
37    /// An I/O error occurred while reading MIB files from disk.
38    #[error("I/O error")]
39    Io(#[from] std::io::Error),
40}
41
42impl LoadError {
43    /// Wrap an arbitrary error as a [`LoadError::Source`].
44    ///
45    /// Useful for custom [`Source`](crate::Source) implementations that
46    /// need to return domain-specific errors through the loading pipeline.
47    pub fn from_source(err: impl std::error::Error + Send + Sync + 'static) -> Self {
48        LoadError::Source(Box::new(err))
49    }
50}