Skip to main content

smpte_timecode/mtc/
errors.rs

1use core::fmt;
2
3use crate::TimecodeError;
4
5const MALFORMED_MESSAGE: &str = "malformed MIDI Time Code message";
6const INVALID_TIMECODE: &str = "MIDI Time Code carries an invalid timecode";
7const NO_RATE_CODE: &str = "frame rate has no MIDI Time Code rate code";
8
9/// An error from decoding or encoding MIDI Time Code.
10#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
11#[non_exhaustive]
12pub enum MtcError {
13    /// A quarter-frame or full-frame message has the wrong length, framing or a data byte with
14    /// its high bit set.
15    MalformedMessage,
16    /// A complete message names a position that does not exist at its frame rate, such as hour 24
17    /// or a skipped drop-frame label.
18    InvalidTimecode(TimecodeError),
19    /// MIDI Time Code has rate codes only for 24, 25 and 30 frame labels per second, so 48 and
20    /// faster rates cannot be sent.
21    NoRateCode,
22}
23
24impl fmt::Display for MtcError {
25    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
26        f.write_str(match self {
27            Self::MalformedMessage => MALFORMED_MESSAGE,
28            Self::InvalidTimecode(_) => INVALID_TIMECODE,
29            Self::NoRateCode => NO_RATE_CODE,
30        })
31    }
32}
33
34impl core::error::Error for MtcError {
35    fn source(&self) -> Option<&(dyn core::error::Error + 'static)> {
36        match self {
37            Self::InvalidTimecode(error) => Some(error),
38            Self::MalformedMessage | Self::NoRateCode => None,
39        }
40    }
41}