Skip to main content

oxabl_cache_format/
error.rs

1use std::fmt;
2
3/// Why a byte string is not a valid frame.
4#[derive(Debug, Clone, Copy, PartialEq, Eq)]
5#[non_exhaustive]
6pub enum FormatError {
7    /// The magic bytes differ from the expected frame.
8    BadMagic,
9    /// The version byte is not supported.
10    UnsupportedVersion(u8),
11    /// The flags byte has bits that are not defined for the frame.
12    UnknownFlags(u8),
13    /// A reserved field is not zero.
14    NonZeroReserved,
15    /// The input ends before the frame does.
16    Truncated,
17    /// Bytes follow the end of the frame.
18    TrailingBytes,
19    /// A count or length exceeds the named limit.
20    LimitExceeded(&'static str),
21    /// A string is not valid UTF-8.
22    InvalidUtf8,
23    /// The bytes decode but are not the canonical encoding, or break a rule of the format.
24    Invalid(&'static str),
25}
26
27impl fmt::Display for FormatError {
28    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
29        match self {
30            Self::BadMagic => f.write_str("wrong magic"),
31            Self::UnsupportedVersion(v) => write!(f, "unsupported version {v}"),
32            Self::UnknownFlags(v) => write!(f, "unknown flag bits {v:#04x}"),
33            Self::NonZeroReserved => f.write_str("reserved field is not zero"),
34            Self::Truncated => f.write_str("truncated input"),
35            Self::TrailingBytes => f.write_str("trailing bytes"),
36            Self::LimitExceeded(what) => write!(f, "limit exceeded: {what}"),
37            Self::InvalidUtf8 => f.write_str("string is not valid UTF-8"),
38            Self::Invalid(what) => write!(f, "invalid: {what}"),
39        }
40    }
41}
42
43impl std::error::Error for FormatError {}
44
45/// Result of every encode and decode in this crate.
46pub type Result<T> = std::result::Result<T, FormatError>;