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
use esp_idf_sys::{
    esp_err_t, ESP_ERR_INVALID_ARG, ESP_ERR_INVALID_CRC, ESP_ERR_INVALID_MAC,
    ESP_ERR_INVALID_RESPONSE, ESP_ERR_INVALID_SIZE, ESP_ERR_INVALID_STATE, ESP_ERR_INVALID_VERSION,
    ESP_ERR_NOT_FOUND, ESP_ERR_NOT_SUPPORTED, ESP_ERR_NO_MEM, ESP_ERR_TIMEOUT, ESP_OK,
};

pub struct EspError(pub esp_err_t);

impl EspError {
    pub fn into_result(self) -> Result<()> {
        Result::from(self)
    }
}

// TODO: implement Error
#[derive(Clone, Copy, Debug)]
pub enum Error {
    /// Out of memory
    NoMem,
    /// Invalid argument
    InvalidArg,
    /// Invalid state
    InvalidState,
    /// Invalid size
    InvalidSize,
    /// Requested resource not found
    NotFound,
    /// Operation or feature not supported
    NotSupported,
    /// Operation timed out
    Timeout,
    /// Received response was invalid
    InvalidResponse,
    /// CRC or checksum was invalid
    InvalidCrc,
    /// Version was invalid
    InvalidVersion,
    /// MAC address was invalid
    InvalidMac,

    Other(esp_err_t),
}

pub type Result<T, E = Error> = core::result::Result<T, E>;

impl From<EspError> for Result<()> {
    fn from(value: EspError) -> Self {
        use Error::*;

        Err(match value.0 as u32 {
            ESP_OK => return Ok(()),
            ESP_ERR_NO_MEM => NoMem,
            ESP_ERR_INVALID_ARG => InvalidArg,
            ESP_ERR_INVALID_STATE => InvalidState,
            ESP_ERR_INVALID_SIZE => InvalidSize,
            ESP_ERR_NOT_FOUND => NotFound,
            ESP_ERR_NOT_SUPPORTED => NotSupported,
            ESP_ERR_TIMEOUT => Timeout,
            ESP_ERR_INVALID_RESPONSE => InvalidResponse,
            ESP_ERR_INVALID_CRC => InvalidCrc,
            ESP_ERR_INVALID_VERSION => InvalidVersion,
            ESP_ERR_INVALID_MAC => InvalidMac,
            _ => Other(value.0),
        })
    }
}