|
| 1 | +use alloc::collections::TryReserveError; |
| 2 | +use core::ffi::c_int; |
| 3 | +use core::fmt::{self, Display}; |
| 4 | +use kernel::prelude::EINVAL; |
| 5 | + |
| 6 | +pub(crate) enum WireFormatError { |
| 7 | + InvalidSerializedData, |
| 8 | + KernelError(kernel::error::Error), |
| 9 | + TryReserveError(TryReserveError), |
| 10 | + CapnpError(capnp::Error), |
| 11 | + FromIntError(core::num::TryFromIntError), |
| 12 | + FromSliceError(core::array::TryFromSliceError), |
| 13 | + HexError(hex::FromHexError), |
| 14 | +} |
| 15 | + |
| 16 | +impl WireFormatError { |
| 17 | + pub(crate) fn to_errno(&self) -> c_int { |
| 18 | + match self { |
| 19 | + WireFormatError::InvalidSerializedData => kernel::error::Error::to_errno(EINVAL), |
| 20 | + WireFormatError::KernelError(e) => kernel::error::Error::to_errno(*e), |
| 21 | + WireFormatError::TryReserveError(_) => kernel::error::Error::to_errno(EINVAL), |
| 22 | + WireFormatError::CapnpError(_) => kernel::error::Error::to_errno(EINVAL), |
| 23 | + WireFormatError::FromIntError(_) => kernel::error::Error::to_errno(EINVAL), |
| 24 | + WireFormatError::FromSliceError(_) => kernel::error::Error::to_errno(EINVAL), |
| 25 | + WireFormatError::HexError(_) => kernel::error::Error::to_errno(EINVAL), |
| 26 | + } |
| 27 | + } |
| 28 | + |
| 29 | + pub(crate) fn from_errno(errno: kernel::error::Error) -> Self { |
| 30 | + Self::KernelError(errno) |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +impl Display for WireFormatError { |
| 35 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 36 | + match self { |
| 37 | + WireFormatError::InvalidSerializedData => f.write_str("invalid serialized data"), |
| 38 | + WireFormatError::KernelError(e) => { |
| 39 | + f.write_fmt(format_args!("Kernel error {}", e.to_errno())) |
| 40 | + } |
| 41 | + WireFormatError::TryReserveError(_) => f.write_str("TryReserveError"), |
| 42 | + WireFormatError::CapnpError(_) => f.write_str("Capnp error"), |
| 43 | + WireFormatError::FromIntError(_) => f.write_str("TryFromIntError"), |
| 44 | + WireFormatError::FromSliceError(_) => f.write_str("TryFromSliceError"), |
| 45 | + WireFormatError::HexError(_) => f.write_str("HexError"), |
| 46 | + } |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +pub(crate) type Result<T> = kernel::error::Result<T, WireFormatError>; |
| 51 | + |
| 52 | +// TODO figure out how to use thiserror |
| 53 | +#[allow(unused_qualifications)] |
| 54 | +impl core::convert::From<kernel::error::Error> for WireFormatError { |
| 55 | + #[allow(deprecated)] |
| 56 | + fn from(source: kernel::error::Error) -> Self { |
| 57 | + WireFormatError::KernelError(source) |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +#[allow(unused_qualifications)] |
| 62 | +impl core::convert::From<TryReserveError> for WireFormatError { |
| 63 | + #[allow(deprecated)] |
| 64 | + fn from(source: TryReserveError) -> Self { |
| 65 | + WireFormatError::TryReserveError(source) |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +#[allow(unused_qualifications)] |
| 70 | +impl core::convert::From<capnp::Error> for WireFormatError { |
| 71 | + #[allow(deprecated)] |
| 72 | + fn from(source: capnp::Error) -> Self { |
| 73 | + WireFormatError::CapnpError(source) |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +#[allow(unused_qualifications)] |
| 78 | +impl core::convert::From<core::array::TryFromSliceError> for WireFormatError { |
| 79 | + #[allow(deprecated)] |
| 80 | + fn from(source: core::array::TryFromSliceError) -> Self { |
| 81 | + WireFormatError::FromSliceError(source) |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +#[allow(unused_qualifications)] |
| 86 | +impl core::convert::From<core::num::TryFromIntError> for WireFormatError { |
| 87 | + #[allow(deprecated)] |
| 88 | + fn from(source: core::num::TryFromIntError) -> Self { |
| 89 | + WireFormatError::FromIntError(source) |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +impl core::convert::From<hex::FromHexError> for WireFormatError { |
| 94 | + #[allow(deprecated)] |
| 95 | + fn from(source: hex::FromHexError) -> Self { |
| 96 | + WireFormatError::HexError(source) |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +#[allow(unused_qualifications)] |
| 101 | +impl core::convert::From<WireFormatError> for kernel::error::Error { |
| 102 | + #[allow(deprecated)] |
| 103 | + fn from(source: WireFormatError) -> Self { |
| 104 | + kernel::error::Error::from_errno(source.to_errno()) |
| 105 | + } |
| 106 | +} |
0 commit comments