Skip to content

Commit

Permalink
str_util: extract StringPattern filtering iterator from revset module
Browse files Browse the repository at this point in the history
  • Loading branch information
yuja committed Oct 21, 2023
1 parent 2d80f07 commit f7c8622
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 27 deletions.
37 changes: 10 additions & 27 deletions lib/src/revset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

#![allow(missing_docs)]

use std::collections::{BTreeMap, HashMap, HashSet};
use std::collections::{HashMap, HashSet};
use std::convert::Infallible;
use std::ops::Range;
use std::path::Path;
Expand All @@ -23,7 +23,6 @@ use std::str::FromStr;
use std::sync::Arc;
use std::{error, fmt};

use either::Either;
use itertools::Itertools;
use once_cell::sync::Lazy;
use pest::iterators::{Pair, Pairs};
Expand Down Expand Up @@ -1938,21 +1937,6 @@ pub fn walk_revs<'index>(
.evaluate(repo)
}

fn filter_map_values_by_key_pattern<'a: 'b, 'b, V>(
map: &'a BTreeMap<String, V>,
pattern: &'b StringPattern,
) -> impl Iterator<Item = &'a V> + 'b {
if let Some(key) = pattern.as_exact() {
Either::Left(map.get(key).into_iter())
} else {
Either::Right(
map.iter()
.filter(|(key, _)| pattern.matches(key))
.map(|(_, value)| value),
)
}
}

fn resolve_git_ref(repo: &dyn Repo, symbol: &str) -> Option<Vec<CommitId>> {
let view = repo.view();
for git_ref_prefix in &["", "refs/"] {
Expand Down Expand Up @@ -2152,8 +2136,9 @@ fn resolve_commit_ref(
RevsetCommitRef::Root => Ok(vec![repo.store().root_commit_id().clone()]),
RevsetCommitRef::Branches(pattern) => {
let view_data = repo.view().store_view();
let commit_ids = filter_map_values_by_key_pattern(&view_data.local_branches, pattern)
.flat_map(|target| target.added_ids())
let commit_ids = pattern
.filter_btree_map(&view_data.local_branches)
.flat_map(|(_, target)| target.added_ids())
.cloned()
.collect();
Ok(commit_ids)
Expand All @@ -2163,14 +2148,12 @@ fn resolve_commit_ref(
remote_pattern,
} => {
let view_data = repo.view().store_view();
let commit_ids =
filter_map_values_by_key_pattern(&view_data.remote_views, remote_pattern)
.flat_map(|remote_view| {
filter_map_values_by_key_pattern(&remote_view.branches, branch_pattern)
})
.flat_map(|remote_ref| remote_ref.target.added_ids())
.cloned()
.collect();
let commit_ids = remote_pattern
.filter_btree_map(&view_data.remote_views)
.flat_map(|(_, remote_view)| branch_pattern.filter_btree_map(&remote_view.branches))
.flat_map(|(_, remote_ref)| remote_ref.target.added_ids())
.cloned()
.collect();
Ok(commit_ids)
}
RevsetCommitRef::Tags => {
Expand Down
16 changes: 16 additions & 0 deletions lib/src/str_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@

//! String helpers.
use std::borrow::Borrow;
use std::collections::BTreeMap;

use either::Either;
use thiserror::Error;

/// Error occurred during pattern string parsing.
Expand Down Expand Up @@ -66,4 +70,16 @@ impl StringPattern {
StringPattern::Substring(needle) => haystack.contains(needle),
}
}

/// Iterates entries of the given `map` whose keys matches this pattern.
pub fn filter_btree_map<'a: 'b, 'b, K: Borrow<str> + Ord, V>(
&'b self,
map: &'a BTreeMap<K, V>,
) -> impl Iterator<Item = (&'a K, &'a V)> + 'b {
if let Some(key) = self.as_exact() {
Either::Left(map.get_key_value(key).into_iter())
} else {
Either::Right(map.iter().filter(|&(key, _)| self.matches(key.borrow())))
}
}
}

0 comments on commit f7c8622

Please sign in to comment.