|
| 1 | +use std::ops::{AddAssign, Deref}; |
| 2 | + |
1 | 3 | macro_rules! declare_types {
|
2 | 4 | ($(<$lt:lifetime>)?
|
3 | 5 | $(derive($($Der:ident),*),)?
|
4 | 6 | str = $s:ty,
|
5 |
| - List = $L:ident<_> |
| 7 | + List = $List:ident<_> |
6 | 8 | ) => {
|
7 | 9 | /// A module of definitions.
|
8 | 10 | $(#[derive($($Der),*)])?
|
9 |
| - pub struct Module<$($lt)?>($L<($s, Binding<$($lt)?>)>); |
| 11 | + pub struct Module<$($lt)?>($List<($s, Binding<$($lt)?>)>); |
10 | 12 |
|
11 | 13 | /// A definition bound in a module, with metadata.
|
12 | 14 | $(#[derive($($Der),*)])?
|
@@ -38,8 +40,109 @@ macro_rules! declare_types {
|
38 | 40 | pub enum Symbol<$($lt)?> {
|
39 | 41 | /// A symbol without modifiers.
|
40 | 42 | Single(char),
|
41 |
| - /// A symbol with named modifiers. The symbol defaults to its first variant. |
42 |
| - Multi($L<($s, char)>), |
| 43 | + /// A symbol with named modifiers. |
| 44 | + /// The symbol defaults to its first variant. |
| 45 | + Multi($List<(ModifierSet<$s>, char)>), |
43 | 46 | }
|
44 | 47 | };
|
45 | 48 | }
|
| 49 | + |
| 50 | +/// A set of modifiers. |
| 51 | +#[derive(Debug, Copy, Clone)] |
| 52 | +pub struct ModifierSet<S>(S); |
| 53 | + |
| 54 | +impl<S: Deref<Target = str>> ModifierSet<S> { |
| 55 | + /// Convert the underlying string to a slice. |
| 56 | + pub fn as_deref(&self) -> ModifierSet<&str> { |
| 57 | + ModifierSet(&self.0) |
| 58 | + } |
| 59 | + |
| 60 | + /// Construct a modifier set from a string, |
| 61 | + /// where modifiers are separated by the character `.`. |
| 62 | + /// |
| 63 | + /// It is not unsafe to use this function wrongly, but it can produce |
| 64 | + /// unexpected results down the line. Correct usage should ensure that |
| 65 | + /// `s` does not contain any empty modifiers (i.e. the sequence `..`) |
| 66 | + /// and that no modifier occurs twice. |
| 67 | + pub fn new_unchecked(s: S) -> Self { |
| 68 | + Self(s) |
| 69 | + } |
| 70 | + |
| 71 | + /// Construct an empty modifier set. |
| 72 | + pub fn empty() -> Self |
| 73 | + where |
| 74 | + S: Default, |
| 75 | + { |
| 76 | + Self(S::default()) |
| 77 | + } |
| 78 | + |
| 79 | + /// Whether `self` is empty. |
| 80 | + pub fn is_empty(&self) -> bool { |
| 81 | + self.0.is_empty() |
| 82 | + } |
| 83 | + |
| 84 | + /// Add a modifier to the set, without checking that it is a valid modifier. |
| 85 | + /// |
| 86 | + /// It is not unsafe to use this method wrongly, but that can produce |
| 87 | + /// unexpected results down the line. Correct usage should ensure that |
| 88 | + /// `modifier` is not empty and doesn't contain the character `.`. |
| 89 | + pub fn add_unchecked(&mut self, m: &str) |
| 90 | + where |
| 91 | + S: for<'a> AddAssign<&'a str>, |
| 92 | + { |
| 93 | + if !self.0.is_empty() { |
| 94 | + self.0 += "."; |
| 95 | + } |
| 96 | + self.0 += m; |
| 97 | + } |
| 98 | + |
| 99 | + /// Iterate over the list of modifiers in an arbitrary order. |
| 100 | + pub fn iter(&self) -> impl Iterator<Item = &str> { |
| 101 | + self.0.split('.').filter(|s| !s.is_empty()) |
| 102 | + } |
| 103 | + |
| 104 | + /// Whether the set contains the modifier `m`. |
| 105 | + pub fn contains(&self, m: &str) -> bool { |
| 106 | + self.iter().any(|lhs| lhs == m) |
| 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 | + /// Find the best match from the list. |
| 115 | + /// |
| 116 | + /// To be considered a match, the modifier set must be a superset of |
| 117 | + /// (or equal to) `self`. Among different matches, the best one is selected |
| 118 | + /// by the following two criteria (in order): |
| 119 | + /// 1. Number of modifiers in common with `self` (more is better). |
| 120 | + /// 2. Total number of modifiers (fewer is better). |
| 121 | + pub fn best_match_in<'a, T>( |
| 122 | + &self, |
| 123 | + variants: impl Iterator<Item = (ModifierSet<&'a str>, T)>, |
| 124 | + ) -> Option<T> { |
| 125 | + let mut best = None; |
| 126 | + let mut best_score = None; |
| 127 | + |
| 128 | + // Find the best table entry with this name. |
| 129 | + for candidate in variants.filter(|(set, _)| self.is_subset(*set)) { |
| 130 | + let mut matching = 0; |
| 131 | + let mut total = 0; |
| 132 | + for modifier in candidate.0.iter() { |
| 133 | + if self.contains(modifier) { |
| 134 | + matching += 1; |
| 135 | + } |
| 136 | + total += 1; |
| 137 | + } |
| 138 | + |
| 139 | + let score = (matching, core::cmp::Reverse(total)); |
| 140 | + if best_score.map_or(true, |b| score > b) { |
| 141 | + best = Some(candidate.1); |
| 142 | + best_score = Some(score); |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + best |
| 147 | + } |
| 148 | +} |
0 commit comments