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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

use crate::MediaTrackConstraintResolutionStrategy;

/// A bare value or constraint specifying a range 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                                   |
/// | ---------------------------------- | ------------------------------------- |
/// | `ValueRangeConstraint<u64>` | [`ConstrainULong`][constrain_ulong]   |
/// | `ValueRangeConstraint<f64>` | [`ConstrainDouble`][constrain_double] |
///
/// [constrain_double]: https://www.w3.org/TR/mediacapture-streams/#dom-constraindouble
/// [constrain_ulong]: https://www.w3.org/TR/mediacapture-streams/#dom-constrainulong
/// [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 ValueRangeConstraint<T> {
    /// A bare-valued media track constraint.
    Bare(T),
    /// A fully-qualified media track constraint.
    Constraint(ResolvedValueRangeConstraint<T>),
}

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

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

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

impl<T> ValueRangeConstraint<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,
    ) -> ResolvedValueRangeConstraint<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,
    ) -> ResolvedValueRangeConstraint<T> {
        match self {
            Self::Bare(bare) => match strategy {
                MediaTrackConstraintResolutionStrategy::BareToIdeal => {
                    ResolvedValueRangeConstraint::default().ideal(bare)
                }
                MediaTrackConstraintResolutionStrategy::BareToExact => {
                    ResolvedValueRangeConstraint::default().exact(bare)
                }
            },
            Self::Constraint(constraint) => constraint,
        }
    }
}

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

/// A constraint specifying a range of accepted values.
///
/// Corresponding W3C spec types as per ["Media Capture and Streams"][spec]:
/// - `ConstrainDouble` => `ResolvedValueRangeConstraint<f64>`
/// - `ConstrainULong` => `ResolvedValueRangeConstraint<u64>`
///
/// [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 ResolvedValueRangeConstraint<T> {
    /// The minimum legal value of this property.
    ///
    /// This is a required value.
    #[cfg_attr(
        feature = "serde",
        serde(skip_serializing_if = "core::option::Option::is_none")
    )]
    pub min: Option<T>,
    /// The maximum legal value of this property.
    ///
    /// This is a required value.
    #[cfg_attr(
        feature = "serde",
        serde(skip_serializing_if = "core::option::Option::is_none")
    )]
    pub max: Option<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<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<T>,
}

impl<T> ResolvedValueRangeConstraint<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<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<T>: From<U>,
    {
        self.ideal = ideal.into();
        self
    }

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

    /// Consumes `self`, returning a corresponding constraint
    /// with the maximum required value set to `max`.
    #[inline]
    pub fn max<U>(mut self, max: U) -> Self
    where
        Option<T>: From<U>,
    {
        self.max = max.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.min.is_some() || self.max.is_some() || 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 {
        self.min.is_none() && self.max.is_none() && self.exact.is_none() && self.ideal.is_none()
    }

    /// 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 {
            min: self.min,
            max: self.max,
            exact: self.exact,
            ideal: None,
        }
    }
}

impl<T> Default for ResolvedValueRangeConstraint<T> {
    #[inline]
    fn default() -> Self {
        Self {
            min: None,
            max: None,
            exact: None,
            ideal: None,
        }
    }
}

impl<T> std::fmt::Display for ResolvedValueRangeConstraint<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(exact) = &self.exact {
            f.write_fmt(format_args!("x == {:?}", exact))?;
            is_first = false;
        } else if let (Some(min), Some(max)) = (&self.min, &self.max) {
            f.write_fmt(format_args!("{:?} <= x <= {:?}", min, max))?;
            is_first = false;
        } else if let Some(min) = &self.min {
            f.write_fmt(format_args!("{:?} <= x", min))?;
            is_first = false;
        } else if let Some(max) = &self.max {
            f.write_fmt(format_args!("x <= {:?}", max))?;
            is_first = false;
        }
        if let Some(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 = [
            (ResolvedValueRangeConstraint::default(), "(<empty>)"),
            (ResolvedValueRangeConstraint::default().exact(1), "(x == 1)"),
            (ResolvedValueRangeConstraint::default().ideal(2), "(x ~= 2)"),
            (
                ResolvedValueRangeConstraint::default().exact(1).ideal(2),
                "(x == 1 && x ~= 2)",
            ),
        ];

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

            assert_eq!(actual, expected);
        }
    }

    #[test]
    fn is_required() {
        for min_is_some in [false, true] {
            // TODO: Replace `if { Some(_) } else { None }` with `.then_some(_)`
            // once MSRV has passed 1.62.0:
            let min = if min_is_some { Some(1) } else { None };
            for max_is_some in [false, true] {
                // TODO: Replace `if { Some(_) } else { None }` with `.then_some(_)`
                // once MSRV has passed 1.62.0:
                let max = if max_is_some { Some(2) } else { None };
                for exact_is_some in [false, true] {
                    // TODO: Replace `if { Some(_) } else { None }` with `.then_some(_)`
                    // once MSRV has passed 1.62.0:
                    let exact = if exact_is_some { Some(3) } else { None };
                    for ideal_is_some in [false, true] {
                        // TODO: Replace `if { Some(_) } else { None }` with `.then_some(_)`
                        // once MSRV has passed 1.62.0:
                        let ideal = if ideal_is_some { Some(4) } else { None };

                        let constraint = ResolvedValueRangeConstraint::<u64> {
                            min,
                            max,
                            exact,
                            ideal,
                        };

                        let actual = constraint.is_required();
                        let expected = min_is_some || max_is_some || exact_is_some;

                        assert_eq!(actual, expected);
                    }
                }
            }
        }
    }

    mod is_empty {
        use super::*;

        #[test]
        fn bare() {
            let constraint = ValueRangeConstraint::Bare(42);

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

        #[test]
        fn constraint() {
            for min_is_some in [false, true] {
                // TODO: Replace `if { Some(_) } else { None }` with `.then_some(_)`
                // once MSRV has passed 1.62.0:
                let min = if min_is_some { Some(1) } else { None };
                for max_is_some in [false, true] {
                    // TODO: Replace `if { Some(_) } else { None }` with `.then_some(_)`
                    // once MSRV has passed 1.62.0:
                    let max = if max_is_some { Some(2) } else { None };
                    for exact_is_some in [false, true] {
                        // TODO: Replace `if { Some(_) } else { None }` with `.then_some(_)`
                        // once MSRV has passed 1.62.0:
                        let exact = if exact_is_some { Some(3) } else { None };
                        for ideal_is_some in [false, true] {
                            // TODO: Replace `if { Some(_) } else { None }` with `.then_some(_)`
                            // once MSRV has passed 1.62.0:
                            let ideal = if ideal_is_some { Some(4) } else { None };

                            let constraint = ResolvedValueRangeConstraint::<u64> {
                                min,
                                max,
                                exact,
                                ideal,
                            };

                            let actual = constraint.is_empty();
                            let expected =
                                !(min_is_some || max_is_some || exact_is_some || ideal_is_some);

                            assert_eq!(actual, expected);
                        }
                    }
                }
            }
        }
    }
}

#[test]
fn resolve_to_advanced() {
    let constraints = [
        ValueRangeConstraint::Bare(42),
        ValueRangeConstraint::Constraint(ResolvedValueRangeConstraint::default().exact(42)),
    ];
    let strategy = MediaTrackConstraintResolutionStrategy::BareToExact;

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

        let expected = ResolvedValueRangeConstraint::default().exact(42);

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

#[test]
fn resolve_to_basic() {
    let constraints = [
        ValueRangeConstraint::Bare(42),
        ValueRangeConstraint::Constraint(ResolvedValueRangeConstraint::default().ideal(42)),
    ];
    let strategy = MediaTrackConstraintResolutionStrategy::BareToIdeal;

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

        let expected = ResolvedValueRangeConstraint::default().ideal(42);

        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 => {
            value: $value:expr
        }) => {
            type Subject = ValueRangeConstraint<$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($value.to_owned());
                let json = serde_json::json!($value);

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

            #[test]
            fn min_constraint() {
                let subject = Subject::Constraint(ResolvedValueRangeConstraint::default().min($value.to_owned()));
                let json = serde_json::json!({
                    "min": $value,
                });

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

            #[test]
            fn max_constraint() {
                let subject = Subject::Constraint(ResolvedValueRangeConstraint::default().max($value.to_owned()));
                let json = serde_json::json!({
                    "max": $value,
                });

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

            #[test]
            fn exact_constraint() {
                let subject = Subject::Constraint(ResolvedValueRangeConstraint::default().exact($value.to_owned()));
                let json = serde_json::json!({
                    "exact": $value,
                });

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

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

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

            #[test]
            fn full_constraint() {
                let subject = Subject::Constraint(ResolvedValueRangeConstraint::default().min($value.to_owned()).max($value.to_owned()).exact($value.to_owned()).ideal($value.to_owned()));
                let json = serde_json::json!({
                    "min": $value,
                    "max": $value,
                    "exact": $value,
                    "ideal": $value,
                });

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

    mod f64 {
        use super::*;

        test_serde!(f64 => {
            value: 42.0
        });
    }

    mod u64 {
        use super::*;

        test_serde!(u64 => {
            value: 42
        });
    }
}