Skip to main content

android_abx/
error.rs

1//! [`AbxError`] — this crate's single error type — and the [`Result`] alias
2//! built on it.
3
4/// Everything that can go wrong parsing, encoding, or deserializing ABX data.
5#[derive(Debug, thiserror::Error)]
6pub enum AbxError {
7    /// The 4-byte `ABX\0` magic header didn't match at the start of the input.
8    #[error("invalid magic header: expected {expected:?}, got {actual:?}")]
9    InvalidMagic {
10        /// The magic bytes this crate expects (`ABX\0`).
11        expected: [u8; 4],
12        /// The bytes actually found at the start of the input.
13        actual: [u8; 4],
14    },
15    /// The input ended before a complete token or value could be read.
16    #[error("unexpected end of input while reading {0}")]
17    UnexpectedEof(&'static str),
18    /// An interned-string back-reference pointed past the end of the pool.
19    #[error("invalid interned string index {0}")]
20    BadInternedIndex(u16),
21    /// A string's bytes were not valid UTF-8.
22    #[error("invalid UTF-8 in string")]
23    InvalidUtf8,
24    /// An attribute value's type nibble didn't match any known `TYPE_*` constant.
25    #[error("unknown attribute type 0x{0:02X}")]
26    UnknownAttributeType(u8),
27    /// A token's command nibble didn't match any known `CMD_*` constant.
28    #[error("unknown command 0x{0:02X}")]
29    UnknownCommand(u8),
30    /// A string or byte blob was too long to fit the wire format's `u16` length prefix.
31    #[error("value too long: {len} bytes exceeds maximum of {max}")]
32    ValueTooLong {
33        /// The value's actual length in bytes.
34        len: usize,
35        /// The maximum length the wire format can express (`u16::MAX`, 65,535).
36        max: usize,
37    },
38    /// The underlying `nom` parser failed; the message is `nom`'s own formatted error.
39    #[error("nom parse error: {0}")]
40    Nom(String),
41    /// A read or write on the underlying `Read`/`Write` failed.
42    #[error("I/O error: {0}")]
43    Io(#[from] std::io::Error),
44    /// Mapping decoded ABX events onto a `serde` type failed (`serialize` feature).
45    #[error("deserialization error: {0}")]
46    Deserialization(String),
47    /// Parsing XML text failed while encoding it to ABX (`xml` feature).
48    #[error("XML parse error: {0}")]
49    Xml(String),
50}
51
52impl<I: std::fmt::Debug> From<nom::Err<nom::error::Error<I>>> for AbxError {
53    fn from(e: nom::Err<nom::error::Error<I>>) -> Self {
54        AbxError::Nom(format!("{:?}", e))
55    }
56}
57
58/// This crate's [`Result`](std::result::Result) alias, with [`AbxError`] as the error type.
59pub type Result<T> = std::result::Result<T, AbxError>;