mirror of
https://github.com/martinvonz/jj.git
synced 2025-02-06 03:22:59 +00:00
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:
parent
d6f1ab697a
commit
e7f83e7681
2 changed files with 14 additions and 7 deletions
|
@ -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) => {
|
||||
resolution.map(|mut xs| {
|
||||
xs.sort(); // order of values might not be preserved by IdIndex
|
||||
PrefixResolution::SingleMatch(xs)
|
||||
}
|
||||
_ => resolution,
|
||||
}
|
||||
xs
|
||||
})
|
||||
}
|
||||
let id_index = IdIndex::from_vec(vec![
|
||||
(ChangeId::from_hex("0000"), 0),
|
||||
|
|
|
@ -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) {
|
||||
|
|
Loading…
Reference in a new issue