mirror of
https://github.com/rojo-rbx/rojo.git
synced 2026-10-21 20:21:38 +00:00
Add ignorable glob support (#1256)
This commit is contained in:
parent
0afb26e143
commit
15939b1647
6 changed files with 222 additions and 20 deletions
58
src/glob.rs
58
src/glob.rs
|
|
@ -48,3 +48,61 @@ impl<'de> Deserialize<'de> for Glob {
|
|||
Glob::new(&glob).map_err(D::Error::custom)
|
||||
}
|
||||
}
|
||||
|
||||
/// A glob with optional gitignore-style negation. A leading `!` marks the
|
||||
/// pattern as a negation (re-includes paths that an earlier rule excluded).
|
||||
/// To match a literal `!` at the start of a pattern, escape it with `\!`.
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct IgnorableGlob {
|
||||
glob: Glob,
|
||||
negated: bool,
|
||||
raw: String,
|
||||
}
|
||||
|
||||
impl IgnorableGlob {
|
||||
pub fn new(pattern: &str) -> Result<Self, Error> {
|
||||
let (negated, body) = if let Some(rest) = pattern.strip_prefix('!') {
|
||||
(true, rest)
|
||||
} else if pattern.starts_with(r"\!") {
|
||||
(false, &pattern[1..])
|
||||
} else {
|
||||
(false, pattern)
|
||||
};
|
||||
|
||||
Ok(IgnorableGlob {
|
||||
glob: Glob::new(body)?,
|
||||
negated,
|
||||
raw: pattern.to_owned(),
|
||||
})
|
||||
}
|
||||
|
||||
pub fn is_match<P: AsRef<Path>>(&self, path: P) -> bool {
|
||||
self.glob.is_match(path)
|
||||
}
|
||||
|
||||
pub fn is_negation(&self) -> bool {
|
||||
self.negated
|
||||
}
|
||||
}
|
||||
|
||||
impl PartialEq for IgnorableGlob {
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
self.negated == other.negated && self.glob == other.glob
|
||||
}
|
||||
}
|
||||
|
||||
impl Eq for IgnorableGlob {}
|
||||
|
||||
impl Serialize for IgnorableGlob {
|
||||
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
|
||||
serializer.serialize_str(&self.raw)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'de> Deserialize<'de> for IgnorableGlob {
|
||||
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
|
||||
let pattern = String::deserialize(deserializer)?;
|
||||
|
||||
IgnorableGlob::new(&pattern).map_err(D::Error::custom)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue