str_util: extract StringPattern from revset module

Branch name filtering in CLI will be migrated to this, and I'll probably add
glob:<pattern> in place of --glob option.
This commit is contained in:
Yuya Nishihara 2023-10-19 16:35:09 +09:00
parent 96cb72291c
commit 5707a194d5
5 changed files with 56 additions and 37 deletions

View file

@ -9,7 +9,8 @@ use jj_lib::backend::{CommitId, ObjectId};
use jj_lib::git;
use jj_lib::op_store::RefTarget;
use jj_lib::repo::Repo;
use jj_lib::revset::{self, RevsetExpression, StringPattern};
use jj_lib::revset::{self, RevsetExpression};
use jj_lib::str_util::StringPattern;
use jj_lib::view::View;
use crate::cli_util::{user_error, user_error_with_hint, CommandError, CommandHelper, RevisionArg};

View file

@ -20,9 +20,10 @@ use jj_lib::refs::{
};
use jj_lib::repo::Repo;
use jj_lib::repo_path::RepoPath;
use jj_lib::revset::{self, RevsetExpression, RevsetIteratorExt as _, StringPattern};
use jj_lib::revset::{self, RevsetExpression, RevsetIteratorExt as _};
use jj_lib::settings::{ConfigResultExt as _, UserSettings};
use jj_lib::store::Store;
use jj_lib::str_util::StringPattern;
use jj_lib::workspace::Workspace;
use maplit::hashset;

View file

@ -62,6 +62,7 @@ pub mod simple_op_heads_store;
pub mod simple_op_store;
pub mod stacked_table;
pub mod store;
pub mod str_util;
pub mod submodule_store;
pub mod transaction;
pub mod tree;

View file

@ -42,6 +42,7 @@ use crate::repo::Repo;
use crate::repo_path::{FsPathParseError, RepoPath};
use crate::revset_graph::RevsetGraphEdge;
use crate::store::Store;
use crate::str_util::StringPattern;
/// Error occurred during symbol resolution.
#[derive(Debug, Error)]
@ -305,41 +306,6 @@ fn rename_rules_in_pest_error(mut err: pest::error::Error<Rule>) -> pest::error:
pub const GENERATION_RANGE_FULL: Range<u64> = 0..u64::MAX;
pub const GENERATION_RANGE_EMPTY: Range<u64> = 0..0;
/// Pattern to be tested against string property like commit description or
/// branch name.
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum StringPattern {
/// Matches strings exactly equal to `string`.
Exact(String),
/// Matches strings that contain `substring`.
Substring(String),
}
impl StringPattern {
/// Pattern that matches any string.
pub fn everything() -> Self {
StringPattern::Substring(String::new())
}
/// Returns true if this pattern matches the `haystack`.
pub fn matches(&self, haystack: &str) -> bool {
match self {
StringPattern::Exact(literal) => haystack == literal,
StringPattern::Substring(needle) => haystack.contains(needle),
}
}
/// Returns a literal pattern if this should match input strings exactly.
///
/// This can be used to optimize map lookup by exact key.
pub fn as_exact(&self) -> Option<&str> {
match self {
StringPattern::Exact(literal) => Some(literal),
StringPattern::Substring(_) => None,
}
}
}
/// Symbol or function to be resolved to `CommitId`s.
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum RevsetCommitRef {

50
lib/src/str_util.rs Normal file
View file

@ -0,0 +1,50 @@
// Copyright 2021-2023 The Jujutsu Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//! String helpers.
/// Pattern to be tested against string property like commit description or
/// branch name.
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum StringPattern {
/// Matches strings exactly equal to `string`.
Exact(String),
/// Matches strings that contain `substring`.
Substring(String),
}
impl StringPattern {
/// Pattern that matches any string.
pub const fn everything() -> Self {
StringPattern::Substring(String::new())
}
/// Returns a literal pattern if this should match input strings exactly.
///
/// This can be used to optimize map lookup by exact key.
pub fn as_exact(&self) -> Option<&str> {
match self {
StringPattern::Exact(literal) => Some(literal),
StringPattern::Substring(_) => None,
}
}
/// Returns true if this pattern matches the `haystack`.
pub fn matches(&self, haystack: &str) -> bool {
match self {
StringPattern::Exact(literal) => haystack == literal,
StringPattern::Substring(needle) => haystack.contains(needle),
}
}
}