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
use crate::ResolvedValueRangeConstraint;
use super::{
setting::SettingFitnessDistanceError, FitnessDistance, SettingFitnessDistanceErrorKind,
};
macro_rules! impl_value_range_constraint {
(setting: $s:ty, constraint: $c:ty) => {
impl<'a> FitnessDistance<Option<&'a $s>> for ResolvedValueRangeConstraint<$c> {
type Error = SettingFitnessDistanceError;
fn fitness_distance(&self, setting: Option<&'a $s>) -> Result<f64, Self::Error> {
if let Some(exact) = self.exact {
// As specified in step 2 of the `fitness distance` algorithm:
// <https://www.w3.org/TR/mediacapture-streams/#dfn-fitness-distance>
//
// > If the constraint is required (constraintValue either contains
// > one or more members named […] 'exact' […]), and the settings
// > dictionary's constraintName member's value does not satisfy the
// > constraint or doesn't exist, the fitness distance is positive infinity.
match setting {
Some(&actual) if super::is_nearly_equal_to(actual as f64, exact as f64) => {
}
Some(setting) => {
return Err(SettingFitnessDistanceError {
kind: SettingFitnessDistanceErrorKind::Mismatch,
constraint: format!("{}", self.to_required_only()),
setting: Some(format!("{:?}", setting)),
})
}
None => {
return Err(SettingFitnessDistanceError {
kind: SettingFitnessDistanceErrorKind::Missing,
constraint: format!("{}", self.to_required_only()),
setting: None,
})
}
};
}
if let Some(min) = self.min {
// As specified in step 2 of the `fitness distance` algorithm:
// <https://www.w3.org/TR/mediacapture-streams/#dfn-fitness-distance>
//
// > If the constraint is required (constraintValue either contains
// > one or more members named […] 'min' […]), and the settings
// > dictionary's constraintName member's value does not satisfy the
// > constraint or doesn't exist, the fitness distance is positive infinity.
match setting {
Some(&actual)
if super::is_nearly_greater_than_or_equal_to(
actual as f64,
min as f64,
) => {}
Some(setting) => {
return Err(SettingFitnessDistanceError {
kind: SettingFitnessDistanceErrorKind::TooSmall,
constraint: format!("{}", self.to_required_only()),
setting: Some(format!("{:?}", setting)),
})
}
None => {
return Err(SettingFitnessDistanceError {
kind: SettingFitnessDistanceErrorKind::Missing,
constraint: format!("{}", self.to_required_only()),
setting: None,
})
}
};
}
if let Some(max) = self.max {
// As specified in step 2 of the `fitness distance` algorithm:
// <https://www.w3.org/TR/mediacapture-streams/#dfn-fitness-distance>
//
// > If the constraint is required (constraintValue either contains
// > one or more members named […] 'max' […]), and the settings
// > dictionary's constraintName member's value does not satisfy the
// > constraint or doesn't exist, the fitness distance is positive infinity.
match setting {
Some(&actual)
if super::is_nearly_less_than_or_equal_to(
actual as f64,
max as f64,
) => {}
Some(setting) => {
return Err(SettingFitnessDistanceError {
kind: SettingFitnessDistanceErrorKind::TooLarge,
constraint: format!("{}", self.to_required_only()),
setting: Some(format!("{:?}", setting)),
})
}
None => {
return Err(SettingFitnessDistanceError {
kind: SettingFitnessDistanceErrorKind::Missing,
constraint: format!("{}", self.to_required_only()),
setting: None,
})
}
};
}
if let Some(ideal) = self.ideal {
match setting {
Some(&actual) => {
let actual: f64 = actual as f64;
let ideal: f64 = ideal as f64;
// As specified in step 7 of the `fitness distance` algorithm:
// <https://www.w3.org/TR/mediacapture-streams/#dfn-fitness-distance>
//
// > For all positive numeric constraints […],
// > the fitness distance is the result of the formula
// >
// > ```
// > (actual == ideal) ? 0 : |actual - ideal| / max(|actual|, |ideal|)
// > ```
Ok(super::relative_fitness_distance(actual, ideal))
}
None => {
// As specified in step 5 of the `fitness distance` algorithm:
// <https://www.w3.org/TR/mediacapture-streams/#dfn-fitness-distance>
//
// > If the settings dictionary's `constraintName` member
// > does not exist, the fitness distance is 1.
Ok(1.0)
}
}
} else {
// As specified in step 6 of the `fitness distance` algorithm:
// <https://www.w3.org/TR/mediacapture-streams/#dfn-fitness-distance>
//
// > If no ideal value is specified (constraintValue either
// > contains no member named 'ideal', or, if bare values are to be
// > treated as 'ideal', isn't a bare value), the fitness distance is 0.
Ok(0.0)
}
}
}
};
}
impl_value_range_constraint!(setting: f64, constraint: f64);
impl_value_range_constraint!(setting: i64, constraint: u64);
impl_value_range_constraint!(setting: i64, constraint: f64);
impl_value_range_constraint!(setting: f64, constraint: u64);
// Specialized implementations for non-boolean value constraints of mis-matching,
// and thus ignored setting types:
macro_rules! impl_ignored_value_range_constraint {
(settings: [$($s:ty),+], constraint: $c:ty) => {
$(impl_ignored_value_range_constraint!(setting: $s, constraint: $c);)+
};
(setting: $s:ty, constraint: $c:ty) => {
impl<'a> FitnessDistance<Option<&'a $s>> for ResolvedValueRangeConstraint<$c> {
type Error = SettingFitnessDistanceError;
fn fitness_distance(&self, _setting: Option<&'a $s>) -> Result<f64, Self::Error> {
// As specified in step 3 of the `fitness distance` algorithm:
// <https://www.w3.org/TR/mediacapture-streams/#dfn-fitness-distance>
//
// > If the constraint does not apply for this type of object,
// > the fitness distance is 0 (that is, the constraint does not
// > influence the fitness distance).
Ok(0.0)
}
}
};
}
impl_ignored_value_range_constraint!(settings: [bool, String], constraint: u64);
impl_ignored_value_range_constraint!(settings: [bool, String], constraint: f64);
#[cfg(test)]
mod tests;