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
use std::fmt::Debug;

use derive_builder::Builder;

mod facing_mode;
mod resize_mode;

pub use facing_mode::*;
pub use resize_mode::*;

pub type Width = u32;
pub type Height = u32;
pub type AspectRatio = f64;
pub type FrameRate = f64;

/// A video's settings
#[derive(PartialEq, Default, Clone, Builder)]
pub struct Video {
    #[builder(default, setter(strip_option))]
    pub width: Option<Width>,
    #[builder(default, setter(strip_option))]
    pub height: Option<Height>,
    #[builder(default, setter(strip_option))]
    pub aspect_ratio: Option<AspectRatio>,
    #[builder(default, setter(strip_option))]
    pub frame_rate: Option<FrameRate>,
    #[builder(default, setter(strip_option))]
    pub facing_mode: Option<FacingMode>,
    #[builder(default, setter(strip_option))]
    pub resize_mode: Option<ResizeMode>,
}

impl Video {
    pub fn builder() -> VideoBuilder {
        Default::default()
    }

    pub fn new(
        width: Option<Width>,
        height: Option<Height>,
        aspect_ratio: Option<AspectRatio>,
        frame_rate: Option<FrameRate>,
        facing_mode: Option<FacingMode>,
        resize_mode: Option<ResizeMode>,
    ) -> Self {
        Self {
            width,
            height,
            aspect_ratio,
            frame_rate,
            facing_mode,
            resize_mode,
        }
    }
}

impl Debug for Video {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let mut builder = f.debug_struct("Video");

        if let Some(width) = self.width {
            builder.field("width", &width);
        }
        if let Some(height) = self.height {
            builder.field("height", &height);
        }
        if let Some(aspect_ratio) = self.aspect_ratio {
            builder.field("aspect_ratio", &aspect_ratio);
        }
        if let Some(frame_rate) = self.frame_rate {
            builder.field("frame_rate", &frame_rate);
        }
        if let Some(facing_mode) = self.facing_mode {
            builder.field("facing_mode", &facing_mode);
        }
        if let Some(resize_mode) = self.resize_mode {
            builder.field("resize_mode", &resize_mode);
        }

        builder.finish()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn default() {
        let subject = Video::default();
        assert_eq!(
            subject,
            Video {
                width: None,
                height: None,
                aspect_ratio: None,
                frame_rate: None,
                facing_mode: None,
                resize_mode: None,
            }
        );
    }

    #[test]
    fn builder() {
        let subject = Video::builder()
            .height(42)
            .frame_rate(30.0)
            .resize_mode(ResizeMode::CropAndScale)
            .build()
            .unwrap();
        assert_eq!(
            subject,
            Video {
                width: None,
                height: Some(42),
                aspect_ratio: None,
                frame_rate: Some(30.0),
                facing_mode: None,
                resize_mode: Some(ResizeMode::CropAndScale),
            }
        );
    }

    #[test]
    fn debug() {
        let subject = Video {
            width: None,
            height: Some(42),
            aspect_ratio: None,
            frame_rate: Some(30.0),
            facing_mode: None,
            resize_mode: Some(ResizeMode::CropAndScale),
        };
        assert_eq!(
            format!("{:?}", subject),
            "Video { height: 42, frame_rate: 30.0, resize_mode: CropAndScale }"
        );
    }

    #[test]
    fn debug_empty() {
        let subject = Video::default();
        assert_eq!(format!("{:?}", subject), "Video");
    }
}