fidl_next_bind/
error.rs

1// Copyright 2025 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.
4
5use thiserror::Error;
6
7use fidl_next_codec::{DecodeError, EncodeError};
8
9/// Error returned by TryFrom on a strict enum if none of the members match the supplied value.
10#[derive(Debug)]
11pub struct UnknownStrictEnumMemberError(i128);
12
13impl UnknownStrictEnumMemberError {
14    /// Create a new error given an unknown value.
15    pub fn new(unknown_value: i128) -> Self {
16        Self(unknown_value)
17    }
18}
19
20impl core::fmt::Display for UnknownStrictEnumMemberError {
21    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
22        write!(f, "Strict enum doesn't have a member with value: {}", self.0)
23    }
24}
25
26impl core::error::Error for UnknownStrictEnumMemberError {}
27
28/// An encoding, decoding, or transport FIDL error.
29#[derive(Error, Clone, Debug)]
30pub enum Error<
31    #[cfg(feature = "fuchsia")] E = <zx::Channel as fidl_next_protocol::Transport>::Error,
32    #[cfg(not(feature = "fuchsia"))] E,
33> {
34    /// A FIDL encoding error.
35    #[error("encoding error: {0}")]
36    Encode(#[from] EncodeError),
37    /// A FIDL decoding error.
38    #[error("decoding error: {0}")]
39    Decode(#[from] DecodeError),
40    /// A FIDL transport error.
41    #[error("transport error: {0}")]
42    Transport(E),
43    /// The FIDL transport was closed.
44    #[error("the transport was closed")]
45    Closed,
46}
47
48impl<E> From<Option<E>> for Error<E> {
49    fn from(value: Option<E>) -> Self {
50        if let Some(e) = value {
51            Self::Transport(e)
52        } else {
53            Self::Closed
54        }
55    }
56}