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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
// Take a look at the license at the top of the repository in the LICENSE file.

use std::{cmp, fmt, mem, str};

use glib::translate::*;

glib::wrapper! {
    /// A representation of a difference between two [`VideoTimeCode`][crate::VideoTimeCode] instances.
    /// Will not necessarily correspond to a real timecode (e.g. 00:00:10;00)
    #[doc(alias = "GstVideoTimeCodeInterval")]
    pub struct VideoTimeCodeInterval(BoxedInline<ffi::GstVideoTimeCodeInterval>);

    match fn {
        type_ => || ffi::gst_video_time_code_interval_get_type(),
    }
}

impl VideoTimeCodeInterval {
    /// ## `hours`
    /// the hours field of [`VideoTimeCodeInterval`][crate::VideoTimeCodeInterval]
    /// ## `minutes`
    /// the minutes field of [`VideoTimeCodeInterval`][crate::VideoTimeCodeInterval]
    /// ## `seconds`
    /// the seconds field of [`VideoTimeCodeInterval`][crate::VideoTimeCodeInterval]
    /// ## `frames`
    /// the frames field of [`VideoTimeCodeInterval`][crate::VideoTimeCodeInterval]
    ///
    /// # Returns
    ///
    /// a new [`VideoTimeCodeInterval`][crate::VideoTimeCodeInterval] with the given values.
    pub fn new(hours: u32, minutes: u32, seconds: u32, frames: u32) -> Self {
        assert_initialized_main_thread!();
        unsafe {
            let mut v = mem::MaybeUninit::uninit();
            ffi::gst_video_time_code_interval_init(v.as_mut_ptr(), hours, minutes, seconds, frames);
            Self {
                inner: v.assume_init(),
            }
        }
    }

    #[doc(alias = "get_hours")]
    pub fn hours(&self) -> u32 {
        self.inner.hours
    }

    pub fn set_hours(&mut self, hours: u32) {
        self.inner.hours = hours
    }

    #[doc(alias = "get_minutes")]
    pub fn minutes(&self) -> u32 {
        self.inner.minutes
    }

    pub fn set_minutes(&mut self, minutes: u32) {
        assert!(minutes < 60);
        self.inner.minutes = minutes
    }

    #[doc(alias = "get_seconds")]
    pub fn seconds(&self) -> u32 {
        self.inner.seconds
    }

    pub fn set_seconds(&mut self, seconds: u32) {
        assert!(seconds < 60);
        self.inner.seconds = seconds
    }

    #[doc(alias = "get_frames")]
    pub fn frames(&self) -> u32 {
        self.inner.frames
    }

    pub fn set_frames(&mut self, frames: u32) {
        self.inner.frames = frames
    }
}

unsafe impl Send for VideoTimeCodeInterval {}
unsafe impl Sync for VideoTimeCodeInterval {}

impl PartialEq for VideoTimeCodeInterval {
    fn eq(&self, other: &Self) -> bool {
        self.inner.hours == other.inner.hours
            && self.inner.minutes == other.inner.minutes
            && self.inner.seconds == other.inner.seconds
            && self.inner.frames == other.inner.frames
    }
}

impl Eq for VideoTimeCodeInterval {}

impl PartialOrd for VideoTimeCodeInterval {
    #[inline]
    fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> {
        Some(self.cmp(other))
    }
}

impl Ord for VideoTimeCodeInterval {
    #[inline]
    fn cmp(&self, other: &Self) -> cmp::Ordering {
        self.inner
            .hours
            .cmp(&other.inner.hours)
            .then_with(|| self.inner.minutes.cmp(&other.inner.minutes))
            .then_with(|| self.inner.seconds.cmp(&other.inner.seconds))
            .then_with(|| self.inner.frames.cmp(&other.inner.frames))
    }
}

impl fmt::Debug for VideoTimeCodeInterval {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.debug_struct("VideoTimeCodeInterval")
            .field("hours", &self.inner.hours)
            .field("minutes", &self.inner.minutes)
            .field("seconds", &self.inner.seconds)
            .field("frames", &self.inner.frames)
            .finish()
    }
}

impl fmt::Display for VideoTimeCodeInterval {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(
            f,
            "{:02}:{:02}:{:02}:{:02}",
            self.inner.hours, self.inner.minutes, self.inner.seconds, self.inner.frames
        )
    }
}

impl str::FromStr for VideoTimeCodeInterval {
    type Err = glib::error::BoolError;

    #[doc(alias = "gst_video_time_code_interval_new_from_string")]
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        assert_initialized_main_thread!();
        unsafe {
            Option::<Self>::from_glib_full(ffi::gst_video_time_code_interval_new_from_string(
                s.to_glib_none().0,
            ))
            .ok_or_else(|| glib::bool_error!("Failed to create VideoTimeCodeInterval from string"))
        }
    }
}