midi_msg/
parse_error.rs

1use alloc::fmt;
2use core::error;
3
4/// Returned when [`MidiMsg::from_midi`](crate::MidiMsg::from_midi) and similar where not successful.
5#[derive(Debug, PartialEq, Clone)]
6pub enum ParseError {
7    /// The given input ended before a `MidiMsg` could be fully formed.
8    UnexpectedEnd,
9    /// Received a non-status byte with no prior channel messages.
10    ContextlessRunningStatus,
11    /// Reached end without an End of System Exclusive flag.
12    NoEndOfSystemExclusiveFlag,
13    /// Encountered an unexpected End of System Exclusive flag.
14    UnexpectedEndOfSystemExclusiveFlag,
15    /// Received a system exclusive message but the crate
16    /// was built without the sysex feature.
17    SystemExclusiveDisabled,
18    /// Received a meta event message but the crate
19    /// was built without the file feature.
20    FileDisabled,
21    /// The series of bytes was otherwise invalid.
22    Invalid(&'static str),
23    /// Attempted to use a not yet implemented feature.
24    NotImplemented(&'static str),
25    /// A byte exceeded 7 bits.
26    ByteOverflow,
27    /// A variable length quanity exceeded 4 bytes.
28    VlqOverflow,
29    /// Encountered an undefined system common message
30    UndefinedSystemCommonMessage(u8),
31    /// Encountered an undefined system real time message
32    UndefinedSystemRealTimeMessage(u8),
33    /// Encountered an undefined system exclusive message
34    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}