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
//! Representations of errors returned by this crate.

use ssmarshal;

#[cfg(feature = "use_std")]
use std::io;

#[cfg(feature = "use_std")]
use std::result;
#[cfg(not(feature = "use_std"))]
use core::result;

/// Type alias for results from this crate.
pub type Result<T> = result::Result<T, Error>;

/// Errors from this crate.
#[derive(Debug)]
pub enum Error {
    /// COBS decode failed
    CobsDecodeFailed,

    /// COBS encode failed
    CobsEncodeFailed,

    /// Checksum error: the received frame was corrupted.
    ChecksumError,

    /// End of data while reading a frame; we received some of a frame
    /// but it was incomplete.
    EofDuringFrame,

    /// End of data before a frame started; we received none of a frame.
    EofBeforeFrame,

    /// Forwarded io::Error.
    #[cfg(feature = "use_std")]
    Io(io::Error),

    /// Forwarded ssmarshal::Error.
    Ssmarshal(ssmarshal::Error),
}

impl Error {
    /// Returns true if the error represents a corrupted frame. Data
    /// may have been lost but the decoder should decode the next
    /// frame correctly.
    pub fn is_corrupt_frame(&self) -> bool {
        match *self {
            Error::ChecksumError |
            Error::CobsDecodeFailed |
            Error::EofDuringFrame
                => true,
            _ => false,
        }
    }
}

#[cfg(feature = "use_std")]
impl From<io::Error> for Error {
    fn from(e: io::Error) -> Error {
        Error::Io(e)
    }
}

impl From<ssmarshal::Error> for Error {
    fn from(e: ssmarshal::Error) -> Error {
        Error::Ssmarshal(e)
    }
}