midi_msg/
parse_error.rs

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