From 5707a194d5bdcafff5ab2dd365a747f5153126af Mon Sep 17 00:00:00 2001 From: Yuya Nishihara Date: Thu, 19 Oct 2023 16:35:09 +0900 Subject: [PATCH] str_util: extract StringPattern from revset module Branch name filtering in CLI will be migrated to this, and I'll probably add glob: in place of --glob option. --- cli/src/commands/branch.rs | 3 ++- cli/src/commands/git.rs | 3 ++- lib/src/lib.rs | 1 + lib/src/revset.rs | 36 +-------------------------- lib/src/str_util.rs | 50 ++++++++++++++++++++++++++++++++++++++ 5 files changed, 56 insertions(+), 37 deletions(-) create mode 100644 lib/src/str_util.rs diff --git a/cli/src/commands/branch.rs b/cli/src/commands/branch.rs index b1e5aeb3e..b246344af 100644 --- a/cli/src/commands/branch.rs +++ b/cli/src/commands/branch.rs @@ -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}; diff --git a/cli/src/commands/git.rs b/cli/src/commands/git.rs index 91b9c6f76..2def802ae 100644 --- a/cli/src/commands/git.rs +++ b/cli/src/commands/git.rs @@ -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; diff --git a/lib/src/lib.rs b/lib/src/lib.rs index 303bbbb0f..f0ac9314a 100644 --- a/lib/src/lib.rs +++ b/lib/src/lib.rs @@ -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; diff --git a/lib/src/revset.rs b/lib/src/revset.rs index 155d62ff3..43200d532 100644 --- a/lib/src/revset.rs +++ b/lib/src/revset.rs @@ -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) -> pest::error: pub const GENERATION_RANGE_FULL: Range = 0..u64::MAX; pub const GENERATION_RANGE_EMPTY: Range = 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 { diff --git a/lib/src/str_util.rs b/lib/src/str_util.rs new file mode 100644 index 000000000..e7b6cf67f --- /dev/null +++ b/lib/src/str_util.rs @@ -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), + } + } +}