fix: no matches of some icons (#19)

This commit is contained in:
三咲雅 · Misaki Masa 2023-08-04 09:19:45 +08:00 committed by GitHub
parent 5b88c68236
commit 78ee5e9ac8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 11 deletions

View file

@ -54,14 +54,17 @@ rules = [
[icons]
"Desktop/" = ""
"Documents/" = ""
"Downloads/" = ""
"Music/" = ""
"Pictures/" = ""
"Public/" = ""
"Videos/" = ""
".config/" = ""
"Desktop/" = ""
"Documents/" = ""
"Downloads/" = ""
"Pictures/" = ""
"Music/" = ""
"Movies/" = ""
"Videos/" = ""
"Public/" = ""
"Library/" = ""
"Development/" = ""
".config/" = ""
# Git
".git/" = ""

View file

@ -7,13 +7,22 @@ use serde::Deserialize;
pub struct Pattern {
inner: glob::Pattern,
is_folder: bool,
full_path: bool,
}
impl Pattern {
#[inline]
pub fn matches(&self, str: impl AsRef<str>) -> bool { self.inner.matches(str.as_ref()) }
#[inline]
pub fn match_path(&self, path: impl AsRef<Path>, is_folder: Option<bool>) -> bool {
is_folder.map_or(true, |f| f == self.is_folder) && self.inner.matches_path(path.as_ref())
let path = path.as_ref();
let s = if self.full_path {
path.to_str()
} else {
path.file_name().and_then(|n| n.to_str()).or(path.to_str())
};
is_folder.map_or(true, |f| f == self.is_folder) && s.map_or(false, |s| self.matches(s))
}
}
@ -21,8 +30,12 @@ impl TryFrom<&str> for Pattern {
type Error = anyhow::Error;
fn try_from(s: &str) -> Result<Self, Self::Error> {
let is_folder = s.ends_with('/');
Ok(Self { inner: glob::Pattern::new(s.trim_end_matches('/'))?, is_folder })
let new = s.trim_end_matches('/');
Ok(Self {
inner: glob::Pattern::new(new)?,
is_folder: new.len() < s.len(),
full_path: new.contains('/'),
})
}
}