1// Copyright 2024 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
45use thiserror::Error;
67/// Errors that can be produced when decoding FIDL messages.
8#[derive(Error, Debug)]
9pub enum DecodeError {
10/// A required handle was absent
11#[error("required handle is absent")]
12RequiredHandleAbsent,
1314/// A required value was absent
15#[error("required value is absent")]
16RequiredValueAbsent,
1718/// A boolean was set to a value other than 0 or 1
19#[error("`bool` field has an invalid value; expected 0 or 1, found {0}")]
20InvalidBool(u8),
2122/// A handle was set to a value other than 0 or u32::MAX
23#[error("handle has an invalid presence marker; expected 0 or u32::MAX, found {0}")]
24InvalidHandlePresence(u32),
2526/// A pointer was set to a value other than 0 or u64::MAX
27#[error("pointer has an invalid presence marker; expected 0 or u64::MAX, found {0}.")]
28InvalidPointerPresence(u64),
2930/// An envelope had an invalid size
31#[error("invalid envelope size; expected a multiple of 8, found {0}")]
32InvalidEnvelopeSize(u32),
3334/// An enum had an invalid ordinal
35#[error("invalid enum ordinal; expected a valid ordinal, found {0}")]
36InvalidEnumOrdinal(i128),
3738/// A union had an invalid ordinal
39#[error("invalid union ordinal; expected a valid ordinal, found {0}")]
40InvalidUnionOrdinal(usize),
4142/// A bit set had an invalid set of bits
43#[error("invalid bits; expected a subset of {expected:b}, found {actual:b}")]
44InvalidBits {
45/// The expected set of bits
46expected: usize,
47/// The actual set of bits
48actual: usize,
49 },
5051/// An envelope was out-of-line, but the out-of-line data was too small
52#[error(
53"envelope has out-of-line data which is too small; expected more than 4 bytes out-of-line, \
54 found {0} bytes"
55)]
56OutOfLineValueTooSmall(u32),
5758/// An envelope had inline data that was too big
59#[error(
60"envelope has inline data which is too big; expected 4 bytes or fewer, found {0} bytes"
61)]
62InlineValueTooBig(usize),
6364/// An envelope should always be inline, but it contained out-of-line data
65#[error("envelope should always be inline, but it contained {0} out-of-line bytes")]
66ExpectedInline(usize),
6768/// An envelope consumed a different number of handles than it indicated in its metadata
69#[error(
70"envelope consumed a different number of handles than it claimed that it would; expected \
71 {expected} to be consumed, found {actual} were consumed"
72)]
73IncorrectNumberOfHandlesConsumed {
74/// The number of handles the envelope was expected to consume
75expected: usize,
76/// The number of handles actually consumed by the envelope
77actual: usize,
78 },
7980/// An optional value was marked absent but its size was non-zero
81#[error("optional value is absent but has a non-zero size; expected 0, found {0}")]
82InvalidOptionalSize(u64),
8384/// A vector had a length greater than its allowed limit
85#[error(
86"vector has a length greater than the allowed limit; expected no more than {limit} \
87 elements, found {size} elements"
88)]
89VectorTooLong {
90/// The actual size of the vector
91size: u64,
92/// The maximum allowed size of the vector
93limit: u64,
94 },
9596/// A string contained non-UTF8 data
97#[error("string has non-UTF8 content; {0}")]
98InvalidUtf8(#[from] core::str::Utf8Error),
99100/// A union was marked absent, but its envelope was not set to zero
101#[error("union is absent but has a non-zero envelope")]
102InvalidUnionEnvelope,
103104/// A framework error contained an unrecognized error code.
105#[error("framework error has an unrecognized error code")]
106InvalidFrameworkError(i32),
107108/// The decoder ran out of data before decoding finished
109#[error("reached the end of the buffer before decoding finished")]
110InsufficientData,
111112/// The decoder ran out of handles before decoding finished
113#[error("consumed all handles before decoding finished")]
114InsufficientHandles,
115116/// Attempted to decode a driver handle with an decoder that does not support them.
117#[error("cannot decode driver handles with this decoder")]
118DriverHandlesUnsupported,
119120/// The decoder ran out of driver handles before decoding finished.
121#[error("consumed all driver handles before decoding finished")]
122InsufficientDriverHandles,
123124/// Decoding finished without consuming all of the bytes
125#[error(
126"finished decoding before all bytes were consumed; completed with {num_extra} bytes left \
127 over"
128)]
129ExtraBytes {
130/// The number of bytes left over after decoding finished
131num_extra: usize,
132 },
133134/// Decoding finished without consuming all of the handles
135#[error(
136"finished decoding before all handles were consumed; completed with {num_extra} handles \
137 left over"
138)]
139ExtraHandles {
140/// The number of handles left over after decoding finished
141num_extra: usize,
142 },
143}