Skip to content

Commit

Permalink
impl std::error::Error for BLEError
Browse files Browse the repository at this point in the history
  • Loading branch information
taks committed Apr 30, 2024
1 parent 2086a0c commit fd787dc
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/ble_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,17 @@ impl BLEError {
}
}

impl core::fmt::Display for BLEError {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match return_code_to_string(self.0.get()) {
Some(text) => write!(f, "{text}")?,
None => write!(f, "0x{:X}", self.0)?,
};

Ok(())
}
}

impl core::fmt::Debug for BLEError {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match return_code_to_string(self.0.get()) {
Expand All @@ -42,6 +53,9 @@ impl core::fmt::Debug for BLEError {
}
}

#[cfg(feature = "std")]
impl std::error::Error for BLEError {}

pub fn return_code_to_string(rc: i32) -> Option<&'static str> {
let rc = rc as u32;
if rc < 0x100 {
Expand Down
6 changes: 6 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@
#![feature(decl_macro)]
#![feature(get_mut_unchecked)]
#![doc = include_str!("../README.md")]

#[cfg(feature = "std")]
#[allow(unused_imports)]
#[macro_use]
extern crate std;

extern crate alloc;

#[doc(hidden)]
Expand Down

0 comments on commit fd787dc

Please sign in to comment.