//! The error type shared by the `no_std` wire-format crates.

use core::fmt;

/// Errors produced while parsing or constructing wire-format primitives.
///
/// Kept deliberately small and `no_std`-friendly. Higher layers (`plexus-codec`,
/// `plexus-gateway`) wrap this in their own richer error types.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum Error {
    /// A buffer was too short to contain the expected structure.
    Truncated,
    /// A field held a value outside its defined domain.
    InvalidValue,
    /// A declared length disagreed with the actual data.
    LengthMismatch,
}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let msg = match self {
            Self::Truncated => "buffer truncated",
            Self::InvalidValue => "invalid field value",
            Self::LengthMismatch => "declared length mismatch",
        };
        f.write_str(msg)
    }
}

// `core::error::Error` is stable from Rust 1.81; the workspace MSRV is 1.75, so
// the trait impl is gated behind the `std` feature for now.
#[cfg(feature = "std")]
impl std::error::Error for Error {}
