Skip to main content

ic_wire/
error.rs

1/// The first allowance that refused an operation.
2#[derive(Clone, Copy, Debug, Eq, PartialEq)]
3pub enum LimitKind {
4    /// Complete encoded or supplied input bytes.
5    Bytes,
6    /// Conservative traversal allocation allowance.
7    Allocation,
8    /// Value and seed visits.
9    Nodes,
10    /// Nested traversal depth.
11    Depth,
12    /// Caller-provided destination capacity.
13    Output,
14    /// Caller-provided temporary storage for preparing extensions.
15    Scratch,
16    /// Declared field bound in a fallible constructor.
17    Field,
18}
19
20/// Wire failure categories, independent of transport and application errors.
21///
22/// Allocation is reserved for fallible codec-owned output allocation; consumer
23/// deserialization can allocate outside the codec's control.
24#[derive(Clone, Copy, Debug, Eq, PartialEq)]
25pub enum Error {
26    /// A wire, collection, traversal or output allowance was exceeded.
27    Limit(LimitKind),
28    /// Allocation failed within an admitted allowance.
29    Allocation,
30    /// The representation is invalid or truncated.
31    Malformed,
32    /// Input remains after the expected value.
33    Trailing,
34    /// The envelope marker, method identity or direction differs.
35    Identity,
36    /// A well-formed evolved value cannot satisfy the required projection.
37    Incompatible,
38}
39
40impl core::fmt::Display for Error {
41    fn fmt(&self, formatter: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
42        formatter.write_str(match self {
43            Self::Limit(LimitKind::Bytes) => "wire bytes limit",
44            Self::Limit(LimitKind::Allocation) => "wire allocation limit",
45            Self::Limit(LimitKind::Nodes) => "wire nodes limit",
46            Self::Limit(LimitKind::Depth) => "wire depth limit",
47            Self::Limit(LimitKind::Output) => "wire output limit",
48            Self::Limit(LimitKind::Scratch) => "wire scratch limit",
49            Self::Limit(LimitKind::Field) => "wire field limit",
50            Self::Allocation => "wire allocation",
51            Self::Malformed => "wire malformed",
52            Self::Trailing => "wire trailing",
53            Self::Identity => "wire identity",
54            Self::Incompatible => "wire incompatible",
55        })
56    }
57}
58
59impl core::error::Error for Error {}