artnet_protocol/
error.rs

1use std::ops::Range;
2
3/// The result that this crate uses
4pub type Result<T> = std::result::Result<T, Error>;
5
6/// All the possible errors this crate can encounter
7#[derive(Debug)]
8pub enum Error {
9    /// Could not read or write to the inner curso
10    CursorEof(std::io::Error),
11
12    /// Could not serialize an artnet command
13    SerializeError(&'static str, Box<Error>),
14
15    /// Could not deserialize an artnet command
16    DeserializeError(&'static str, Box<Error>),
17
18    /// The given message was too short
19    MessageTooShort {
20        /// The message that was being send or received
21        message: Vec<u8>,
22
23        /// The minimal length that is supported
24        min_len: usize,
25    },
26
27    /// The given message was too long or too short
28    MessageSizeInvalid {
29        /// The message that was being send or received
30        message: Vec<u8>,
31
32        /// The size that the artnet protocol expects
33        allowed_size: Range<usize>,
34    },
35
36    /// The artnet header is invalid
37    InvalidArtnetHeader(Vec<u8>),
38
39    /// Could not parse the given opcode
40    OpcodeError(&'static str, Box<Error>),
41
42    /// Unknown opcode ID
43    UnknownOpcode(u16),
44
45    /// The Art-Net PortAddress was not from 0 to 32_767
46    InvalidPortAddress(i32),
47
48    /// There was a problem matching to a known timecode frame type
49    InvalidTimecodeFrameType(u8),
50}
51
52impl std::fmt::Display for Error {
53    fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result {
54        match self {
55            Error::CursorEof(inner) => write!(fmt, "Cursor EOF: {}", inner),
56            Error::SerializeError(message, inner) => write!(fmt, "{}: {}", message, inner),
57            Error::DeserializeError(message, inner) => write!(fmt, "{}: {}", message, inner),
58            Error::MessageTooShort { message, min_len } => write!(
59                fmt,
60                "Message too short, it was {} but artnet expects at least {}",
61                message.len(),
62                min_len
63            ),
64            Error::MessageSizeInvalid {
65                message,
66                allowed_size,
67            } => write!(
68                fmt,
69                "Message size invalid, it was {} but artnet expects between {} and {}",
70                message.len(),
71                allowed_size.start,
72                allowed_size.end
73            ),
74            Error::InvalidArtnetHeader(_) => write!(fmt, "Invalid artnet header"),
75            Error::OpcodeError(opcode, inner) => {
76                write!(fmt, "Could not parse opcode {:?}: {}", opcode, inner)
77            }
78            Error::UnknownOpcode(opcode) => write!(fmt, "Unknown opcode 0x{:X}", opcode),
79            Error::InvalidPortAddress(wrong_number) => write!(
80                fmt,
81                "Art-Net PortAddress must be from 0 to 32_767. Got {:?}",
82                wrong_number
83            ),
84            Error::InvalidTimecodeFrameType(frame_type_number) => write!(
85                fmt,
86                "Timecode Frame Type should be 0 thru 3, received {}",
87                frame_type_number
88            ),
89        }
90    }
91}
92
93impl std::error::Error for Error {}