use crate::syntax::matcher::match_segment;
use crate::syntax::{CaseSensitivity, CodePoint, Segment, Token};
use bitflags::bitflags;
#[cfg(test)]
mod tests;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum EntryType {
File,
Dir,
Other,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum TimeField {
Btime,
Mtime,
Atime,
Ctime,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Cmp {
Lt,
Le,
Eq,
Ne,
Ge,
Gt,
}
impl Cmp {
fn apply<T: PartialOrd>(self, a: T, b: T) -> bool {
match self {
Cmp::Lt => a < b,
Cmp::Le => a <= b,
Cmp::Eq => a == b,
Cmp::Ne => a != b,
Cmp::Ge => a >= b,
Cmp::Gt => a > b,
}
}
}
bitflags! {
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct MetaMask: u16 {
const SIZE = 1 << 0;
const MTIME = 1 << 1;
const ATIME = 1 << 2;
const CTIME = 1 << 3;
const BTIME = 1 << 4;
const TYPE = 1 << 5;
const REPARSE = 1 << 6;
const ATTRS = 1 << 7;
}
}
#[derive(Clone, Copy, Debug)]
pub struct EntryMeta<'a> {
pub name: &'a [CodePoint],
pub depth: u32,
pub entry_type: EntryType,
pub is_reparse: bool,
pub reparse_tag: u32,
pub attributes: u32,
pub size: u64,
pub btime: i64,
pub mtime: i64,
pub atime: i64,
pub ctime: i64,
}
impl EntryMeta<'_> {
fn time(&self, field: TimeField) -> i64 {
match field {
TimeField::Btime => self.btime,
TimeField::Mtime => self.mtime,
TimeField::Atime => self.atime,
TimeField::Ctime => self.ctime,
}
}
}
#[derive(Clone, Debug)]
pub enum Leaf {
Name {
seg: Segment,
case: CaseSensitivity,
negate: bool,
},
NameInSet {
segs: Vec<Segment>,
case: CaseSensitivity,
negate: bool,
},
IsType {
ty: EntryType,
negate: bool,
},
IsReparse {
negate: bool,
},
ReparseTag {
tag: u32,
negate: bool,
},
AttrsAllSet(u32),
AttrsAllClear(u32),
Size {
op: Cmp,
value: u64,
},
Time {
field: TimeField,
op: Cmp,
value: i64,
},
Depth {
op: Cmp,
value: u32,
},
}
fn literals(s: &str) -> Segment {
s.chars().map(|c| Token::Literal(c as u32)).collect()
}
impl Leaf {
pub fn name_exact(name: &str, case: CaseSensitivity) -> Leaf {
Leaf::Name {
seg: literals(name),
case,
negate: false,
}
}
pub fn name_glob(seg: Segment, case: CaseSensitivity) -> Leaf {
Leaf::Name {
seg,
case,
negate: false,
}
}
pub fn name_contains(needle: &str, case: CaseSensitivity) -> Leaf {
let mut seg = vec![Token::Star];
seg.extend(literals(needle));
seg.push(Token::Star);
Leaf::Name {
seg,
case,
negate: false,
}
}
pub fn name_extension(ext: &str, case: CaseSensitivity) -> Leaf {
let mut seg = vec![Token::Star, Token::Literal('.' as u32)];
seg.extend(literals(ext));
Leaf::Name {
seg,
case,
negate: false,
}
}
pub fn name_in_set(names: &[&str], case: CaseSensitivity) -> Leaf {
Leaf::NameInSet {
segs: names.iter().map(|n| literals(n)).collect(),
case,
negate: false,
}
}
}
pub fn eval_leaf(leaf: &Leaf, meta: &EntryMeta) -> bool {
match leaf {
Leaf::Name { seg, case, negate } => match_segment(seg, meta.name, *case) ^ negate,
Leaf::NameInSet { segs, case, negate } => {
segs.iter().any(|s| match_segment(s, meta.name, *case)) ^ negate
}
Leaf::IsType { ty, negate } => (meta.entry_type == *ty) ^ negate,
Leaf::IsReparse { negate } => meta.is_reparse ^ negate,
Leaf::ReparseTag { tag, negate } => (meta.reparse_tag == *tag) ^ negate,
Leaf::AttrsAllSet(mask) => meta.attributes & mask == *mask,
Leaf::AttrsAllClear(mask) => meta.attributes & mask == 0,
Leaf::Size { op, value } => op.apply(meta.size, *value),
Leaf::Time { field, op, value } => op.apply(meta.time(*field), *value),
Leaf::Depth { op, value } => op.apply(meta.depth, *value),
}
}
pub fn eval_all(leaves: &[Leaf], meta: &EntryMeta) -> bool {
leaves.iter().all(|leaf| eval_leaf(leaf, meta))
}
fn leaf_fields(leaf: &Leaf) -> MetaMask {
match leaf {
Leaf::Name { .. } | Leaf::NameInSet { .. } | Leaf::Depth { .. } => MetaMask::empty(),
Leaf::IsType { .. } => MetaMask::TYPE,
Leaf::IsReparse { .. } | Leaf::ReparseTag { .. } => MetaMask::REPARSE,
Leaf::AttrsAllSet(_) | Leaf::AttrsAllClear(_) => MetaMask::ATTRS,
Leaf::Size { .. } => MetaMask::SIZE,
Leaf::Time { field, .. } => match field {
TimeField::Btime => MetaMask::BTIME,
TimeField::Mtime => MetaMask::MTIME,
TimeField::Atime => MetaMask::ATIME,
TimeField::Ctime => MetaMask::CTIME,
},
}
}
pub fn required_fields(leaves: &[Leaf]) -> MetaMask {
leaves
.iter()
.fold(MetaMask::empty(), |acc, leaf| acc | leaf_fields(leaf))
}