feat: make the glob expr case insensitive by default, and prepend \s to make it sensitive (#156)

This commit is contained in:
三咲雅 · Misaki Masa 2023-09-14 09:34:07 +08:00 committed by GitHub
parent 9c6b3c9d1d
commit b62307dc4a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 8 deletions

View file

@ -81,8 +81,8 @@ rules = [
Available rule parameters are as follows:
- name: Glob expression for matching the file name
- mime: Glob expression for matching the MIME type
- name: Glob expression for matching the file name. Case insensitive by default, add `\s` to the beginning to make it sensitive.
- mime: Glob expression for matching the MIME type. Case insensitive by default, add `\s` to the beginning to make it sensitive.
- use: Opener name corresponding to the names in the opener section.
## tasks

View file

@ -1,18 +1,26 @@
use std::path::Path;
use glob::MatchOptions;
use serde::Deserialize;
#[derive(Debug, Deserialize)]
#[serde(try_from = "String")]
pub struct Pattern {
inner: glob::Pattern,
sensitive: bool,
is_folder: bool,
full_path: bool,
}
impl Pattern {
#[inline]
pub fn matches(&self, str: impl AsRef<str>) -> bool { self.inner.matches(str.as_ref()) }
pub fn matches(&self, str: impl AsRef<str>) -> bool {
self.inner.matches_with(str.as_ref(), MatchOptions {
case_sensitive: self.sensitive,
require_literal_separator: false,
require_literal_leading_dot: false,
})
}
#[inline]
pub fn match_path(&self, path: impl AsRef<Path>, is_folder: Option<bool>) -> bool {
@ -20,7 +28,7 @@ impl Pattern {
let s = if self.full_path {
path.to_str()
} else {
path.file_name().and_then(|n| n.to_str()).or(path.to_str())
path.file_name().and_then(|n| n.to_str()).or_else(|| path.to_str())
};
is_folder.map_or(true, |f| f == self.is_folder) && s.map_or(false, |s| self.matches(s))
}
@ -30,11 +38,13 @@ impl TryFrom<&str> for Pattern {
type Error = anyhow::Error;
fn try_from(s: &str) -> Result<Self, Self::Error> {
let new = s.trim_end_matches('/');
let a = s.trim_start_matches("\\s");
let b = a.trim_end_matches('/');
Ok(Self {
inner: glob::Pattern::new(new)?,
is_folder: new.len() < s.len(),
full_path: new.contains('/'),
inner: glob::Pattern::new(b)?,
sensitive: a.len() < s.len(),
is_folder: b.len() < a.len(),
full_path: b.contains('/'),
})
}
}