timecode/
fraction.rs

1use super::TimecodeFrames;
2use serde::{Deserialize, Serialize};
3use std::fmt::Display;
4
5#[derive(Debug, Deserialize, Clone, PartialEq, Serialize)]
6pub enum Fraction {
7  Frames(TimecodeFrames),
8  MilliSeconds(u16),
9}
10
11impl Fraction {
12  pub fn number_of_digits(&self) -> usize {
13    match self {
14      Self::Frames(timecode_frame) => timecode_frame.number_of_digits(),
15      Self::MilliSeconds(_) => 3,
16    }
17  }
18
19  pub fn separator(&self) -> char {
20    match self {
21      Self::Frames(timecode_frame) => timecode_frame.separator(),
22      Self::MilliSeconds(_) => '.',
23    }
24  }
25}
26
27impl Display for Fraction {
28  fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
29    let str = match self {
30      Self::Frames(timecode_frame) => timecode_frame.to_string(),
31      Self::MilliSeconds(milliseconds) => format!(".{:03}", milliseconds),
32    };
33    write!(f, "{}", str)
34  }
35}