1 #[cfg(feature = "serde")] 2 use serde::{Deserialize, Serialize}; 3 4 /// A capability specifying a range of supported values. 5 /// 6 /// # W3C Spec Compliance 7 /// 8 /// Corresponds to [`sequence<T>`][sequence] from the W3C ["WebIDL"][webidl_spec] spec: 9 /// 10 /// | Rust | W3C | 11 /// | ----------------------------------------- | --------------------- | 12 /// | `MediaTrackValueSequenceCapability<bool>` | `sequence<boolean>` | 13 /// | `MediaTrackValueSequenceCapability<String>` | `sequence<DOMString>` | 14 /// 15 /// [sequence]: https://webidl.spec.whatwg.org/#idl-sequence 16 /// [webidl_spec]: https://webidl.spec.whatwg.org/ 17 #[derive(Default, Debug, Clone, Eq, PartialEq)] 18 #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] 19 #[cfg_attr(feature = "serde", serde(transparent))] 20 pub struct MediaTrackValueSequenceCapability<T> { 21 pub values: Vec<T>, 22 } 23 24 impl<T> From<T> for MediaTrackValueSequenceCapability<T> { from(value: T) -> Self25 fn from(value: T) -> Self { 26 Self { 27 values: vec![value], 28 } 29 } 30 } 31 32 impl<T> From<Vec<T>> for MediaTrackValueSequenceCapability<T> { from(values: Vec<T>) -> Self33 fn from(values: Vec<T>) -> Self { 34 Self { values } 35 } 36 } 37 38 #[cfg(test)] 39 mod tests { 40 use super::*; 41 42 type Subject = MediaTrackValueSequenceCapability<String>; 43 44 #[test] default()45 fn default() { 46 let subject = Subject::default(); 47 48 let actual = subject.values; 49 50 let expected: Vec<String> = vec![]; 51 52 assert_eq!(actual, expected); 53 } 54 55 mod from { 56 use super::*; 57 58 #[test] value()59 fn value() { 60 let subject = Subject::from("foo".to_owned()); 61 62 let actual = subject.values; 63 64 let expected: Vec<String> = vec!["foo".to_owned()]; 65 66 assert_eq!(actual, expected); 67 } 68 69 #[test] values()70 fn values() { 71 let subject = Subject::from(vec!["foo".to_owned(), "bar".to_owned()]); 72 73 let actual = subject.values; 74 75 let expected: Vec<String> = vec!["foo".to_owned(), "bar".to_owned()]; 76 77 assert_eq!(actual, expected); 78 } 79 } 80 } 81 82 #[cfg(feature = "serde")] 83 #[cfg(test)] 84 mod serde_tests { 85 use crate::macros::test_serde_symmetry; 86 87 use super::*; 88 89 type Subject = MediaTrackValueSequenceCapability<String>; 90 91 #[test] customized()92 fn customized() { 93 let subject = Subject { 94 values: vec!["foo".to_owned(), "bar".to_owned()], 95 }; 96 let json = serde_json::json!(["foo", "bar"]); 97 98 test_serde_symmetry!(subject: subject, json: json); 99 } 100 } 101