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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
// Copyright 2024 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

use thiserror::Error;

/// Errors that can be produced when decoding FIDL messages.
#[derive(Error, Debug)]
pub enum DecodeError {
    /// A required handle was absent
    #[error("required handle is absent")]
    RequiredHandleAbsent,

    /// A required value was absent
    #[error("required value is absent")]
    RequiredValueAbsent,

    /// A boolean was set to a value other than 0 or 1
    #[error("`bool` field has an invalid value; expected 0 or 1, found {0}")]
    InvalidBool(u8),

    /// A handle was set to a value other than 0 or u32::MAX
    #[error("handle has an invalid presence marker; expected 0 or u32::MAX, found {0}")]
    InvalidHandlePresence(u32),

    /// A pointer was set to a value other than 0 or u64::MAX
    #[error("pointer has an invalid presence marker; expected 0 or u64::MAX, found {0}.")]
    InvalidPointerPresence(u64),

    /// An envelope had an invalid size
    #[error("invalid envelope size; expected a multiple of 8, found {0}")]
    InvalidEnvelopeSize(u32),

    /// An enum had an invalid ordinal
    #[error("invalid enum ordinal; expected a valid ordinal, found {0}")]
    InvalidEnumOrdinal(usize),

    /// A union had an invalid ordinal
    #[error("invalid union ordinal; expected a valid ordinal, found {0}")]
    InvalidUnionOrdinal(usize),

    /// An envelope was out-of-line, but the out-of-line data was too small
    #[error(
        "envelope has out-of-line data which is too small; expected more than 4 bytes out-of-line, \
        found {0} bytes"
    )]
    OutOfLineValueTooSmall(u32),

    /// An envelope had inline data that was too big
    #[error(
        "envelope has inline data which is too big; expected 4 bytes or fewer, found {0} bytes"
    )]
    InlineValueTooBig(usize),

    /// An envelope consumed a different number of handles than it indicated in its metadata
    #[error(
        "envelope consumed a different number of handles than it claimed that it would; expected \
        {expected} to be consumed, found {actual} were consumed"
    )]
    IncorrectNumberOfHandlesConsumed {
        /// The number of handles the envelope was expected to consume
        expected: usize,
        /// The number of handles actually consumed by the envelope
        actual: usize,
    },

    /// An optional value was marked absent but its size was non-zero
    #[error("optional value is absent but has a non-zero size; expected 0, found {0}")]
    InvalidOptionalSize(u64),

    /// A vector had a length greater than its allowed limit
    #[error(
        "vector has a length greater than the allowed limit; expected no more than {limit} \
        elements, found {size} elements"
    )]
    VectorTooLong {
        /// The actual size of the vector
        size: u64,
        /// The maximum allowed size of the vector
        limit: u64,
    },

    /// A string contained non-UTF8 data
    #[error("string has non-UTF8 content; {0}")]
    InvalidUtf8(#[from] core::str::Utf8Error),

    /// A union was marked absent, but its envelope was not set to zero
    #[error("union is absent but has a non-zero envelope")]
    InvalidUnionEnvelope,

    /// The decoder ran out of data before decoding finished
    #[error("reached the end of the buffer before decoding finished")]
    InsufficientData,

    /// The decoder ran out of handles before decoding finished
    #[error("consumed all handles before decoding finished")]
    InsufficientHandles,

    /// Decoding finished without consuming all of the bytes
    #[error(
        "finished decoding before all bytes were consumed; completed with {num_extra} bytes left \
        over"
    )]
    ExtraBytes {
        /// The number of bytes left over after decoding finished
        num_extra: usize,
    },

    /// Decoding finished without consuming all of the handles
    #[error(
        "finished decoding before all handles were consumed; completed with {num_extra} handles \
        left over"
    )]
    ExtraHandles {
        /// The number of handles left over after decoding finished
        num_extra: usize,
    },
}