watchexec/lib/src/filter/tagged/parse.rs

127 lines
3.0 KiB
Rust
Raw Normal View History

2021-09-18 07:07:32 +02:00
use std::str::FromStr;
2021-09-28 11:22:14 +02:00
use nom::{
branch::alt,
bytes::complete::{is_not, tag, tag_no_case, take_while1},
character::complete::char,
combinator::{map_res, opt},
sequence::{delimited, tuple},
Finish, IResult,
};
2021-09-18 07:07:32 +02:00
use regex::Regex;
2021-09-28 11:22:14 +02:00
use tracing::trace;
2021-09-18 07:07:32 +02:00
use super::*;
impl FromStr for Filter {
type Err = error::TaggedFiltererError;
2021-09-18 07:07:32 +02:00
fn from_str(s: &str) -> Result<Self, Self::Err> {
fn matcher(i: &str) -> IResult<&str, Matcher> {
map_res(
alt((
tag_no_case("tag"),
tag_no_case("path"),
tag_no_case("kind"),
tag_no_case("source"),
tag_no_case("src"),
tag_no_case("process"),
tag_no_case("signal"),
tag_no_case("exit"),
)),
|m: &str| match m.to_ascii_lowercase().as_str() {
"tag" => Ok(Matcher::Tag),
"path" => Ok(Matcher::Path),
"kind" => Ok(Matcher::FileEventKind),
"source" => Ok(Matcher::Source),
"src" => Ok(Matcher::Source),
"process" => Ok(Matcher::Process),
"signal" => Ok(Matcher::Signal),
"exit" => Ok(Matcher::ProcessCompletion),
m => Err(format!("unknown matcher: {}", m)),
},
)(i)
}
fn op(i: &str) -> IResult<&str, Op> {
map_res(
alt((
tag("=="),
tag("!="),
tag("~="),
tag("*="),
tag(":="),
tag(":!"),
tag("="),
)),
|o: &str| match o {
"==" => Ok(Op::Equal),
"!=" => Ok(Op::NotEqual),
"~=" => Ok(Op::Regex),
"~!" => Ok(Op::NotRegex),
2021-09-18 07:07:32 +02:00
"*=" => Ok(Op::Glob),
"*!" => Ok(Op::NotGlob),
2021-09-18 07:07:32 +02:00
":=" => Ok(Op::InSet),
":!" => Ok(Op::NotInSet),
"=" => Ok(Op::Auto),
o => Err(format!("unknown op: `{}`", o)),
},
)(i)
}
fn pattern(i: &str) -> IResult<&str, &str> {
alt((
// TODO: escapes
delimited(char('"'), is_not("\""), char('"')),
delimited(char('\''), is_not("'"), char('\'')),
take_while1(|_| true),
))(i)
}
fn filter(i: &str) -> IResult<&str, Filter> {
map_res(
tuple((opt(tag("!")), matcher, op, pattern)),
|(n, m, o, p)| -> Result<_, ()> {
2021-09-18 07:07:32 +02:00
Ok(Filter {
in_path: None,
on: m,
op: match o {
Op::Auto => match m {
Matcher::Path => Op::Glob,
_ => Op::InSet,
},
o => o,
},
pat: match (o, m) {
// TODO: carry regex/glob errors through
2021-09-29 17:03:46 +02:00
(Op::Auto | Op::Glob, Matcher::Path) | (Op::Glob | Op::NotGlob, _) => {
2021-10-11 12:34:14 +02:00
Pattern::Glob(p.to_string())
2021-09-18 07:07:32 +02:00
}
2021-09-29 17:03:46 +02:00
(Op::Auto | Op::InSet | Op::NotInSet, _) => {
Pattern::Set(p.split(',').map(|s| s.trim().to_string()).collect())
2021-09-28 11:22:14 +02:00
}
(Op::Regex | Op::NotRegex, _) => {
Pattern::Regex(Regex::new(p).map_err(drop)?)
}
2021-09-29 17:03:46 +02:00
(Op::Equal | Op::NotEqual, _) => Pattern::Exact(p.to_string()),
2021-09-18 07:07:32 +02:00
},
negate: n.is_some(),
2021-09-18 07:07:32 +02:00
})
},
)(i)
}
2021-09-28 11:22:14 +02:00
trace!(src=?s, "parsing tagged filter");
2021-09-18 07:07:32 +02:00
filter(s)
.finish()
2021-09-28 11:22:14 +02:00
.map(|(_, f)| {
trace!(src=?s, filter=?f, "parsed tagged filter");
f
})
.map_err(|e| error::TaggedFiltererError::Parse {
2021-09-18 07:07:32 +02:00
src: s.to_string(),
err: e.code,
})
}
}