essential_types/predicate/header/
error.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
use std::fmt::Display;

pub type DecodeResult<T> = core::result::Result<T, DecodeError>;

#[derive(Debug)]
/// Error when encoding a predicate.
pub enum PredicateError {
    /// State read too large.
    StateReadTooLarge(usize),
    /// Constraint too large.
    ConstraintTooLarge(usize),
    /// Too many state reads.
    TooManyStateReads(usize),
    /// Too many constraints.
    TooManyConstraints(usize),
    /// Predicate too large.
    PredicateTooLarge(usize),
}

#[derive(Debug)]
/// Error when decoding a predicate.
pub enum DecodeError {
    /// Missing number of state reads when decoding predicate header.
    MissingNumStateReads,
    /// Missing number of constraints when decoding predicate header.
    MissingNumConstraints,
    /// Missing nested length when decoding predicate header.
    MissingNestedLen,
    /// Overflow when decoding predicate.
    Overflow,
    /// Incorrect body length when decoding predicate.
    IncorrectBodyLength,
    /// Buffer too small when decoding predicate header.
    BufferTooSmall,
    /// Error with decoded predicate.
    PredicateError(PredicateError),
}

impl std::error::Error for PredicateError {}
impl std::error::Error for DecodeError {}

impl Display for DecodeError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            DecodeError::MissingNumStateReads => {
                write!(
                    f,
                    "missing number of state reads when decoding predicate header"
                )
            }
            DecodeError::MissingNumConstraints => {
                write!(
                    f,
                    "missing number of constraints when decoding predicate header"
                )
            }
            DecodeError::MissingNestedLen => {
                write!(f, "missing nested length when decoding predicate header")
            }
            DecodeError::Overflow => {
                write!(f, "overflow when decoding predicate")
            }
            DecodeError::IncorrectBodyLength => {
                write!(f, "incorrect body length when decoding predicate")
            }
            DecodeError::BufferTooSmall => {
                write!(f, "buffer too small when decoding predicate header")
            }
            DecodeError::PredicateError(e) => {
                write!(f, "predicate error: {}", e)
            }
        }
    }
}

impl Display for PredicateError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            PredicateError::StateReadTooLarge(s) => {
                write!(f, "state read too large when encoding predicate: {}", s)
            }
            PredicateError::ConstraintTooLarge(s) => {
                write!(f, "constraint too large when encoding predicate: {}", s)
            }
            PredicateError::TooManyStateReads(s) => {
                write!(f, "too many state reads when encoding predicate: {}", s)
            }
            PredicateError::TooManyConstraints(s) => {
                write!(f, "too many constraints when encoding predicate: {}", s)
            }
            PredicateError::PredicateTooLarge(s) => {
                write!(f, "predicate too large when encoding predicate: {}", s)
            }
        }
    }
}

impl From<PredicateError> for DecodeError {
    fn from(e: PredicateError) -> Self {
        DecodeError::PredicateError(e)
    }
}