Skip to main content

smpte_timecode/timecode/
errors.rs

1use core::fmt;
2
3const UNSUPPORTED_FRAME_RATE: &str = "unsupported frame rate";
4const INVALID_SYNTAX: &str = "timecode must be written as HH:MM:SS:FF";
5const DROP_FRAME_SEPARATOR: &str = "only a drop-frame rate takes a ';' frame separator";
6const FIELD_OUT_OF_RANGE: &str = "timecode field is out of range for the frame rate";
7const DROPPED_LABEL: &str = "drop-frame timecode skips this label";
8const FRAME_OUT_OF_RANGE: &str = "frame position is outside the 24-hour day";
9const ZERO_DENOMINATOR: &str = "ratio denominator is zero";
10const ZERO_SAMPLE_RATE: &str = "sample rate is zero";
11const OVERFLOW: &str = "arithmetic overflow";
12
13/// An error from building, parsing or converting a timecode.
14#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
15#[non_exhaustive]
16pub enum TimecodeError {
17    /// The nominal rate, 1000/1001 and drop-frame combination is not one of
18    /// [`FrameRate::ALL`](crate::FrameRate::ALL).
19    UnsupportedFrameRate,
20    /// The text is not `HH:MM:SS:FF` with two-digit fields and a two- or three-digit frame field.
21    InvalidSyntax,
22    /// A non-drop-frame timecode used the `;` frame separator.
23    DropFrameSeparator,
24    /// Hours exceed 23, minutes or seconds exceed 59, or frames reach the nominal rate.
25    FieldOutOfRange,
26    /// The label is one drop-frame counting skips, such as `00:01:00;00` at 29.97 DF.
27    DroppedLabel,
28    /// The frame position does not fall within one 24-hour day.
29    FrameOutOfRange,
30    /// A ratio was given a zero denominator.
31    ZeroDenominator,
32    /// A conversion from samples was given a zero sample rate.
33    ZeroSampleRate,
34    /// An intermediate or final value does not fit the result type.
35    Overflow,
36}
37
38impl TimecodeError {
39    const fn message(self) -> &'static str {
40        match self {
41            Self::UnsupportedFrameRate => UNSUPPORTED_FRAME_RATE,
42            Self::InvalidSyntax => INVALID_SYNTAX,
43            Self::DropFrameSeparator => DROP_FRAME_SEPARATOR,
44            Self::FieldOutOfRange => FIELD_OUT_OF_RANGE,
45            Self::DroppedLabel => DROPPED_LABEL,
46            Self::FrameOutOfRange => FRAME_OUT_OF_RANGE,
47            Self::ZeroDenominator => ZERO_DENOMINATOR,
48            Self::ZeroSampleRate => ZERO_SAMPLE_RATE,
49            Self::Overflow => OVERFLOW,
50        }
51    }
52}
53
54impl fmt::Display for TimecodeError {
55    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
56        f.write_str(self.message())
57    }
58}
59
60impl core::error::Error for TimecodeError {}