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
extern crate serialport;

use std::error::Error as StdError;
use std::fmt;

// Differents file which should be linked
pub mod communicator;
pub mod eep;
pub mod enocean;

/// Custom Result type = std::result::Result<T, ParseEspError>
type ParseEspResult<T> = std::result::Result<T, ParseEspError>;

/// Custom error type (eg. allow to see corresponding packet / byte index )
#[derive(Debug, Clone)]
pub struct ParseEspError {
    /// ErrorKind
    pub kind: ParseEspErrorKind,
    /// Associated message
    pub message: String,
    /// Index of the byte which caused the error
    pub byte_index: Option<i16>,
    /// Packet which caused this error
    pub packet: Vec<u8>,
}
/// Kind of error
#[derive(Debug, Clone, PartialEq)]
pub enum ParseEspErrorKind {
    NoSyncByte,
    CrcMismatch,
    IncompleteMessage,
    Unimplemented,
}

impl fmt::Display for ParseEspError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self.byte_index {
            // Error chould occur on a specific byte
            Some(bi) => write!(
                f,
                "{:?} error :{} in {:x?} at index {}",
                self.kind, self.message, self.packet, bi
            ),
            // Or on whole packet
            _ => write!(
                f,
                "{:?} error :{} in packet {:x?}",
                self.kind, self.message, self.packet
            ),
        }
    }
}
impl StdError for ParseEspError {
    fn description(&self) -> &str {
        &self.message
    }
}

/// Working with the type EnoceanMessage is more explicit than u8 vector.
type EnoceanMessage = Vec<u8>;