fidl_next_protocol/
framework_error.rs

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.
4
5use core::fmt;
6use core::hint::unreachable_unchecked;
7
8use fidl_next_codec::{
9    munge, Decode, DecodeError, Encodable, Encode, EncodeError, Slot, TakeFrom, WireI32,
10    ZeroPadding,
11};
12
13/// An internal framework error.
14#[derive(Clone, Copy, Debug)]
15#[repr(i32)]
16pub enum FrameworkError {
17    /// The protocol method was not recognized by the receiver.
18    UnknownMethod = -2,
19}
20
21/// An internal framework error.
22#[derive(Clone, Copy)]
23#[repr(transparent)]
24pub struct WireFrameworkError {
25    inner: WireI32,
26}
27
28unsafe impl ZeroPadding for WireFrameworkError {
29    #[inline]
30    unsafe fn zero_padding(_: *mut Self) {}
31}
32
33impl fmt::Debug for WireFrameworkError {
34    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
35        FrameworkError::from(*self).fmt(f)
36    }
37}
38
39impl From<WireFrameworkError> for FrameworkError {
40    fn from(value: WireFrameworkError) -> Self {
41        match *value.inner {
42            -2 => Self::UnknownMethod,
43            _ => unsafe { unreachable_unchecked() },
44        }
45    }
46}
47
48unsafe impl<D: ?Sized> Decode<D> for WireFrameworkError {
49    fn decode(slot: Slot<'_, Self>, _: &mut D) -> Result<(), DecodeError> {
50        munge!(let Self { inner } = slot);
51        match **inner {
52            -2 => Ok(()),
53            code => Err(DecodeError::InvalidFrameworkError(code)),
54        }
55    }
56}
57
58impl Encodable for FrameworkError {
59    type Encoded = WireFrameworkError;
60}
61
62impl<E: ?Sized> Encode<E> for FrameworkError {
63    fn encode(&mut self, _: &mut E, slot: Slot<'_, Self::Encoded>) -> Result<(), EncodeError> {
64        munge!(let WireFrameworkError { mut inner } = slot);
65        inner.write(WireI32(match self {
66            Self::UnknownMethod => -2,
67        }));
68
69        Ok(())
70    }
71}
72
73impl TakeFrom<WireFrameworkError> for FrameworkError {
74    fn take_from(from: &WireFrameworkError) -> Self {
75        Self::from(*from)
76    }
77}