Merge pull request #3652 from cyqsimon/syntax-mapping-glob-parse-cleanup

Builtin syntax mapping: cleanup matcher glob parsing logic
This commit is contained in:
Keith Hall authored and GitHub committed 2026-03-25 22:01:52 +02:00
commit 3ee9ea9a8d
2 files changed
+34 -36

No files matched your search

+1
View File
@@ -33,6 +33,7 @@
- Use git version of cross. See #3533 (@OctopusET) - Use git version of cross. See #3533 (@OctopusET)
- Bump MSRV to 1.88, update `time` crate to 0.3.47 to fix RUSTSEC-2026-0009, see #3581 (@NORMAL-EX) - Bump MSRV to 1.88, update `time` crate to 0.3.47 to fix RUSTSEC-2026-0009, see #3581 (@NORMAL-EX)
- Allow home and end keys to be used with builtin pager, see #3651 (@keith-hall) - Allow home and end keys to be used with builtin pager, see #3651 (@keith-hall)
- Builtin syntax mapping: cleanup matcher glob parsing logic #3652 (@cyqsimon)
## Syntaxes ## Syntaxes
+33 -36
View File
@@ -47,13 +47,24 @@ impl ToTokens for MappingTarget {
} }
} }
/// Helper type for deserializing a `Matcher` from either a plain string or a
/// `{ glob = "...", case_sensitive = true }` struct.
#[derive(Deserialize)]
#[serde(untagged)]
enum RawMatcher {
Simple(String),
Full {
glob: String,
case_sensitive: Option<bool>,
},
}
/// Whether a glob pattern should be matched case-sensitively or case-insensitively. /// Whether a glob pattern should be matched case-sensitively or case-insensitively.
/// ///
/// Mirrors the runtime `Case` type in `src/syntax_mapping.rs`. /// Mirrors the runtime `Case` type in `src/syntax_mapping.rs`.
#[derive(Clone, Debug, PartialEq, Eq, Hash, Default)] #[derive(Clone, Debug, PartialEq, Eq, Hash)]
enum Case { enum Case {
Sensitive, Sensitive,
#[default]
Insensitive, Insensitive,
} }
impl ToTokens for Case { impl ToTokens for Case {
@@ -66,11 +77,11 @@ impl ToTokens for Case {
} }
} }
#[derive(Clone, Debug, Default, PartialEq, Eq, Hash, Deserialize)]
#[serde(try_from = "RawMatcher")]
/// A single matcher. /// A single matcher.
/// ///
/// Codegen converts this into a `Lazy<Option<GlobMatcher>>`. /// Codegen converts this into a `Lazy<Option<GlobMatcher>>`.
#[derive(Clone, Debug, PartialEq, Eq, Hash, Deserialize)]
#[serde(try_from = "RawMatcher")]
struct Matcher { struct Matcher {
segments: Vec<MatcherSegment>, segments: Vec<MatcherSegment>,
/// Whether the glob pattern should be matched case-sensitively. /// Whether the glob pattern should be matched case-sensitively.
@@ -78,7 +89,7 @@ struct Matcher {
/// Defaults to `Case::Insensitive` for backwards compatibility. /// Defaults to `Case::Insensitive` for backwards compatibility.
case: Case, case: Case,
} }
/// Parse a matcher. /// Parse the glob pattern of a matcher.
/// ///
/// Note that this implementation is rather strict: it will greedily interpret /// Note that this implementation is rather strict: it will greedily interpret
/// every valid environment variable replacement as such, then immediately /// every valid environment variable replacement as such, then immediately
@@ -92,9 +103,7 @@ struct Matcher {
/// ///
/// Revision history: /// Revision history:
/// - 2024-02-20: allow `{` and `}` (glob brace expansion) /// - 2024-02-20: allow `{` and `}` (glob brace expansion)
impl FromStr for Matcher { fn parse_glob(s: &str) -> Result<Vec<MatcherSegment>, anyhow::Error> {
type Err = anyhow::Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
use MatcherSegment as Seg; use MatcherSegment as Seg;
static VAR_REGEX: Lazy<Regex> = Lazy::new(|| Regex::new(r"\$\{([\w\d_]+)\}").unwrap()); static VAR_REGEX: Lazy<Regex> = Lazy::new(|| Regex::new(r"\$\{([\w\d_]+)\}").unwrap());
@@ -142,43 +151,31 @@ impl FromStr for Matcher {
bail!(r#"Invalid matcher: "{s}""#); bail!(r#"Invalid matcher: "{s}""#);
} }
Ok(Self { Ok(non_empty_segments)
segments: non_empty_segments,
case: Case::Insensitive,
})
} }
}
/// Helper type for deserializing a `Matcher` from either a plain string or a
/// `{ glob = "...", case_sensitive = true }` struct.
#[derive(Deserialize)]
#[serde(untagged)]
enum RawMatcher {
Simple(String),
Full {
glob: String,
#[serde(default)]
case_sensitive: bool,
},
}
impl TryFrom<RawMatcher> for Matcher { impl TryFrom<RawMatcher> for Matcher {
type Error = anyhow::Error; type Error = anyhow::Error;
fn try_from(raw: RawMatcher) -> Result<Self, Self::Error> { fn try_from(raw: RawMatcher) -> Result<Self, Self::Error> {
match raw { const DEFAULT_CASE: Case = Case::Insensitive;
RawMatcher::Simple(s) => Matcher::from_str(&s), match &raw {
RawMatcher::Simple(s) => {
let segments = parse_glob(s)?;
Ok(Self {
segments,
case: DEFAULT_CASE,
})
}
RawMatcher::Full { RawMatcher::Full {
glob, glob,
case_sensitive, case_sensitive,
} => { } => {
let mut matcher = Matcher::from_str(&glob)?; let segments = parse_glob(glob)?;
matcher.case = if case_sensitive { let case = match case_sensitive {
Case::Sensitive None => DEFAULT_CASE,
} else { Some(false) => Case::Insensitive,
Case::Insensitive Some(true) => Case::Sensitive,
}; };
Ok(matcher) Ok(Self { segments, case })
} }
} }
} }