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