server/
error.rs

1// SPDX-FileCopyrightText: 2025 Foundation Devices, Inc. <hello@foundation.xyz>
2// SPDX-License-Identifier: GPL-3.0-or-later
3use rkyv::rancor;
4
5#[derive(Debug)]
6pub enum Error {
7    /// syscall error
8    Xous(xous::Error),
9    /// ipc error
10    Ipc(rancor::Error),
11}
12
13impl std::fmt::Display for Error {
14    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
15        match self {
16            Self::Xous(e) => write!(f, "{e:?}"),
17            Self::Ipc(e) => write!(f, "{e}"),
18        }
19    }
20}
21
22impl Error {
23    #[inline]
24    pub fn into_xous(self) -> xous::Error {
25        match self {
26            Error::Xous(e) => e,
27            Error::Ipc(_) => xous::Error::InternalError,
28        }
29    }
30
31    #[inline]
32    pub fn into_rancor(self) -> rancor::Error {
33        match self {
34            Error::Xous(e) => rancor::Source::new(XousError(e)),
35            Error::Ipc(e) => e,
36        }
37    }
38}
39
40impl From<xous::Error> for Error {
41    #[inline]
42    fn from(e: xous::Error) -> Self { Self::Xous(e) }
43}
44
45impl From<rancor::Error> for Error {
46    #[inline]
47    fn from(e: rancor::Error) -> Self { Self::Ipc(e) }
48}
49
50impl PartialEq<xous::Error> for Error {
51    fn eq(&self, other: &xous::Error) -> bool { matches!(self, Self::Xous(e) if e == other) }
52}
53
54#[repr(transparent)]
55struct XousError(xous::Error);
56
57impl std::fmt::Debug for XousError {
58    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!(f, "{:?}", self.0) }
59}
60
61impl std::fmt::Display for XousError {
62    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!(f, "{:?}", self.0) }
63}
64
65impl std::error::Error for XousError {}
66
67const _: () = {
68    #[cfg(target_pointer_width = "32")]
69    const PTR_SIZE: usize = 4;
70    #[cfg(target_pointer_width = "64")]
71    const PTR_SIZE: usize = 8;
72
73    #[cfg(debug_assertions)]
74    const ERROR_SIZE: usize = PTR_SIZE * 2;
75    #[cfg(not(debug_assertions))]
76    const ERROR_SIZE: usize = PTR_SIZE;
77
78    assert!(std::mem::size_of::<xous::Error>() == PTR_SIZE);
79    assert!(std::mem::size_of::<whence::Error<xous::Error>>() == PTR_SIZE * 2);
80
81    assert!(std::mem::size_of::<Error>() == ERROR_SIZE);
82    assert!(std::mem::size_of::<whence::Error<Error>>() == ERROR_SIZE + PTR_SIZE);
83};