1use alloc::fmt;
2#[cfg(feature = "std")]
3use std::error;
4
5#[derive(Debug, PartialEq, Clone)]
7pub enum ParseError {
8 UnexpectedEnd,
10 ContextlessRunningStatus,
12 NoEndOfSystemExclusiveFlag,
14 UnexpectedEndOfSystemExclusiveFlag,
16 SystemExclusiveDisabled,
19 FileDisabled,
22 Invalid(&'static str),
24 NotImplemented(&'static str),
26 ByteOverflow,
28 VlqOverflow,
30 UndefinedSystemCommonMessage(u8),
32 UndefinedSystemRealTimeMessage(u8),
34 UndefinedSystemExclusiveMessage(Option<u8>),
36}
37
38#[cfg(feature = "std")]
39impl error::Error for ParseError {}
40
41impl fmt::Display for ParseError {
42 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
43 write!(f, "Error parsing MIDI input: ")?;
44 match self {
45 Self::UnexpectedEnd => {
46 write!(f, "The input ended before a MidiMsg could be fully formed")
47 }
48 Self::ContextlessRunningStatus => write!(
49 f,
50 "Received a non-status byte with no prior channel messages"
51 ),
52 Self::NoEndOfSystemExclusiveFlag => {
53 write!(f, "Tried to read a SystemExclusiveMsg, but reached the end without an End of System Exclusive flag")
54 }
55 Self::UnexpectedEndOfSystemExclusiveFlag => {
56 write!(f, "Encountered an unexpected End of System Exclusive flag")
57 }
58 Self::SystemExclusiveDisabled => {
59 write!(f, "Received a system exclusive message but the crate was built without the sysex feature")
60 }
61 Self::FileDisabled => {
62 write!(f, "Received a meta event message but the crate was built without the file feature")
63 }
64 Self::NotImplemented(msg) => {
65 write!(f, "{} is not yet implemented", msg)
66 }
67 Self::Invalid(s) => write!(f, "{}", s),
68 Self::ByteOverflow => write!(f, "A byte exceeded 7 bits"),
69 Self::VlqOverflow => write!(f, "A variable-length quantity exceeded 4 bytes"),
70 Self::UndefinedSystemCommonMessage(byte) => write!(
71 f,
72 "Encountered undefined system common message {:#04x}",
73 byte
74 ),
75 Self::UndefinedSystemRealTimeMessage(byte) => write!(
76 f,
77 "Encountered undefined system real time message {:#04x}",
78 byte
79 ),
80 Self::UndefinedSystemExclusiveMessage(byte) => {
81 if let Some(byte) = byte {
82 write!(
83 f,
84 "Encountered undefined system exclusive message {:#04x}",
85 byte
86 )
87 } else {
88 write!(
89 f,
90 "Encountered undefined system exclusive message {:?}",
91 byte
92 )
93 }
94 }
95 }
96 }
97}