Skip to main content

uts_core/
error.rs

1use crate::codec::v1::opcode::OpCode;
2
3/// Errors returned while decoding proofs.
4#[derive(Debug, thiserror::Error)]
5pub enum DecodeError {
6    /// File began with invalid magic bytes.
7    #[error("bad magic bytes")]
8    BadMagic,
9    /// File has a version we do not understand.
10    #[error("bad version")]
11    BadVersion,
12    /// Expected an attestation tag but decoded something else.
13    #[error("bad attestation tag")]
14    BadAttestationTag,
15    /// Read an LEB128-encoded integer that overflowed the expected size.
16    #[error("read a LEB128 value overflows {0} bits")]
17    LEB128Overflow(u32),
18    /// Encountered an unrecognized opcode.
19    #[error("unrecognized opcode: 0x{0:02x}")]
20    BadOpCode(u8),
21    /// Expected a digest opcode but decoded something else.
22    #[error("expected digest opcode but got: {0}")]
23    ExpectedDigestOp(OpCode),
24    /// Read a value that is not in the allowed range.
25    #[error("read value out of range")]
26    OutOfRange,
27    /// Encountered an invalid character in a URI.
28    #[error("invalid character in URI")]
29    InvalidUriChar,
30    /// URI is too long.
31    #[error("URI too long")]
32    UriTooLong,
33    /// Recursed deeper than allowed while decoding the proof.
34    #[error("recursion limit reached")]
35    RecursionLimit,
36    /// Reached end-of-file unexpectedly.
37    #[error("unexpected end of file")]
38    UnexpectedEof,
39    /// General I/O error
40    #[error("I/O error: {0}")]
41    Io(std::io::Error),
42}
43
44/// Errors returned while encoding proofs.
45#[derive(Debug, thiserror::Error)]
46pub enum EncodeError {
47    /// Tried to encode a `usize` exceeding `u32::MAX`.
48    #[error("tried to encode a usize exceeding u32::MAX")]
49    UsizeOverflow,
50    /// Encountered an invalid character in a URI.
51    #[error("invalid character in URI")]
52    InvalidUriChar,
53    /// URI is too long.
54    #[error("URI too long")]
55    UriTooLong,
56    /// General I/O error
57    #[error("I/O error: {0}")]
58    Io(#[from] std::io::Error),
59}
60
61impl From<std::io::Error> for DecodeError {
62    fn from(err: std::io::Error) -> Self {
63        match err.kind() {
64            std::io::ErrorKind::UnexpectedEof => Self::UnexpectedEof,
65            _ => Self::Io(err),
66        }
67    }
68}