#[cfg(feature = "serde")] use serde::{Deserialize, Serialize}; /// A bare value or constraint specifying a sequence of accepted values. /// /// # W3C Spec Compliance /// /// There exists no direct corresponding type in the /// W3C ["Media Capture and Streams"][media_capture_and_streams_spec] spec, /// since the `BareOrValueConstraint` type aims to be a generalization over /// multiple types in the spec. /// /// | Rust | W3C | /// | ---------------------------------------- | -------------------------------------------- | /// | `BareOrValueSequenceConstraint` | [`ConstrainDOMString`][constrain_dom_string] | /// /// [constrain_dom_string]: https://www.w3.org/TR/mediacapture-streams/#dom-constraindomstring /// [media_capture_and_streams_spec]: https://www.w3.org/TR/mediacapture-streams/ #[derive(Debug, Clone, PartialEq)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] #[cfg_attr(feature = "serde", serde(untagged))] pub enum BareOrValueSequenceConstraint { Bare(Vec), Constraint(ValueSequenceConstraint), } impl Default for BareOrValueSequenceConstraint { fn default() -> Self { Self::Constraint(Default::default()) } } impl From for BareOrValueSequenceConstraint { fn from(bare: T) -> Self { Self::Bare(vec![bare]) } } impl From> for BareOrValueSequenceConstraint { fn from(bare: Vec) -> Self { Self::Bare(bare) } } impl From> for BareOrValueSequenceConstraint { fn from(constraint: ValueSequenceConstraint) -> Self { Self::Constraint(constraint) } } /// A constraint specifying a sequence of accepted values. /// /// # W3C Spec Compliance /// /// There exists no direct corresponding type in the /// W3C ["Media Capture and Streams"][media_capture_and_streams_spec] spec, /// since the `BareOrValueSequenceConstraint` type aims to be a /// generalization over multiple types in the W3C spec: /// /// | Rust | W3C | /// | --------------------------------- | ----------------------------------------------------------------- | /// | `ValueSequenceConstraint` | [`ConstrainDOMStringParameters`][constrain_dom_string_parameters] | /// /// [constrain_dom_string_parameters]: https://www.w3.org/TR/mediacapture-streams/#dom-constraindomstringparameters /// [media_capture_and_streams_spec]: https://www.w3.org/TR/mediacapture-streams/ #[derive(Debug, Clone, PartialEq)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] #[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))] pub struct ValueSequenceConstraint { // See https://developer.mozilla.org/en-US/docs/Web/API/MediaTrackConstraints#constraindomstring #[cfg_attr( feature = "serde", serde(skip_serializing_if = "core::option::Option::is_none") )] pub exact: Option>, #[cfg_attr( feature = "serde", serde(skip_serializing_if = "core::option::Option::is_none") )] pub ideal: Option>, } impl ValueSequenceConstraint { pub fn exact_only(exact: Vec) -> Self { Self { exact: Some(exact), ideal: None, } } pub fn ideal_only(ideal: Vec) -> Self { Self { exact: None, ideal: Some(ideal), } } pub fn is_required(&self) -> bool { self.exact.is_some() } } impl Default for ValueSequenceConstraint { fn default() -> Self { Self { exact: None, ideal: None, } } } #[cfg(feature = "serde")] #[cfg(test)] mod serde_tests { use crate::macros::test_serde_symmetry; use super::*; macro_rules! test_serde { ($t:ty => { values: [$($values:expr),*] }) => { type Subject = BareOrValueSequenceConstraint<$t>; #[test] fn default() { let subject = Subject::default(); let json = serde_json::json!({}); test_serde_symmetry!(subject: subject, json: json); } #[test] fn bare() { let subject = Subject::Bare(vec![$($values.to_owned()),*].into()); let json = serde_json::json!([$($values),*]); test_serde_symmetry!(subject: subject, json: json); } #[test] fn constraint() { let subject = Subject::Constraint(ValueSequenceConstraint:: { exact: Some(vec![$($values.to_owned()),*].into()), ideal: None, }); let json = serde_json::json!({ "exact": [$($values),*], }); test_serde_symmetry!(subject: subject, json: json); } }; } mod string { use super::*; test_serde!(String => { values: ["VALUE_0", "VALUE_1"] }); } }