File
Blob: archive/fft-benchmark/firmware/crates/esp32-radio/src/error.rs
| 1 | //! Small diagnostic errors: no request bodies, credentials, or SDP in messages. |
| 2 | use std::fmt; |
| 3 | |
| 4 | #[derive(Debug, Clone, Copy)] |
| 5 | pub(crate) struct Error { |
| 6 | context: &'static str, |
| 7 | code: Option<i32>, |
| 8 | } |
| 9 | pub(crate) type Result<T> = std::result::Result<T, Error>; |
| 10 | |
| 11 | impl Error { |
| 12 | pub(crate) fn new(context: &'static str) -> Self { |
| 13 | Self { |
| 14 | context, |
| 15 | code: None, |
| 16 | } |
| 17 | } |
| 18 | pub(crate) fn native(context: &'static str, code: i32) -> Self { |
| 19 | Self { |
| 20 | context, |
| 21 | code: Some(code), |
| 22 | } |
| 23 | } |
| 24 | } |
| 25 | impl fmt::Display for Error { |
| 26 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 27 | f.write_str(self.context)?; |
| 28 | if let Some(code) = self.code { |
| 29 | write!(f, " (code {code})")?; |
| 30 | } |
| 31 | Ok(()) |
| 32 | } |
| 33 | } |
| 34 | impl std::error::Error for Error {} |
| 35 | impl From<&'static str> for Error { |
| 36 | fn from(value: &'static str) -> Self { |
| 37 | Self::new(value) |
| 38 | } |
| 39 | } |
| 40 | pub(crate) fn check(code: i32, context: &'static str) -> Result<()> { |
| 41 | if code == 0 { |
| 42 | Ok(()) |
| 43 | } else { |
| 44 | Err(Error::native(context, code)) |
| 45 | } |
| 46 | } |