pub struct Mode { /* private fields */ }
Expand description
Information about a Pattern
.
Its main purpose is to accelerate pattern matching, or to negate the match result or to keep special rules only applicable when matching paths.
The mode is typically created when parsing the pattern by inspecting it and isn’t typically handled by the user.
Implementations§
source§impl Mode
impl Mode
sourcepub const NO_SUB_DIR: Self = _
pub const NO_SUB_DIR: Self = _
The pattern does not contain a sub-directory and - it doesn’t contain slashes after removing the trailing one.
sourcepub const ENDS_WITH: Self = _
pub const ENDS_WITH: Self = _
A pattern that is ‘*literal’, meaning that it ends with what’s given here
sourcepub const MUST_BE_DIR: Self = _
pub const MUST_BE_DIR: Self = _
The pattern must match a directory, and not a file.
sourcepub const NEGATIVE: Self = _
pub const NEGATIVE: Self = _
The pattern matches, but should be negated. Note that this mode has to be checked and applied by the caller.
sourcepub const ABSOLUTE: Self = _
pub const ABSOLUTE: Self = _
The pattern starts with a slash and thus matches only from the beginning.
sourcepub const fn empty() -> Self
pub const fn empty() -> Self
Returns an empty set of flags.
Examples found in repository?
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
pub fn pattern(mut pat: &[u8]) -> Option<(BString, pattern::Mode, Option<usize>)> {
let mut mode = Mode::empty();
if pat.is_empty() {
return None;
};
if pat.first() == Some(&b'!') {
mode |= Mode::NEGATIVE;
pat = &pat[1..];
} else if pat.first() == Some(&b'\\') {
let second = pat.get(1);
if second == Some(&b'!') || second == Some(&b'#') {
pat = &pat[1..];
}
}
if pat.iter().all(|b| b.is_ascii_whitespace()) {
return None;
}
if pat.first() == Some(&b'/') {
mode |= Mode::ABSOLUTE;
pat = &pat[1..];
}
let mut pat = truncate_non_escaped_trailing_spaces(pat);
if pat.last() == Some(&b'/') {
mode |= Mode::MUST_BE_DIR;
pat.pop();
}
if !pat.contains(&b'/') {
mode |= Mode::NO_SUB_DIR;
}
if pat.first() == Some(&b'*') && first_wildcard_pos(&pat[1..]).is_none() {
mode |= Mode::ENDS_WITH;
}
let pos_of_first_wildcard = first_wildcard_pos(&pat);
Some((pat, mode, pos_of_first_wildcard))
}
sourcepub const fn from_bits(bits: u32) -> Option<Self>
pub const fn from_bits(bits: u32) -> Option<Self>
Convert from underlying bit representation, unless that representation contains bits that do not correspond to a flag.
sourcepub const fn from_bits_truncate(bits: u32) -> Self
pub const fn from_bits_truncate(bits: u32) -> Self
Convert from underlying bit representation, dropping any bits that do not correspond to flags.
sourcepub const unsafe fn from_bits_unchecked(bits: u32) -> Self
pub const unsafe fn from_bits_unchecked(bits: u32) -> Self
Convert from underlying bit representation, preserving all bits (even those not corresponding to a defined flag).
Safety
The caller of the bitflags!
macro can chose to allow or
disallow extra bits for their bitflags type.
The caller of from_bits_unchecked()
has to ensure that
all bits correspond to a defined flag or that extra bits
are valid for this bitflags type.
sourcepub const fn intersects(&self, other: Self) -> bool
pub const fn intersects(&self, other: Self) -> bool
Returns true
if there are flags common to both self
and other
.
sourcepub const fn contains(&self, other: Self) -> bool
pub const fn contains(&self, other: Self) -> bool
Returns true
if all of the flags in other
are contained within self
.
Examples found in repository?
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
pub fn is_negative(&self) -> bool {
self.mode.contains(Mode::NEGATIVE)
}
/// Match the given `path` which takes slashes (and only slashes) literally, and is relative to the repository root.
/// Note that `path` is assumed to be relative to the repository.
///
/// We may take various shortcuts which is when `basename_start_pos` and `is_dir` come into play.
/// `basename_start_pos` is the index at which the `path`'s basename starts.
///
/// Lastly, `case` folding can be configured as well.
pub fn matches_repo_relative_path<'a>(
&self,
path: impl Into<&'a BStr>,
basename_start_pos: Option<usize>,
is_dir: Option<bool>,
case: Case,
) -> bool {
let is_dir = is_dir.unwrap_or(false);
if !is_dir && self.mode.contains(pattern::Mode::MUST_BE_DIR) {
return false;
}
let flags = wildmatch::Mode::NO_MATCH_SLASH_LITERAL
| match case {
Case::Fold => wildmatch::Mode::IGNORE_CASE,
Case::Sensitive => wildmatch::Mode::empty(),
};
let path = path.into();
debug_assert_eq!(
basename_start_pos,
path.rfind_byte(b'/').map(|p| p + 1),
"BUG: invalid cached basename_start_pos provided"
);
debug_assert!(!path.starts_with(b"/"), "input path must be relative");
if self.mode.contains(pattern::Mode::NO_SUB_DIR) && !self.mode.contains(pattern::Mode::ABSOLUTE) {
let basename = &path[basename_start_pos.unwrap_or_default()..];
self.matches(basename, flags)
} else {
self.matches(path, flags)
}
}
/// See if `value` matches this pattern in the given `mode`.
///
/// `mode` can identify `value` as path which won't match the slash character, and can match
/// strings with cases ignored as well. Note that the case folding performed here is ASCII only.
///
/// Note that this method uses some shortcuts to accelerate simple patterns.
fn matches<'a>(&self, value: impl Into<&'a BStr>, mode: wildmatch::Mode) -> bool {
let value = value.into();
match self.first_wildcard_pos {
// "*literal" case, overrides starts-with
Some(pos) if self.mode.contains(pattern::Mode::ENDS_WITH) && !value.contains(&b'/') => {
let text = &self.text[pos + 1..];
if mode.contains(wildmatch::Mode::IGNORE_CASE) {
value
.len()
.checked_sub(text.len())
.map(|start| text.eq_ignore_ascii_case(&value[start..]))
.unwrap_or(false)
} else {
value.ends_with(text.as_ref())
}
}
Some(pos) => {
if mode.contains(wildmatch::Mode::IGNORE_CASE) {
if !value
.get(..pos)
.map_or(false, |value| value.eq_ignore_ascii_case(&self.text[..pos]))
{
return false;
}
} else if !value.starts_with(&self.text[..pos]) {
return false;
}
crate::wildmatch(self.text.as_bstr(), value, mode)
}
None => {
if mode.contains(wildmatch::Mode::IGNORE_CASE) {
self.text.eq_ignore_ascii_case(value)
} else {
self.text == value
}
}
}
}
}
impl fmt::Display for Pattern {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if self.mode.contains(Mode::NEGATIVE) {
"!".fmt(f)?;
}
if self.mode.contains(Mode::ABSOLUTE) {
"/".fmt(f)?;
}
self.text.fmt(f)?;
if self.mode.contains(Mode::MUST_BE_DIR) {
"/".fmt(f)?;
}
Ok(())
}
sourcepub fn set(&mut self, other: Self, value: bool)
pub fn set(&mut self, other: Self, value: bool)
Inserts or removes the specified flags depending on the passed value.
sourcepub const fn intersection(self, other: Self) -> Self
pub const fn intersection(self, other: Self) -> Self
Returns the intersection between the flags in self
and
other
.
Specifically, the returned set contains only the flags which are
present in both self
and other
.
This is equivalent to using the &
operator (e.g.
ops::BitAnd
), as in flags & other
.
sourcepub const fn union(self, other: Self) -> Self
pub const fn union(self, other: Self) -> Self
Returns the union of between the flags in self
and other
.
Specifically, the returned set contains all flags which are
present in either self
or other
, including any which are
present in both (see Self::symmetric_difference
if that
is undesirable).
This is equivalent to using the |
operator (e.g.
ops::BitOr
), as in flags | other
.
sourcepub const fn difference(self, other: Self) -> Self
pub const fn difference(self, other: Self) -> Self
Returns the difference between the flags in self
and other
.
Specifically, the returned set contains all flags present in
self
, except for the ones present in other
.
It is also conceptually equivalent to the “bit-clear” operation:
flags & !other
(and this syntax is also supported).
This is equivalent to using the -
operator (e.g.
ops::Sub
), as in flags - other
.
sourcepub const fn symmetric_difference(self, other: Self) -> Self
pub const fn symmetric_difference(self, other: Self) -> Self
Returns the symmetric difference between the flags
in self
and other
.
Specifically, the returned set contains the flags present which
are present in self
or other
, but that are not present in
both. Equivalently, it contains the flags present in exactly
one of the sets self
and other
.
This is equivalent to using the ^
operator (e.g.
ops::BitXor
), as in flags ^ other
.
sourcepub const fn complement(self) -> Self
pub const fn complement(self) -> Self
Returns the complement of this set of flags.
Specifically, the returned set contains all the flags which are
not set in self
, but which are allowed for this type.
Alternatively, it can be thought of as the set difference
between Self::all()
and self
(e.g. Self::all() - self
)
This is equivalent to using the !
operator (e.g.
ops::Not
), as in !flags
.
Trait Implementations§
source§impl BitAndAssign<Mode> for Mode
impl BitAndAssign<Mode> for Mode
source§fn bitand_assign(&mut self, other: Self)
fn bitand_assign(&mut self, other: Self)
Disables all flags disabled in the set.
source§impl BitOrAssign<Mode> for Mode
impl BitOrAssign<Mode> for Mode
source§fn bitor_assign(&mut self, other: Self)
fn bitor_assign(&mut self, other: Self)
Adds the set of flags.
source§impl BitXorAssign<Mode> for Mode
impl BitXorAssign<Mode> for Mode
source§fn bitxor_assign(&mut self, other: Self)
fn bitxor_assign(&mut self, other: Self)
Toggles the set of flags.
source§impl<'de> Deserialize<'de> for Mode
impl<'de> Deserialize<'de> for Mode
source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
source§impl Extend<Mode> for Mode
impl Extend<Mode> for Mode
source§fn extend<T: IntoIterator<Item = Self>>(&mut self, iterator: T)
fn extend<T: IntoIterator<Item = Self>>(&mut self, iterator: T)
source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one
)source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one
)source§impl FromIterator<Mode> for Mode
impl FromIterator<Mode> for Mode
source§fn from_iter<T: IntoIterator<Item = Self>>(iterator: T) -> Self
fn from_iter<T: IntoIterator<Item = Self>>(iterator: T) -> Self
source§impl Ord for Mode
impl Ord for Mode
source§impl PartialEq<Mode> for Mode
impl PartialEq<Mode> for Mode
source§impl PartialOrd<Mode> for Mode
impl PartialOrd<Mode> for Mode
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read moresource§impl SubAssign<Mode> for Mode
impl SubAssign<Mode> for Mode
source§fn sub_assign(&mut self, other: Self)
fn sub_assign(&mut self, other: Self)
Disables all flags enabled in the set.