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
//! Media track settings.

use std::fmt::Debug;

use derive_builder::Builder;

pub mod audio;
pub mod video;

#[derive(PartialEq, Clone)]
pub enum MediaKind {
    Audio(audio::Audio),
    Video(video::Video),
}

impl From<audio::Audio> for MediaKind {
    fn from(audio: audio::Audio) -> Self {
        Self::Audio(audio)
    }
}

impl From<video::Video> for MediaKind {
    fn from(video: video::Video) -> Self {
        Self::Video(video)
    }
}

impl Debug for MediaKind {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Audio(audio) => audio.fmt(f),
            Self::Video(video) => video.fmt(f),
        }
    }
}

#[derive(PartialEq, Clone, Builder)]
pub struct Media {
    #[builder(default, setter(strip_option))]
    pub device_id: Option<String>,
    #[builder(default, setter(strip_option))]
    pub group_id: Option<String>,
    #[builder(setter(into))]
    pub kind: MediaKind,
}

impl Media {
    pub fn builder() -> MediaBuilder {
        Default::default()
    }

    pub fn new(device_id: Option<String>, group_id: Option<String>, kind: MediaKind) -> Self {
        Self {
            device_id,
            group_id,
            kind,
        }
    }
}

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

        if let Some(device_id) = &self.device_id {
            builder.field("device_id", &device_id);
        }
        if let Some(group_id) = &self.group_id {
            builder.field("group_id", &group_id);
        }

        builder.field("kind", &self.kind);

        builder.finish()
    }
}

#[cfg(test)]
mod tests {
    use crate::track::setting::video::Video;

    use super::*;

    #[test]
    fn builder() {
        let subject = Media::builder()
            .device_id("DEVICE".to_owned())
            .kind(Video::default())
            .build()
            .unwrap();
        assert_eq!(
            subject,
            Media {
                device_id: Some("DEVICE".to_owned()),
                group_id: None,
                kind: MediaKind::Video(Video::default())
            }
        );
    }

    #[test]
    fn debug() {
        let subject = Media {
            device_id: Some("DEVICE".to_owned()),
            group_id: None,
            kind: MediaKind::Video(Video {
                width: Some(42),
                height: None,
                aspect_ratio: None,
                frame_rate: None,
                facing_mode: None,
                resize_mode: None,
            }),
        };
        assert_eq!(
            format!("{:?}", subject),
            "Media { device_id: \"DEVICE\", kind: Video { width: 42 } }"
        );
    }
}