sel4_shared_ring_buffer_block_io/
errors.rs

1//
2// Copyright 2023, Colias Group, LLC
3//
4// SPDX-License-Identifier: BSD-2-Clause
5//
6
7use sel4_shared_ring_buffer::PeerMisbehaviorError as SharedRingBuffersPeerMisbehaviorError;
8use sel4_shared_ring_buffer_bookkeeping::slot_tracker::SlotTrackerError;
9
10#[derive(Debug, Clone)]
11pub enum ErrorOrUserError {
12    Error(Error),
13    UserError(UserError),
14}
15
16#[derive(Debug, Clone)]
17pub enum UserError {
18    InvalidRequestIndex,
19    RequestStateMismatch,
20    TooManyOutstandingRequests,
21}
22
23#[derive(Debug, Clone)]
24pub enum Error {
25    IOError(IOError),
26    BounceBufferAllocationError,
27    PeerMisbehaviorError(PeerMisbehaviorError),
28}
29
30#[derive(Debug, Clone)]
31pub struct IOError;
32
33#[derive(Debug, Clone)]
34pub enum PeerMisbehaviorError {
35    InvalidDescriptor,
36    DescriptorMismatch,
37    OutOfBoundsCookie,
38    StateMismatch,
39    SharedRingBuffersPeerMisbehaviorError(SharedRingBuffersPeerMisbehaviorError),
40}
41
42impl ErrorOrUserError {
43    pub fn unwrap_error(self) -> Error {
44        match self {
45            Self::Error(err) => err,
46            Self::UserError(err) => panic!(
47                "called `ErrorOrUserError::unwrap_error()` on an `UserError` value: {:?}",
48                err
49            ),
50        }
51    }
52}
53
54impl From<Error> for ErrorOrUserError {
55    fn from(err: Error) -> Self {
56        Self::Error(err)
57    }
58}
59
60impl From<UserError> for ErrorOrUserError {
61    fn from(err: UserError) -> Self {
62        Self::UserError(err)
63    }
64}
65
66impl From<IOError> for Error {
67    fn from(err: IOError) -> Self {
68        Self::IOError(err)
69    }
70}
71
72impl From<IOError> for ErrorOrUserError {
73    fn from(err: IOError) -> Self {
74        Error::from(err).into()
75    }
76}
77
78impl From<PeerMisbehaviorError> for Error {
79    fn from(err: PeerMisbehaviorError) -> Self {
80        Self::PeerMisbehaviorError(err)
81    }
82}
83
84impl From<PeerMisbehaviorError> for ErrorOrUserError {
85    fn from(err: PeerMisbehaviorError) -> Self {
86        Error::from(err).into()
87    }
88}
89
90impl From<SharedRingBuffersPeerMisbehaviorError> for PeerMisbehaviorError {
91    fn from(err: SharedRingBuffersPeerMisbehaviorError) -> Self {
92        Self::SharedRingBuffersPeerMisbehaviorError(err)
93    }
94}
95
96impl From<SharedRingBuffersPeerMisbehaviorError> for Error {
97    fn from(err: SharedRingBuffersPeerMisbehaviorError) -> Self {
98        PeerMisbehaviorError::from(err).into()
99    }
100}
101
102impl From<SharedRingBuffersPeerMisbehaviorError> for ErrorOrUserError {
103    fn from(err: SharedRingBuffersPeerMisbehaviorError) -> Self {
104        Error::from(err).into()
105    }
106}
107
108impl From<SlotTrackerError> for UserError {
109    fn from(err: SlotTrackerError) -> Self {
110        match err {
111            SlotTrackerError::OutOfBounds => UserError::InvalidRequestIndex,
112            SlotTrackerError::StateMismatch => UserError::RequestStateMismatch,
113        }
114    }
115}
116
117impl From<SlotTrackerError> for ErrorOrUserError {
118    fn from(err: SlotTrackerError) -> Self {
119        UserError::from(err).into()
120    }
121}