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
use crate::constraint::ResolvedValueConstraint;
use super::{
setting::SettingFitnessDistanceError, FitnessDistance, SettingFitnessDistanceErrorKind,
};
// Standard implementation for value constraints of arbitrary `Setting` and `Constraint`
// types where `Setting: PartialEq<Constraint>`:
macro_rules! impl_non_numeric_value_constraint {
(setting: $s:ty, constraint: $c:ty) => {
impl<'a> FitnessDistance<Option<&'a $s>> for ResolvedValueConstraint<$c>
where
$s: PartialEq<$c>,
{
type Error = SettingFitnessDistanceError;
fn fitness_distance(&self, setting: Option<&'a $s>) -> Result<f64, Self::Error> {
if let Some(exact) = self.exact.as_ref() {
// 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 actual == exact => {}
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(ideal) = self.ideal.as_ref() {
match setting {
Some(actual) if actual == ideal => {
// As specified in step 8 of the `fitness distance` algorithm:
// <https://www.w3.org/TR/mediacapture-streams/#dfn-fitness-distance>
//
// > For all string, enum and boolean constraints […],
// > the fitness distance is the result of the formula:
// >
// > ```
// > (actual == ideal) ? 0 : 1
// > ```
Ok(0.0)
}
_ => {
// 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_non_numeric_value_constraint!(setting: bool, constraint: bool);
impl_non_numeric_value_constraint!(setting: String, constraint: String);
// Specialized implementations for floating-point value constraints (and settings):
macro_rules! impl_numeric_value_constraint {
(setting: $s:ty, constraint: $c:ty) => {
impl<'a> FitnessDistance<Option<&'a $s>> for ResolvedValueConstraint<$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(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_numeric_value_constraint!(setting: f64, constraint: f64);
impl_numeric_value_constraint!(setting: i64, constraint: u64);
impl_numeric_value_constraint!(setting: i64, constraint: f64);
impl_numeric_value_constraint!(setting: f64, constraint: u64);
// Specialized implementations for boolean value constraints of mis-matching
// and thus either "existence"-checked or ignored setting types:
macro_rules! impl_exists_value_constraint {
(settings: [$($s:ty),+], constraint: bool) => {
$(impl_exists_value_constraint!(setting: $s, constraint: bool);)+
};
(setting: $s:ty, constraint: bool) => {
impl<'a> FitnessDistance<Option<&'a $s>> for ResolvedValueConstraint<bool> {
type Error = SettingFitnessDistanceError;
fn fitness_distance(&self, setting: Option<&'a $s>) -> Result<f64, Self::Error> {
// A bare boolean value (as described in step 4 of the
// `fitness distance` algorithm) gets parsed as:
// ```
// ResolvedValueConstraint::<bool> {
// exact: Some(bare),
// ideal: None,
// }
// ```
//
// For all other configurations we just interpret it as an incompatible constraint.
match self.exact {
// As specified in step 4 of the `fitness distance` algorithm:
// <https://www.w3.org/TR/mediacapture-streams/#dfn-fitness-distance>
//
// > If constraintValue is a boolean, but the constrainable property is not,
// > then the fitness distance is based on whether the settings dictionary's
// > `constraintName` member exists or not, from the formula:
// >
// > ```
// > (constraintValue == exists) ? 0 : 1
// > ```
Some(expected) => {
if setting.is_some() == expected {
Ok(0.0)
} else {
Ok(1.0)
}
}
// 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).
None => Ok(0.0),
}
}
}
};
}
impl_exists_value_constraint!(settings: [String, i64, f64], constraint: bool);
#[cfg(test)]
mod tests;