1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
use core::{ffi, fmt, num::NonZeroI32, slice, str};
use crate::{esp_err_t, esp_err_to_name, ESP_OK};
#[repr(transparent)]
#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
pub struct EspError(NonZeroI32);
const _: () = if ESP_OK != 0 {
panic!("ESP_OK *has* to be 0")
};
impl EspError {
pub const fn from(error: esp_err_t) -> Option<Self> {
match NonZeroI32::new(error) {
None => None,
Some(err) => Some(Self(err)),
}
}
pub const fn from_non_zero(error: NonZeroI32) -> Self {
Self(error)
}
pub const fn from_infallible<const E: esp_err_t>() -> Self {
struct Dummy<const D: esp_err_t>;
impl<const D: esp_err_t> Dummy<D> {
pub const ERR: EspError = match EspError::from(D) {
Some(err) => err,
None => panic!("ESP_OK can't be an error"),
};
}
Dummy::<E>::ERR
}
pub fn check_and_return<T>(error: esp_err_t, value: T) -> Result<T, Self> {
match NonZeroI32::new(error) {
None => Ok(value),
Some(err) => Err(Self(err)),
}
}
pub fn convert(error: esp_err_t) -> Result<(), Self> {
EspError::check_and_return(error, ())
}
#[track_caller]
pub fn panic(&self) {
panic!("ESP-IDF ERROR: {self}");
}
pub fn code(&self) -> esp_err_t {
self.0.get()
}
}
#[cfg(feature = "std")]
impl std::error::Error for EspError {}
impl fmt::Display for EspError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
unsafe fn strlen(c_s: *const ffi::c_char) -> usize {
let mut len = 0;
while *c_s.offset(len) != 0 {
len += 1;
}
len as usize
}
unsafe {
let c_s = esp_err_to_name(self.code());
str::from_utf8_unchecked(slice::from_raw_parts(c_s as *const u8, strlen(c_s))).fmt(f)
}
}
}
#[macro_export]
macro_rules! esp {
($err:expr) => {{
$crate::EspError::convert($err as $crate::esp_err_t)
}};
}
#[macro_export]
macro_rules! esp_result {
($err:expr, $value:expr) => {{
$crate::EspError::check_and_return($err as $crate::esp_err_t, $value)
}};
}
#[macro_export]
macro_rules! esp_nofail {
($err:expr) => {{
if let ::core::option::Option::Some(error) =
$crate::EspError::from($err as $crate::esp_err_t)
{
error.panic();
}
}};
}