Skip to main content

hang/catalog/video/
stereo.rs

1use std::str::FromStr;
2
3use derive_more::Display;
4
5/// How a rendition packs each eye's view into its frames.
6///
7/// Absent means [`Mono`](Self::Mono). An unrecognized value decodes to
8/// [`Stereo::Unknown`] instead of failing, and a consumer treats it as absent.
9#[derive(Debug, Clone, PartialEq, Eq, Display)]
10#[non_exhaustive]
11pub enum Stereo {
12	/// One view, shown to both eyes.
13	#[display("mono")]
14	Mono,
15
16	/// The left eye's view in the left half of each frame, the right eye's in the right half.
17	#[display("left-right")]
18	LeftRight,
19
20	/// The left eye's view in the top half of each frame, the right eye's in the bottom half.
21	#[display("top-bottom")]
22	TopBottom,
23
24	/// A packing this build does not recognize, preserved verbatim.
25	#[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}