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
6use crate::types::DiagnosticReport;
7
8/// Errors returned by [`Loader::load`](crate::Loader::load) and the
9/// free function [`load`](crate::load::load).
10///
11/// All variants carry enough context for callers to present useful error
12/// messages. The [`Display`](std::fmt::Display) implementation produces
13/// human-readable text for each case.
14#[derive(Debug, thiserror::Error)]
15pub enum LoadError {
16    /// No [`Source`](crate::Source)s were configured on the [`Loader`](crate::Loader).
17    #[error("no MIB sources provided")]
18    NoSources,
19
20    /// One or more explicitly requested modules were not found after resolution.
21    ///
22    /// The contained `Vec` lists the missing module names.
23    #[error("requested modules not found: {}", .0.join(", "))]
24    MissingModules(Vec<String>),
25
26    /// A diagnostic exceeded the configured fail-at severity threshold.
27    ///
28    /// `report` contains every diagnostic collected during the load, not
29    /// only those at or above the failure threshold. The diagnostics are
30    /// ordered by pipeline phase, code, effective severity, module, stable
31    /// source identity and label, half-open source range, and message. The
32    /// report retains every source document needed to derive locations after
33    /// the failed MIB has been dropped.
34    ///
35    /// See [`DiagnosticConfig`](crate::DiagnosticConfig) for threshold configuration.
36    #[error("diagnostic threshold exceeded")]
37    DiagnosticThreshold {
38        /// All diagnostics and their retained sources from the failed load.
39        report: DiagnosticReport,
40    },
41
42    /// A [`Source`](crate::Source) implementation returned a custom error.
43    ///
44    /// Use [`LoadError::from_source`] to construct this variant from an
45    /// arbitrary error type.
46    #[error("source error")]
47    Source(#[source] Box<dyn std::error::Error + Send + Sync>),
48
49    /// An I/O error occurred while reading MIB files from disk.
50    #[error("I/O error")]
51    Io(#[from] std::io::Error),
52}
53
54impl LoadError {
55    /// Wrap an arbitrary error as a [`LoadError::Source`].
56    ///
57    /// Useful for custom [`Source`](crate::Source) implementations that
58    /// need to return domain-specific errors through the loading pipeline.
59    pub fn from_source(err: impl std::error::Error + Send + Sync + 'static) -> Self {
60        LoadError::Source(Box::new(err))
61    }
62}