graven/
error.rs

1use core::fmt;
2
3/// Every failure mode of reading or writing a Graven buffer.
4#[derive(Debug, Clone, Copy, PartialEq, Eq)]
5#[non_exhaustive]
6pub enum Error {
7    /// A length, header or element extends past the end of the buffer.
8    OutOfBounds,
9    /// A field slot is only partially covered by the table's `fixed_len`.
10    Truncated,
11    /// A pointer targets a position outside the buffer.
12    BadOffset,
13    /// A string is not valid UTF-8.
14    InvalidUtf8,
15    /// A field required by the reader's schema is absent.
16    Missing { field: &'static str },
17    /// `verify` exceeded its maximum nesting depth.
18    DepthExceeded,
19    /// `verify` exceeded its visited-bytes budget.
20    BudgetExceeded,
21    /// The message would exceed `i32::MAX` bytes.
22    TooLarge,
23    /// The envelope does not start with the Graven magic bytes.
24    BadMagic,
25    /// The envelope version is not supported by this reader.
26    UnsupportedVersion(u8),
27    /// The envelope header is malformed.
28    BadHeader,
29    /// The input is shorter than the envelope needs; at least `needed` bytes are required.
30    Incomplete { needed: usize },
31    /// The envelope's schema id differs from the one the reader expects.
32    SchemaMismatch { expected: u64, found: u64 },
33    /// The envelope's CRC32C does not match its payload.
34    ChecksumMismatch,
35}
36
37impl fmt::Display for Error {
38    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
39        match self {
40            Error::OutOfBounds => f.write_str("out of bounds"),
41            Error::Truncated => f.write_str("field truncated by fixed_len"),
42            Error::BadOffset => f.write_str("pointer outside buffer"),
43            Error::InvalidUtf8 => f.write_str("invalid utf-8"),
44            Error::Missing { field } => write!(f, "required field `{field}` missing"),
45            Error::DepthExceeded => f.write_str("verify depth exceeded"),
46            Error::BudgetExceeded => f.write_str("verify budget exceeded"),
47            Error::TooLarge => f.write_str("message exceeds i32::MAX bytes"),
48            Error::BadMagic => f.write_str("bad magic"),
49            Error::UnsupportedVersion(v) => write!(f, "unsupported version {v}"),
50            Error::BadHeader => f.write_str("malformed header"),
51            Error::Incomplete { needed } => write!(f, "incomplete input: need at least {needed} bytes"),
52            Error::SchemaMismatch { expected, found } => {
53                write!(f, "schema mismatch: expected {expected:#x}, found {found:#x}")
54            }
55            Error::ChecksumMismatch => f.write_str("checksum mismatch"),
56        }
57    }
58}
59
60impl core::error::Error for Error {}