ok/jj
1
0
Fork 0
forked from mirrors/jj

str_util: add more StringPattern methods for convenience

The Display impl helps to format error messages. Since both Regex and
glob::Pattern implement Display, it's probably okay for our type to copy that.
This commit is contained in:
Yuya Nishihara 2023-10-20 03:39:10 +09:00
parent 4c4eb31a62
commit 16ef57907b

View file

@ -16,6 +16,7 @@
use std::borrow::Borrow;
use std::collections::BTreeMap;
use std::fmt;
use either::Either;
use thiserror::Error;
@ -49,6 +50,11 @@ impl StringPattern {
StringPattern::Substring(String::new())
}
/// Creates pattern that matches exactly.
pub fn exact(src: impl Into<String>) -> Self {
StringPattern::Exact(src.into())
}
/// Parses the given string as glob pattern.
pub fn glob(src: &str) -> Result<Self, StringPatternParseError> {
// TODO: might be better to do parsing and compilation separately since
@ -61,13 +67,18 @@ impl StringPattern {
/// Parses the given string as pattern of the specified `kind`.
pub fn from_str_kind(src: &str, kind: &str) -> Result<Self, StringPatternParseError> {
match kind {
"exact" => Ok(StringPattern::Exact(src.to_owned())),
"exact" => Ok(StringPattern::exact(src)),
"glob" => StringPattern::glob(src),
"substring" => Ok(StringPattern::Substring(src.to_owned())),
_ => Err(StringPatternParseError::InvalidKind(kind.to_owned())),
}
}
/// Returns true if this pattern matches input strings exactly.
pub fn is_exact(&self) -> bool {
self.as_exact().is_some()
}
/// Returns a literal pattern if this should match input strings exactly.
///
/// This can be used to optimize map lookup by exact key.
@ -78,6 +89,15 @@ impl StringPattern {
}
}
/// Returns the original string of this pattern.
pub fn as_str(&self) -> &str {
match self {
StringPattern::Exact(literal) => literal,
StringPattern::Glob(pattern) => pattern.as_str(),
StringPattern::Substring(needle) => needle,
}
}
/// Returns true if this pattern matches the `haystack`.
pub fn matches(&self, haystack: &str) -> bool {
match self {
@ -99,3 +119,10 @@ impl StringPattern {
}
}
}
impl fmt::Display for StringPattern {
/// Shows the original string of this pattern.
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.as_str())
}
}