hang/catalog/video/
stereo.rs1use std::str::FromStr;
2
3use derive_more::Display;
4
5#[derive(Debug, Clone, PartialEq, Eq, Display)]
10#[non_exhaustive]
11pub enum Stereo {
12 #[display("mono")]
14 Mono,
15
16 #[display("left-right")]
18 LeftRight,
19
20 #[display("top-bottom")]
22 TopBottom,
23
24 #[display("{_0}")]
26 Unknown(String),
27}
28
29impl FromStr for Stereo {
30 type Err = crate::Error;
31
32 fn from_str(s: &str) -> Result<Self, Self::Err> {
33 Ok(match s {
34 "mono" => Self::Mono,
35 "left-right" => Self::LeftRight,
36 "top-bottom" => Self::TopBottom,
37 _ => Self::Unknown(s.to_string()),
38 })
39 }
40}
41
42#[cfg(test)]
43mod test {
44 use crate::catalog::{VideoCodec, VideoConfig};
45
46 use super::*;
47
48 #[test]
49 fn round_trips() {
50 for (stereo, json) in [
51 (Stereo::Mono, "mono"),
52 (Stereo::LeftRight, "left-right"),
53 (Stereo::TopBottom, "top-bottom"),
54 (Stereo::Unknown("multiview".to_string()), "multiview"),
55 ] {
56 let mut config = VideoConfig::new(VideoCodec::VP8);
57 config.stereo = Some(stereo.clone());
58 let encoded = serde_json::to_value(&config).unwrap();
59 assert_eq!(encoded["stereo"], json);
60 assert_eq!(
61 serde_json::from_value::<VideoConfig>(encoded).unwrap().stereo,
62 Some(stereo)
63 );
64 }
65 }
66
67 #[test]
68 fn non_string_is_refused() {
69 let wire = serde_json::json!({ "codec": "vp8", "stereo": 2 });
70 serde_json::from_value::<VideoConfig>(wire).unwrap_err();
71 }
72}