1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
pub use frame_rate::FrameRate;
use std::fmt::Display;

use serde::{Deserialize, Serialize};

#[derive(Debug, Deserialize, Clone, PartialEq, Serialize)]
pub struct TimecodeFrames {
  frame_rate: FrameRate,
  number_of_frames: u8,
  drop_frame: bool,
  color_frame: bool,
}

impl TimecodeFrames {
  pub fn new(
    frame_rate: FrameRate,
    number_of_frames: u8,
    drop_frame: bool,
    color_frame: bool,
  ) -> Self {
    Self {
      frame_rate,
      number_of_frames,
      drop_frame,
      color_frame,
    }
  }

  pub fn frame_rate(&self) -> FrameRate {
    self.frame_rate
  }

  pub fn number_of_frames(&self) -> u8 {
    self.number_of_frames
  }

  pub fn drop_frame(&self) -> bool {
    self.drop_frame
  }

  pub fn color_frame(&self) -> bool {
    self.color_frame
  }

  pub fn number_of_digits(&self) -> usize {
    2
  }

  pub fn separator(&self) -> char {
    if self.drop_frame() {
      ';'
    } else {
      ':'
    }
  }
}

impl Display for TimecodeFrames {
  fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
    let separator = if self.drop_frame() { ';' } else { ':' };

    write!(f, "{}{:02}", separator, self.number_of_frames())
  }
}