xref: /webrtc/constraints/src/capability/value.rs (revision 2dfc7374)
1 #[cfg(feature = "serde")]
2 use serde::{Deserialize, Serialize};
3 
4 /// A capability specifying a single supported value.
5 ///
6 /// # W3C Spec Compliance
7 ///
8 /// There exists no direct corresponding type in the
9 /// W3C ["Media Capture and Streams"][media_capture_and_streams_spec] spec,
10 /// since the `MediaTrackValueCapability<T>` type aims to be a
11 /// generalization over multiple types in the W3C spec:
12 ///
13 /// | Rust                                | W3C                       |
14 /// | ----------------------------------- | ------------------------- |
15 /// | `MediaTrackValueCapability<String>` | [`DOMString`][dom_string] |
16 ///
17 /// [dom_string]: https://webidl.spec.whatwg.org/#idl-DOMString
18 /// [media_capture_and_streams_spec]: https://www.w3.org/TR/mediacapture-streams/
19 #[derive(Debug, Clone, Eq, PartialEq)]
20 #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
21 #[cfg_attr(feature = "serde", serde(transparent))]
22 pub struct MediaTrackValueCapability<T> {
23     pub value: T,
24 }
25 
26 impl<T> From<T> for MediaTrackValueCapability<T> {
27     fn from(value: T) -> Self {
28         Self { value }
29     }
30 }
31 
32 impl From<&str> for MediaTrackValueCapability<String> {
33     fn from(value: &str) -> Self {
34         Self {
35             value: value.to_owned(),
36         }
37     }
38 }
39 
40 #[cfg(feature = "serde")]
41 #[cfg(test)]
42 mod serde_tests {
43     use crate::macros::test_serde_symmetry;
44 
45     use super::*;
46 
47     type Subject = MediaTrackValueCapability<i64>;
48 
49     #[test]
50     fn customized() {
51         let subject = Subject { value: 42 };
52         let json = serde_json::json!(42);
53 
54         test_serde_symmetry!(subject: subject, json: json);
55     }
56 }
57