Skip to main content

hap_tlv8/
error.rs

1//! Error type for `hap-tlv8`.
2//!
3//! [`Tlv8Error`] covers every failure mode of [`crate::Tlv8Reader`] and the
4//! typed getters on [`crate::Tlv8Map`]. The writer never fails: it appends to
5//! a `Vec<u8>` and fragments automatically, so it has no error path.
6
7use thiserror::Error;
8
9/// All errors `hap-tlv8` can produce.
10#[derive(Debug, Error, PartialEq, Eq)]
11#[non_exhaustive]
12pub enum Tlv8Error {
13    /// The reader reached the end of the input before a declared value's
14    /// bytes were available (a truncated item).
15    #[error("unexpected end of input: declared length exceeds remaining bytes")]
16    UnexpectedEof,
17
18    /// A typed getter was asked for an integer width that does not match the
19    /// number of bytes stored for that type (for example `get_u16` on a
20    /// 3-byte value).
21    #[error(
22        "integer value has {actual} bytes, too large for the requested {requested}-byte width"
23    )]
24    IntegerTooLarge {
25        /// The width in bytes the caller requested (1, 2, 4, or 8).
26        requested: usize,
27        /// The number of value bytes actually stored for the type.
28        actual: usize,
29    },
30}
31
32/// `Result<T, Tlv8Error>` for convenience.
33pub type Result<T> = core::result::Result<T, Tlv8Error>;