webrtc-constraints 0.1.0

A pure Rust implementation of WebRTC Media Constraints API
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

use crate::MediaTrackConstraintResolutionStrategy;

/// 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 `ValueConstraint<T>` type aims to be a generalization over
/// multiple types in the spec.
///
/// | Rust                                     | W3C                                          |
/// | ---------------------------------------- | -------------------------------------------- |
/// | `ValueSequenceConstraint<String>` | [`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, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde", serde(untagged))]
pub enum ValueSequenceConstraint<T> {
    /// A bare-valued media track constraint.
    Bare(Vec<T>),
    /// A fully-qualified media track constraint.
    Constraint(ResolvedValueSequenceConstraint<T>),
}

impl<T> Default for ValueSequenceConstraint<T> {
    fn default() -> Self {
        Self::Constraint(Default::default())
    }
}

impl<T> From<T> for ValueSequenceConstraint<T> {
    fn from(bare: T) -> Self {
        Self::Bare(vec![bare])
    }
}

impl<T> From<Vec<T>> for ValueSequenceConstraint<T> {
    fn from(bare: Vec<T>) -> Self {
        Self::Bare(bare)
    }
}

impl<T> From<ResolvedValueSequenceConstraint<T>> for ValueSequenceConstraint<T> {
    fn from(constraint: ResolvedValueSequenceConstraint<T>) -> Self {
        Self::Constraint(constraint)
    }
}

impl<T> ValueSequenceConstraint<T>
where
    T: Clone,
{
    /// Returns a resolved representation of the constraint
    /// with bare values resolved to fully-qualified constraints.
    pub fn to_resolved(
        &self,
        strategy: MediaTrackConstraintResolutionStrategy,
    ) -> ResolvedValueSequenceConstraint<T> {
        self.clone().into_resolved(strategy)
    }

    /// Consumes the constraint, returning a resolved representation of the
    /// constraint with bare values resolved to fully-qualified constraints.
    pub fn into_resolved(
        self,
        strategy: MediaTrackConstraintResolutionStrategy,
    ) -> ResolvedValueSequenceConstraint<T> {
        match self {
            Self::Bare(bare) => match strategy {
                MediaTrackConstraintResolutionStrategy::BareToIdeal => {
                    ResolvedValueSequenceConstraint::default().ideal(bare)
                }
                MediaTrackConstraintResolutionStrategy::BareToExact => {
                    ResolvedValueSequenceConstraint::default().exact(bare)
                }
            },
            Self::Constraint(constraint) => constraint,
        }
    }
}

impl<T> ValueSequenceConstraint<T> {
    /// Returns `true` if `self` is empty, otherwise `false`.
    pub fn is_empty(&self) -> bool {
        match self {
            Self::Bare(bare) => bare.is_empty(),
            Self::Constraint(constraint) => constraint.is_empty(),
        }
    }
}

/// 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 `ValueSequenceConstraint<T>` type aims to be a
/// generalization over multiple types in the W3C spec:
///
/// | Rust                              | W3C                                                               |
/// | --------------------------------- | ----------------------------------------------------------------- |
/// | `ResolvedValueSequenceConstraint<String>` | [`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, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))]
pub struct ResolvedValueSequenceConstraint<T> {
    /// The exact required value for this property.
    ///
    /// This is a required value.
    #[cfg_attr(
        feature = "serde",
        serde(skip_serializing_if = "core::option::Option::is_none")
    )]
    pub exact: Option<Vec<T>>,
    /// The ideal (target) value for this property.
    ///
    /// This is an optional value.
    #[cfg_attr(
        feature = "serde",
        serde(skip_serializing_if = "core::option::Option::is_none")
    )]
    pub ideal: Option<Vec<T>>,
}

impl<T> ResolvedValueSequenceConstraint<T> {
    /// Consumes `self`, returning a corresponding constraint
    /// with the exact required value set to `exact`.
    #[inline]
    pub fn exact<U>(mut self, exact: U) -> Self
    where
        Option<Vec<T>>: From<U>,
    {
        self.exact = exact.into();
        self
    }

    /// Consumes `self`, returning a corresponding constraint
    /// with the ideal required value set to `ideal`.
    #[inline]
    pub fn ideal<U>(mut self, ideal: U) -> Self
    where
        Option<Vec<T>>: From<U>,
    {
        self.ideal = ideal.into();
        self
    }

    /// Returns `true` if `value.is_some()` is `true` for any of its required values,
    /// otherwise `false`.
    pub fn is_required(&self) -> bool {
        self.exact.is_some()
    }

    /// Returns `true` if `value.is_none()` is `true` for all of its values,
    /// otherwise `false`.
    pub fn is_empty(&self) -> bool {
        let exact_is_empty = self.exact.as_ref().map_or(true, Vec::is_empty);
        let ideal_is_empty = self.ideal.as_ref().map_or(true, Vec::is_empty);
        exact_is_empty && ideal_is_empty
    }

    /// Returns a corresponding constraint containing only required values.
    pub fn to_required_only(&self) -> Self
    where
        T: Clone,
    {
        self.clone().into_required_only()
    }

    /// Consumes `self, returning a corresponding constraint
    /// containing only required values.
    pub fn into_required_only(self) -> Self {
        Self {
            exact: self.exact,
            ideal: None,
        }
    }
}

impl<T> Default for ResolvedValueSequenceConstraint<T> {
    fn default() -> Self {
        Self {
            exact: None,
            ideal: None,
        }
    }
}

impl<T> std::fmt::Display for ResolvedValueSequenceConstraint<T>
where
    T: std::fmt::Debug,
{
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let mut is_first = true;
        f.write_str("(")?;
        if let Some(ref exact) = &self.exact {
            f.write_fmt(format_args!("x == {:?}", exact))?;
            is_first = false;
        }
        if let Some(ref ideal) = &self.ideal {
            if !is_first {
                f.write_str(" && ")?;
            }
            f.write_fmt(format_args!("x ~= {:?}", ideal))?;
            is_first = false;
        }
        if is_first {
            f.write_str("<empty>")?;
        }
        f.write_str(")")?;
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn to_string() {
        let scenarios = [
            (ResolvedValueSequenceConstraint::default(), "(<empty>)"),
            (
                ResolvedValueSequenceConstraint::default().exact(vec![1, 2]),
                "(x == [1, 2])",
            ),
            (
                ResolvedValueSequenceConstraint::default().ideal(vec![2, 3]),
                "(x ~= [2, 3])",
            ),
            (
                ResolvedValueSequenceConstraint::default()
                    .exact(vec![1, 2])
                    .ideal(vec![2, 3]),
                "(x == [1, 2] && x ~= [2, 3])",
            ),
        ];

        for (constraint, expected) in scenarios {
            let actual = constraint.to_string();

            assert_eq!(actual, expected);
        }
    }

    #[test]
    fn is_required() {
        let scenarios = [
            (ResolvedValueSequenceConstraint::default(), false),
            (
                ResolvedValueSequenceConstraint::default().exact(vec![true]),
                true,
            ),
            (
                ResolvedValueSequenceConstraint::default().ideal(vec![true]),
                false,
            ),
            (
                ResolvedValueSequenceConstraint::default()
                    .exact(vec![true])
                    .ideal(vec![true]),
                true,
            ),
        ];

        for (constraint, expected) in scenarios {
            let actual = constraint.is_required();

            assert_eq!(actual, expected);
        }
    }

    mod is_empty {
        use super::*;

        #[test]
        fn bare() {
            let constraint = ValueSequenceConstraint::Bare(vec![true]);

            assert!(!constraint.is_empty());
        }

        #[test]
        fn constraint() {
            let scenarios = [
                (ResolvedValueSequenceConstraint::default(), true),
                (
                    ResolvedValueSequenceConstraint::default().exact(vec![true]),
                    false,
                ),
                (
                    ResolvedValueSequenceConstraint::default().ideal(vec![true]),
                    false,
                ),
                (
                    ResolvedValueSequenceConstraint::default()
                        .exact(vec![true])
                        .ideal(vec![true]),
                    false,
                ),
            ];

            for (constraint, expected) in scenarios {
                let constraint = ValueSequenceConstraint::<bool>::Constraint(constraint);

                let actual = constraint.is_empty();

                assert_eq!(actual, expected);
            }
        }
    }

    #[test]
    fn resolve_to_advanced() {
        let constraints = [
            ValueSequenceConstraint::Bare(vec![true]),
            ValueSequenceConstraint::Constraint(
                ResolvedValueSequenceConstraint::default().exact(vec![true]),
            ),
        ];
        let strategy = MediaTrackConstraintResolutionStrategy::BareToExact;

        for constraint in constraints {
            let actuals = [
                constraint.to_resolved(strategy),
                constraint.into_resolved(strategy),
            ];

            let expected = ResolvedValueSequenceConstraint::default().exact(vec![true]);

            for actual in actuals {
                assert_eq!(actual, expected);
            }
        }
    }

    #[test]
    fn resolve_to_basic() {
        let constraints = [
            ValueSequenceConstraint::Bare(vec![true]),
            ValueSequenceConstraint::Constraint(
                ResolvedValueSequenceConstraint::default().ideal(vec![true]),
            ),
        ];
        let strategy = MediaTrackConstraintResolutionStrategy::BareToIdeal;

        for constraint in constraints {
            let actuals = [
                constraint.to_resolved(strategy),
                constraint.into_resolved(strategy),
            ];

            let expected = ResolvedValueSequenceConstraint::default().ideal(vec![true]);

            for actual in actuals {
                assert_eq!(actual, expected);
            }
        }
    }
}

#[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 = ValueSequenceConstraint<$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 exact_constraint() {
                let subject = Subject::Constraint(ResolvedValueSequenceConstraint::default().exact(vec![$($values.to_owned()),*]));
                let json = serde_json::json!({
                    "exact": [$($values),*],
                });

                test_serde_symmetry!(subject: subject, json: json);
            }

            #[test]
            fn ideal_constraint() {
                let subject = Subject::Constraint(ResolvedValueSequenceConstraint::default().ideal(vec![$($values.to_owned()),*]));
                let json = serde_json::json!({
                    "ideal": [$($values),*],
                });

                test_serde_symmetry!(subject: subject, json: json);
            }

            #[test]
            fn full_constraint() {
                let subject = Subject::Constraint(ResolvedValueSequenceConstraint::default().exact(vec![$($values.to_owned()),*]).ideal(vec![$($values.to_owned()),*]));
                let json = serde_json::json!({
                    "exact": [$($values),*],
                    "ideal": [$($values),*],
                });

                test_serde_symmetry!(subject: subject, json: json);
            }
        };
    }

    mod string {
        use super::*;

        test_serde!(String => {
            values: ["VALUE_0", "VALUE_1"]
        });
    }
}