|
| 1 | +use std::ops::Deref; |
| 2 | + |
| 3 | +/// A set of modifiers. |
| 4 | +/// |
| 5 | +/// Beware: The [`Eq`] and [`Hash`] implementations are dependent on the |
| 6 | +/// ordering of the modifiers, in opposition to what a set would usually |
| 7 | +/// constitute. To test for set-wise equality, use [`iter`](Self::iter) and |
| 8 | +/// collect into a true set type like [`HashSet`](std::collections::HashSet). |
| 9 | +#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] |
| 10 | +pub struct ModifierSet<S>( |
| 11 | + // Note: the visibility needs to be `pub(crate)`, since build.rs outputs |
| 12 | + // `ModifierSet(...)`. |
| 13 | + pub(crate) S, |
| 14 | +); |
| 15 | + |
| 16 | +impl<S: Deref<Target = str>> ModifierSet<S> { |
| 17 | + /// Constructs a modifier set from a string, where modifiers are separated |
| 18 | + /// by the character `.`. |
| 19 | + /// |
| 20 | + /// `s` should not contain any empty modifiers (i.e. it shouldn't contain |
| 21 | + /// the sequence `..`) and no modifier should occur twice. Otherwise, |
| 22 | + /// unexpected errors can occur. |
| 23 | + pub fn from_raw_dotted(s: S) -> Self { |
| 24 | + // Checking the other requirement too feels like it would be a bit too |
| 25 | + // expensive, even for debug mode. |
| 26 | + debug_assert!( |
| 27 | + !s.contains(".."), |
| 28 | + "ModifierSet::from_dotted called with string containing empty modifier" |
| 29 | + ); |
| 30 | + Self(s) |
| 31 | + } |
| 32 | + |
| 33 | + /// Whether `self` is empty. |
| 34 | + pub fn is_empty(&self) -> bool { |
| 35 | + self.0.is_empty() |
| 36 | + } |
| 37 | + |
| 38 | + /// Gets the string of modifiers separated by `.`. |
| 39 | + pub fn as_str(&self) -> &str { |
| 40 | + &self.0 |
| 41 | + } |
| 42 | + |
| 43 | + /// Converts the underlying string to a slice. |
| 44 | + pub fn as_deref(&self) -> ModifierSet<&str> { |
| 45 | + ModifierSet(&self.0) |
| 46 | + } |
| 47 | + |
| 48 | + /// Inserts a new modifier into the set. |
| 49 | + /// |
| 50 | + /// `m` should not be empty, contain the character `.`, or already be in the |
| 51 | + /// set. Otherwise, unexpected errors can occur. |
| 52 | + pub fn insert_raw(&mut self, m: &str) |
| 53 | + where |
| 54 | + S: for<'a> std::ops::AddAssign<&'a str>, |
| 55 | + { |
| 56 | + if !self.0.is_empty() { |
| 57 | + self.0 += "."; |
| 58 | + } |
| 59 | + self.0 += m; |
| 60 | + } |
| 61 | + |
| 62 | + /// Iterates over the list of modifiers in an arbitrary order. |
| 63 | + pub fn iter(&self) -> impl Iterator<Item = &str> { |
| 64 | + self.into_iter() |
| 65 | + } |
| 66 | + |
| 67 | + /// Whether the set contains the modifier `m`. |
| 68 | + pub fn contains(&self, m: &str) -> bool { |
| 69 | + self.iter().any(|lhs| lhs == m) |
| 70 | + } |
| 71 | + |
| 72 | + /// Finds the best match from the list. |
| 73 | + /// |
| 74 | + /// To be considered a match, the modifier set must be a superset of (or |
| 75 | + /// equal to) `self`. Among different matches, the best one is selected by |
| 76 | + /// the following two criteria (in order): |
| 77 | + /// 1. Number of modifiers in common with `self` (more is better). |
| 78 | + /// 2. Total number of modifiers (fewer is better). |
| 79 | + /// |
| 80 | + /// If there are multiple best matches, the first of them is returned. |
| 81 | + pub fn best_match_in<'a, T>( |
| 82 | + &self, |
| 83 | + variants: impl Iterator<Item = (ModifierSet<&'a str>, T)>, |
| 84 | + ) -> Option<T> { |
| 85 | + let mut best = None; |
| 86 | + let mut best_score = None; |
| 87 | + |
| 88 | + // Find the best table entry with this name. |
| 89 | + for candidate in variants.filter(|(set, _)| self.is_subset(*set)) { |
| 90 | + let mut matching = 0; |
| 91 | + let mut total = 0; |
| 92 | + for modifier in candidate.0.iter() { |
| 93 | + if self.contains(modifier) { |
| 94 | + matching += 1; |
| 95 | + } |
| 96 | + total += 1; |
| 97 | + } |
| 98 | + |
| 99 | + let score = (matching, std::cmp::Reverse(total)); |
| 100 | + if best_score.is_none_or(|b| score > b) { |
| 101 | + best = Some(candidate.1); |
| 102 | + best_score = Some(score); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + best |
| 107 | + } |
| 108 | + |
| 109 | + /// Whether all modifiers in `self` are also present in `other`. |
| 110 | + pub fn is_subset(&self, other: ModifierSet<&str>) -> bool { |
| 111 | + self.iter().all(|m| other.contains(m)) |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +impl<S: Default> Default for ModifierSet<S> { |
| 116 | + /// Constructs the default modifier set. |
| 117 | + /// |
| 118 | + /// This is typically the empty set, though the remark from |
| 119 | + /// [`Self::from_raw_dotted`] applies since `S::default()` could technically |
| 120 | + /// be anything. |
| 121 | + fn default() -> Self { |
| 122 | + Self(S::default()) |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +impl<'a, S: Deref<Target = str>> IntoIterator for &'a ModifierSet<S> { |
| 127 | + type Item = &'a str; |
| 128 | + type IntoIter = std::str::Split<'a, char>; |
| 129 | + |
| 130 | + /// Iterate over the list of modifiers in an arbitrary order. |
| 131 | + fn into_iter(self) -> Self::IntoIter { |
| 132 | + let mut iter = self.0.split('.'); |
| 133 | + if self.0.is_empty() { |
| 134 | + // Empty the iterator |
| 135 | + let _ = iter.next(); |
| 136 | + } |
| 137 | + iter |
| 138 | + } |
| 139 | +} |
| 140 | + |
| 141 | +impl<'a> IntoIterator for ModifierSet<&'a str> { |
| 142 | + type Item = &'a str; |
| 143 | + type IntoIter = std::str::Split<'a, char>; |
| 144 | + |
| 145 | + /// Iterate over the list of modifiers in an arbitrary order. |
| 146 | + fn into_iter(self) -> Self::IntoIter { |
| 147 | + let mut iter = self.0.split('.'); |
| 148 | + if self.0.is_empty() { |
| 149 | + // Empty the iterator |
| 150 | + let _ = iter.next(); |
| 151 | + } |
| 152 | + iter |
| 153 | + } |
| 154 | +} |
| 155 | + |
| 156 | +#[cfg(test)] |
| 157 | +mod tests { |
| 158 | + type ModifierSet = super::ModifierSet<&'static str>; |
| 159 | + |
| 160 | + #[test] |
| 161 | + fn default_is_empty() { |
| 162 | + assert!(ModifierSet::default().is_empty()); |
| 163 | + } |
| 164 | + |
| 165 | + #[test] |
| 166 | + fn iter_count() { |
| 167 | + assert_eq!(ModifierSet::default().iter().count(), 0); |
| 168 | + assert_eq!(ModifierSet::from_raw_dotted("a").iter().count(), 1); |
| 169 | + assert_eq!(ModifierSet::from_raw_dotted("a.b").iter().count(), 2); |
| 170 | + assert_eq!(ModifierSet::from_raw_dotted("a.b.c").iter().count(), 3); |
| 171 | + } |
| 172 | + |
| 173 | + #[test] |
| 174 | + fn subset() { |
| 175 | + assert!(ModifierSet::from_raw_dotted("a") |
| 176 | + .is_subset(ModifierSet::from_raw_dotted("a.b"))); |
| 177 | + assert!(ModifierSet::from_raw_dotted("a") |
| 178 | + .is_subset(ModifierSet::from_raw_dotted("b.a"))); |
| 179 | + assert!(ModifierSet::from_raw_dotted("a.b") |
| 180 | + .is_subset(ModifierSet::from_raw_dotted("b.c.a"))); |
| 181 | + } |
| 182 | + |
| 183 | + #[test] |
| 184 | + fn best_match() { |
| 185 | + // 1. more modifiers in common with self |
| 186 | + assert_eq!( |
| 187 | + ModifierSet::from_raw_dotted("a.b").best_match_in( |
| 188 | + [ |
| 189 | + (ModifierSet::from_raw_dotted("a.c"), 1), |
| 190 | + (ModifierSet::from_raw_dotted("a.b"), 2), |
| 191 | + ] |
| 192 | + .into_iter() |
| 193 | + ), |
| 194 | + Some(2) |
| 195 | + ); |
| 196 | + // 2. fewer modifiers in general |
| 197 | + assert_eq!( |
| 198 | + ModifierSet::from_raw_dotted("a").best_match_in( |
| 199 | + [ |
| 200 | + (ModifierSet::from_raw_dotted("a"), 1), |
| 201 | + (ModifierSet::from_raw_dotted("a.b"), 2), |
| 202 | + ] |
| 203 | + .into_iter() |
| 204 | + ), |
| 205 | + Some(1) |
| 206 | + ); |
| 207 | + // the first rule takes priority over the second |
| 208 | + assert_eq!( |
| 209 | + ModifierSet::from_raw_dotted("a.b").best_match_in( |
| 210 | + [ |
| 211 | + (ModifierSet::from_raw_dotted("a"), 1), |
| 212 | + (ModifierSet::from_raw_dotted("a.b"), 2), |
| 213 | + ] |
| 214 | + .into_iter() |
| 215 | + ), |
| 216 | + Some(2) |
| 217 | + ); |
| 218 | + // among multiple best matches, the first one is returned |
| 219 | + assert_eq!( |
| 220 | + ModifierSet::default().best_match_in( |
| 221 | + [ |
| 222 | + (ModifierSet::from_raw_dotted("a"), 1), |
| 223 | + (ModifierSet::from_raw_dotted("b"), 2) |
| 224 | + ] |
| 225 | + .into_iter() |
| 226 | + ), |
| 227 | + Some(1) |
| 228 | + ); |
| 229 | + } |
| 230 | +} |
0 commit comments