ndn-tlv 0.4.1

Encoding and Decoding of TLV values for Named Data Networking
Documentation
use thiserror::Error;

/// Common error enum for library functions
#[derive(Debug, Error)]
pub enum TlvError {
    /// A TLV that was being read had an unexpected type
    #[error("TLV read had different type {found}, expected {expected}")]
    TypeMismatch {
        /// The expected type
        expected: usize,
        /// The actual type read
        found: usize,
    },
    /// The data stream ended, even though more data was expected
    #[error("Unexpected end of stream")]
    UnexpectedEndOfStream,

    /// A TLV record of a certain length was expected, but not found
    #[error("TLV Record had unexpected length")]
    UnexpectedLength,

    /// Data being read was not in the expected format
    #[error("Data had unexpected format")]
    FormatError,

    /// An error during an IO operation
    #[error("IO Error")]
    IOError(std::io::Error),
}

impl PartialEq for TlvError {
    fn eq(&self, other: &Self) -> bool {
        // Mostly auto-generated by rust-analyzer
        match (self, other) {
            (
                Self::TypeMismatch {
                    expected: l_expected,
                    found: l_found,
                },
                Self::TypeMismatch {
                    expected: r_expected,
                    found: r_found,
                },
            ) => l_expected == r_expected && l_found == r_found,
            (Self::IOError(_), Self::IOError(_)) => true,
            _ => core::mem::discriminant(self) == core::mem::discriminant(other),
        }
    }
}