dvb-si 17.0.0

ETSI EN 300 468 DVB Service Information parser + builder. MPEG-2 PSI included.
Documentation
//! Error type returned by every parser in this crate.

use thiserror::Error;

/// Crate-wide result alias.
pub type Result<T> = core::result::Result<T, Error>;

/// Error variants that parsers + builders can return.
///
/// Spec references inside `#[error(...)]` strings quote clauses from
/// ETSI EN 300 468 v1.19.1 where applicable.
#[derive(Debug, Error, PartialEq, Eq, Clone)]
#[non_exhaustive]
pub enum Error {
    /// Input buffer was shorter than the smallest valid encoding for the type.
    #[error(transparent)]
    BufferTooShort(#[from] broadcast_core::error::BufferTooShort),

    /// CRC-32 validation failed for a table section.
    #[error(transparent)]
    CrcMismatch(#[from] broadcast_core::error::CrcMismatch),

    /// `table_id` byte doesn't match any expected value for the parser invoked.
    #[error("unexpected table_id {table_id:#04x} for {what} (expected one of {expected:?})")]
    #[non_exhaustive]
    UnexpectedTableId {
        /// Byte value actually read.
        table_id: u8,
        /// Table names or parser expecting it.
        what: &'static str,
        /// The permitted set.
        expected: &'static [u8],
    },

    /// Descriptor payload failed semantic validation.
    #[error("invalid descriptor (tag {tag:#04x}): {reason}")]
    #[non_exhaustive]
    InvalidDescriptor {
        /// Descriptor tag being parsed.
        tag: u8,
        /// Specific failure reason.
        reason: &'static str,
    },

    /// A decoded value passed to a `set_*` accessor could not be encoded to the
    /// field's fixed wire representation (e.g. a duration ≥ 100 hours, or a date
    /// outside the 16-bit MJD range).
    #[error("value out of range for {field}: {reason}")]
    #[non_exhaustive]
    ValueOutOfRange {
        /// Field being set.
        field: &'static str,
        /// Why the value is not representable.
        reason: &'static str,
    },

    /// Text in a Table A.3 character set whose codec needs the
    /// `legacy-charsets` feature, decoded with that feature off.
    #[error("{charset} text needs the `legacy-charsets` feature")]
    #[non_exhaustive]
    CharsetNotEnabled {
        /// The character set the selector names.
        charset: crate::text::LegacyCharset,
    },

    /// A `section_length` declared more bytes than the containing buffer could hold.
    #[error("section_length {declared} exceeds remaining buffer ({available} bytes)")]
    #[non_exhaustive]
    SectionLengthOverflow {
        /// Length bytes declared inside the section header.
        declared: usize,
        /// Bytes actually available after the header.
        available: usize,
    },

    /// A whole document exceeded the named byte limit it was read under.
    #[error("{limit_name} exceeds the configured limit of {limit_bytes} bytes")]
    #[non_exhaustive]
    TooLarge {
        /// Name of the document being read.
        limit_name: &'static str,
        /// The ceiling it exceeded, in bytes.
        limit_bytes: u64,
    },

    /// A reserved or fixed-value bit group was not in the state a spec clause
    /// requires. Most parsers are permissive about reserved bits; this is only
    /// emitted when a clause specifically requires a value.
    #[error(transparent)]
    ReservedBits(#[from] broadcast_core::error::ReservedBits),

    /// A field held a value its spec clause forbids.
    #[error(transparent)]
    InvalidValue(#[from] broadcast_core::error::InvalidValue),

    /// A field is structurally invalid with no single offending number.
    #[error(transparent)]
    InvalidField(#[from] broadcast_core::error::InvalidField),

    /// Write buffer passed to `serialize_into` was smaller than `serialized_len()`.
    #[error(transparent)]
    OutputBufferTooSmall(#[from] broadcast_core::error::OutputBufferTooSmall),

    /// A fixed marker did not match (TS sync byte, expected `0x47`).
    #[error(transparent)]
    InvalidSync(#[from] broadcast_core::error::InvalidSync),

    /// A serializer's length or count value did not fit its wire field.
    /// Returned instead of silently narrowing (`as u8`/`as u16`/masking),
    /// which would emit a misframed section (see #1129).
    #[error(transparent)]
    FieldOverflow(#[from] broadcast_core::error::FieldOverflow),
}

impl Error {
    /// Builds [`Self::TooLarge`].
    #[must_use]
    pub fn too_large(limit_name: &'static str, limit_bytes: u64) -> Self {
        Self::TooLarge {
            limit_name,
            limit_bytes,
        }
    }
    /// Builds [`Self::InvalidDescriptor`].
    #[must_use]
    pub fn invalid_descriptor(tag: u8, reason: &'static str) -> Self {
        Self::InvalidDescriptor { tag, reason }
    }

    /// Builds [`Self::SectionLengthOverflow`].
    #[must_use]
    pub fn section_length_overflow(declared: usize, available: usize) -> Self {
        Self::SectionLengthOverflow {
            declared,
            available,
        }
    }

    /// Builds [`Self::ValueOutOfRange`].
    #[must_use]
    pub fn value_out_of_range(field: &'static str, reason: &'static str) -> Self {
        Self::ValueOutOfRange { field, reason }
    }
}

impl From<mpeg_ts::Error> for Error {
    fn from(e: mpeg_ts::Error) -> Self {
        match e {
            mpeg_ts::Error::BufferTooShort(e) => Error::BufferTooShort(e),
            mpeg_ts::Error::CrcMismatch(e) => Error::CrcMismatch(e),
            mpeg_ts::Error::InvalidSync(e) => Error::InvalidSync(e),
            mpeg_ts::Error::OutputBufferTooSmall(e) => Error::OutputBufferTooSmall(e),
            mpeg_ts::Error::SectionLengthOverflow {
                declared,
                available,
                ..
            } => Error::SectionLengthOverflow {
                declared,
                available,
            },
            // mpeg_ts::Error is #[non_exhaustive]; catch future variants generically.
            _ => Error::BufferTooShort(broadcast_core::error::BufferTooShort::new(
                0,
                0,
                "mpeg_ts (unknown error)",
            )),
        }
    }
}