index: add PrefixResolution::map() helper

I'm going to make IdIndex::resolve_prefix_with() return (key, values) pair,
and add convenient wrappers that .map() the pair to either key or values.
This commit is contained in:
Yuya Nishihara 2023-05-23 18:43:37 +09:00
parent d6f1ab697a
commit e7f83e7681
2 changed files with 14 additions and 7 deletions

View file

@ -249,13 +249,10 @@ mod tests {
#[test]
fn test_id_index_resolve_prefix() {
fn sorted(resolution: PrefixResolution<Vec<i32>>) -> PrefixResolution<Vec<i32>> {
match resolution {
PrefixResolution::SingleMatch(mut xs) => {
xs.sort(); // order of values might not be preserved by IdIndex
PrefixResolution::SingleMatch(xs)
}
_ => resolution,
}
resolution.map(|mut xs| {
xs.sort(); // order of values might not be preserved by IdIndex
xs
})
}
let id_index = IdIndex::from_vec(vec![
(ChangeId::from_hex("0000"), 0),

View file

@ -165,6 +165,16 @@ pub enum PrefixResolution<T> {
AmbiguousMatch,
}
impl<T> PrefixResolution<T> {
pub fn map<U>(self, f: impl FnOnce(T) -> U) -> PrefixResolution<U> {
match self {
PrefixResolution::NoMatch => PrefixResolution::NoMatch,
PrefixResolution::SingleMatch(x) => PrefixResolution::SingleMatch(f(x)),
PrefixResolution::AmbiguousMatch => PrefixResolution::AmbiguousMatch,
}
}
}
impl<T: Clone> PrefixResolution<T> {
pub fn plus(&self, other: &PrefixResolution<T>) -> PrefixResolution<T> {
match (self, other) {