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
use crate::ResolvedValueSequenceConstraint;
use super::{
setting::SettingFitnessDistanceError, FitnessDistance, SettingFitnessDistanceErrorKind,
};
macro_rules! impl_non_numeric_value_sequence_constraint {
(setting: $s:ty, constraint: $c:ty) => {
impl<'a> FitnessDistance<Option<&'a $s>> for ResolvedValueSequenceConstraint<$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 exact.contains(actual) => {}
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() {
// 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
// > ```
//
// As well as 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.
match setting {
Some(actual) if ideal.contains(actual) => Ok(0.0),
Some(_) => Ok(1.0),
None => 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_sequence_constraint!(setting: bool, constraint: bool);
impl_non_numeric_value_sequence_constraint!(setting: String, constraint: String);
macro_rules! impl_numeric_value_sequence_constraint {
(setting: $s:ty, constraint: $c:ty) => {
impl<'a> FitnessDistance<Option<&'a $s>> for ResolvedValueSequenceConstraint<$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 exact.contains(&(actual as $c)) => {}
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 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
// > ```
//
// As well as 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.
match setting {
Some(&actual) => {
let actual: f64 = actual as f64;
let mut min_fitness_distance = 1.0;
for ideal in ideal.into_iter() {
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|)
// > ```
let fitness_distance =
super::relative_fitness_distance(actual, ideal);
if fitness_distance < min_fitness_distance {
min_fitness_distance = fitness_distance;
}
}
Ok(min_fitness_distance)
}
None => 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_sequence_constraint!(setting: f64, constraint: f64);
impl_numeric_value_sequence_constraint!(setting: i64, constraint: u64);
impl_numeric_value_sequence_constraint!(setting: i64, constraint: f64);
impl_numeric_value_sequence_constraint!(setting: f64, constraint: u64);
#[cfg(test)]
mod tests;