diff --git a/Cargo.lock b/Cargo.lock index 805ccff..ec443ba 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -17,6 +17,15 @@ version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe" +[[package]] +name = "aho-corasick" +version = "0.7.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e37cfd5e7657ada45f742d6e99ca5788580b5c529dc78faf11ece6dc702656f" +dependencies = [ + "memchr", +] + [[package]] name = "ansi_term" version = "0.12.1" @@ -1424,6 +1433,8 @@ version = "1.5.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d07a8629359eb56f1e2fb1652bb04212c072a87ba68546a04065d525673ac461" dependencies = [ + "aho-corasick", + "memchr", "regex-syntax", ] @@ -1974,6 +1985,7 @@ dependencies = [ "miette", "notify", "once_cell", + "regex", "thiserror", "tokio", "tracing", diff --git a/lib/Cargo.toml b/lib/Cargo.toml index bde3e98..601d4fa 100644 --- a/lib/Cargo.toml +++ b/lib/Cargo.toml @@ -24,6 +24,7 @@ futures = "0.3.16" miette = "1.0.0-beta.1" notify = "5.0.0-pre.12" once_cell = "1.8.0" +regex = "1.5.4" thiserror = "1.0.26" tracing = "0.1.26" diff --git a/lib/src/command/supervisor.rs b/lib/src/command/supervisor.rs index 23a8ca4..805fc46 100644 --- a/lib/src/command/supervisor.rs +++ b/lib/src/command/supervisor.rs @@ -17,7 +17,7 @@ use tracing::{debug, error, trace, warn}; use crate::{ error::RuntimeError, - event::{Event, Tag, Source}, + event::{Event, Source, Tag}, }; use super::Process; diff --git a/lib/src/filter.rs b/lib/src/filter.rs new file mode 100644 index 0000000..e83bc73 --- /dev/null +++ b/lib/src/filter.rs @@ -0,0 +1,45 @@ +use std::{collections::HashSet, path::PathBuf}; + +use regex::Regex; + +use crate::event::Tag; + +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct Filter { + pub in_path: Option, + pub on: Tag, + pub op: Op, + pub pat: Pattern, +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum Op { + Equal, + NotEqual, + Regex, + Glob, + Includes, + Excludes, + InSet, + OutSet, +} + +#[derive(Debug, Clone)] +pub enum Pattern { + Exact(String), + Regex(Regex), + Set(HashSet), +} + +impl PartialEq for Pattern { + fn eq(&self, other: &Self) -> bool { + match (self, other) { + (Self::Exact(l), Self::Exact(r)) => l == r, + (Self::Regex(l), Self::Regex(r)) => l.as_str() == r.as_str(), + (Self::Set(l), Self::Set(r)) => l == r, + _ => false, + } + } +} + +impl Eq for Pattern {} diff --git a/lib/src/fs.rs b/lib/src/fs.rs index ae3250a..83eef85 100644 --- a/lib/src/fs.rs +++ b/lib/src/fs.rs @@ -14,7 +14,7 @@ use tracing::{debug, error, trace}; use crate::{ error::{CriticalError, RuntimeError}, - event::{Event, Tag, Source}, + event::{Event, Source, Tag}, }; /// What kind of filesystem watcher to use. @@ -249,10 +249,7 @@ fn process_event( metadata.insert("notify-backend".to_string(), vec![src.to_string()]); } - let ev = Event { - tags, - metadata, - }; + let ev = Event { tags, metadata }; trace!(event = ?ev, "processed notify event into watchexec event"); n_events diff --git a/lib/src/lib.rs b/lib/src/lib.rs index 4f916f2..4319a9a 100644 --- a/lib/src/lib.rs +++ b/lib/src/lib.rs @@ -90,6 +90,7 @@ pub mod action; pub mod command; pub mod error; pub mod event; +pub mod filter; pub mod fs; pub mod signal; diff --git a/lib/src/signal.rs b/lib/src/signal.rs index a04227a..ec62504 100644 --- a/lib/src/signal.rs +++ b/lib/src/signal.rs @@ -5,7 +5,7 @@ use tracing::{debug, trace}; use crate::{ error::{CriticalError, RuntimeError}, - event::{Event, Tag, Source}, + event::{Event, Source, Tag}, }; /// A notification sent to the main (watchexec) process.