1use core::fmt;
2
3use crate::tl::Kind;
4
5pub type Result<T> = core::result::Result<T, Error>;
7
8#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
13#[non_exhaustive]
14pub enum Error {
15 UnexpectedEnd,
17 InvalidTl(u8),
20 UnexpectedType {
22 expected: Kind,
24 found: Kind,
26 },
27 ListLength {
29 expected: usize,
31 found: usize,
33 },
34 InvalidLength,
37 ValueOutOfRange,
39 DepthLimit,
41 MissingEndOfMessage,
43 ObisLength(usize),
45 UnknownChoice {
47 what: &'static str,
49 choice: u64,
51 },
52 MessageCrc {
54 computed: u16,
56 found: u16,
58 },
59 FrameCrc {
61 computed: u16,
63 found: u16,
65 },
66 InvalidPadding,
69 InvalidEscape([u8; 4]),
71 UnexpectedStart,
74 BufferFull,
76 OutputTooSmall,
78 NoReadings,
80 ObisSyntax,
82 InvalidFile,
85}
86
87impl fmt::Display for Error {
88 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
89 match self {
90 Self::UnexpectedEnd => f.write_str("input ended in the middle of a value"),
91 Self::InvalidTl(b) => write!(f, "malformed type-length byte 0x{b:02x}"),
92 Self::UnexpectedType { expected, found } => {
93 write!(f, "expected {expected:?}, found {found:?}")
94 }
95 Self::ListLength { expected, found } => {
96 write!(f, "expected a list of {expected}, found {found} elements")
97 }
98 Self::InvalidLength => f.write_str("length out of range for its type"),
99 Self::ValueOutOfRange => f.write_str("value does not fit its field"),
100 Self::DepthLimit => f.write_str("lists nested too deeply"),
101 Self::MissingEndOfMessage => f.write_str("end-of-message byte missing"),
102 Self::ObisLength(n) => write!(f, "object name has {n} bytes, an OBIS code has 6"),
103 Self::UnknownChoice { what, choice } => write!(f, "unknown {what} choice {choice}"),
104 Self::MessageCrc { computed, found } => {
105 write!(f, "message CRC 0x{found:04x} does not match computed 0x{computed:04x}")
106 }
107 Self::FrameCrc { computed, found } => {
108 write!(f, "frame CRC 0x{found:04x} does not match computed 0x{computed:04x}")
109 }
110 Self::InvalidPadding => f.write_str("invalid padding in the end sequence"),
111 Self::InvalidEscape(c) => write!(f, "unknown escape command {c:02x?}"),
112 Self::UnexpectedStart => f.write_str("new start sequence inside a frame"),
113 Self::BufferFull => f.write_str("frame larger than the decoder buffer"),
114 Self::OutputTooSmall => f.write_str("output buffer too small"),
115 Self::NoReadings => f.write_str("the frame holds no GetListResponse"),
116 Self::ObisSyntax => f.write_str("not an OBIS code (expected A-B:C.D.E*F, A-B:C.D.E or C.D.E)"),
117 Self::InvalidFile => {
118 f.write_str("an SML file opens first, closes last and has at most 256 messages")
119 }
120 }
121 }
122}
123
124impl core::error::Error for Error {}
126
127#[cfg(test)]
128mod tests {
129 extern crate std;
130 use super::*;
131 use std::string::ToString;
132
133 #[test]
134 fn errors_say_what_is_wrong() {
135 assert_eq!(
136 Error::FrameCrc { computed: 0x1234, found: 0xabcd }.to_string(),
137 "frame CRC 0xabcd does not match computed 0x1234"
138 );
139 assert_eq!(
140 Error::UnknownChoice { what: "SML_Time", choice: 9 }.to_string(),
141 "unknown SML_Time choice 9"
142 );
143 assert_eq!(Error::NoReadings.to_string(), "the frame holds no GetListResponse");
144 assert_eq!(
145 Error::InvalidFile.to_string(),
146 "an SML file opens first, closes last and has at most 256 messages"
147 );
148 let as_trait: &dyn core::error::Error = &Error::DepthLimit;
149 assert_eq!(as_trait.to_string(), "lists nested too deeply");
150 }
151}