anpp/
errors.rs

1//! All error types used in this crate.
2
3use core::fmt::{self, Display, Formatter};
4#[cfg(feature = "std")]
5use std::error::Error;
6
7/// There wasn't enough space left to complete the operation.
8#[derive(Debug, Copy, Clone, PartialEq, Eq)]
9pub struct InsufficientCapacity {
10    /// The amount of space that was required.
11    pub required: usize,
12    /// The actual amount of space left.
13    pub actual: usize,
14}
15
16#[cfg(feature = "std")]
17impl Error for InsufficientCapacity {
18    fn description(&self) -> &str { "Insufficient Capacity" }
19}
20
21impl Display for InsufficientCapacity {
22    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
23        write!(
24            f,
25            "Insufficient capacity. At least {} bytes were required but only found {}",
26            self.required, self.actual
27        )
28    }
29}
30
31/// An error encountered while decoding.
32#[derive(Debug, Copy, Clone, PartialEq, Eq)]
33pub enum DecodeError {
34    /// More data is required.
35    RequiresMoreData,
36    /// A packet was found, but it had an invalid CRC.
37    InvalidCRC,
38}
39
40impl DecodeError {
41    fn msg(&self) -> &str {
42        match *self {
43            DecodeError::RequiresMoreData => "More data required",
44            DecodeError::InvalidCRC => "Invalid CRC",
45        }
46    }
47}
48
49#[cfg(feature = "std")]
50impl Error for DecodeError {
51    fn description(&self) -> &str { self.msg() }
52}
53
54impl Display for DecodeError {
55    fn fmt(&self, f: &mut Formatter) -> fmt::Result { self.msg().fmt(f) }
56}
57
58/// An error encountered while encoding a `Packet`.
59#[derive(Debug, Copy, Clone, PartialEq, Eq)]
60pub enum EncodeError {
61    /// The buffer wasn't big enough.
62    InsufficientCapacity(InsufficientCapacity),
63    /// The packet was empty.
64    EmptyPacket(EmptyPacket),
65}
66
67#[cfg(feature = "std")]
68impl Error for EncodeError {
69    fn description(&self) -> &str {
70        match *self {
71            EncodeError::InsufficientCapacity(ref i) => i.description(),
72            EncodeError::EmptyPacket(ref e) => e.description(),
73        }
74    }
75}
76
77impl Display for EncodeError {
78    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
79        match *self {
80            EncodeError::InsufficientCapacity(i) => i.fmt(f),
81            EncodeError::EmptyPacket(e) => e.fmt(f),
82        }
83    }
84}
85
86impl From<InsufficientCapacity> for EncodeError {
87    fn from(other: InsufficientCapacity) -> EncodeError {
88        EncodeError::InsufficientCapacity(other)
89    }
90}
91
92impl From<EmptyPacket> for EncodeError {
93    fn from(other: EmptyPacket) -> EncodeError {
94        EncodeError::EmptyPacket(other)
95    }
96}
97
98/// The packet was empty.
99///
100/// All ANPP packets must contain at least one byte of data in order to be
101/// transmitted.
102#[derive(Debug, Copy, Clone, PartialEq, Eq)]
103pub struct EmptyPacket;
104
105impl EmptyPacket {
106    fn msg(&self) -> &str { "Cannot encode an empty packet" }
107}
108
109#[cfg(feature = "std")]
110impl Error for EmptyPacket {
111    fn description(&self) -> &str { self.msg() }
112}
113
114impl Display for EmptyPacket {
115    fn fmt(&self, f: &mut Formatter) -> fmt::Result { self.msg().fmt(f) }
116}