xref: /webrtc/constraints/src/capability/value.rs (revision cebcf132)
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> {
from(value: T) -> Self27     fn from(value: T) -> Self {
28         Self { value }
29     }
30 }
31 
32 impl From<&str> for MediaTrackValueCapability<String> {
from(value: &str) -> Self33     fn from(value: &str) -> Self {
34         Self {
35             value: value.to_owned(),
36         }
37     }
38 }
39 
40 #[cfg(test)]
41 mod tests {
42     use super::*;
43 
44     type Subject = MediaTrackValueCapability<String>;
45 
46     #[test]
from_str()47     fn from_str() {
48         let subject = Subject::from("string");
49 
50         let actual = subject.value.as_str();
51         let expected = "string";
52 
53         assert_eq!(actual, expected);
54     }
55 }
56 
57 #[cfg(feature = "serde")]
58 #[cfg(test)]
59 mod serde_tests {
60     use crate::macros::test_serde_symmetry;
61 
62     use super::*;
63 
64     type Subject = MediaTrackValueCapability<String>;
65 
66     #[test]
customized()67     fn customized() {
68         let subject = Subject {
69             value: "string".to_owned(),
70         };
71         let json = serde_json::json!("string");
72 
73         test_serde_symmetry!(subject: subject, json: json);
74     }
75 }
76