Allow disabling categories of filters

This commit is contained in:
Agatha Lovelace 2023-04-11 19:21:34 +02:00
parent 1ee90d2293
commit 5bd65faa55
Signed by: sorceress
GPG Key ID: 01D0B3AB10CED4F8
3 changed files with 12 additions and 3 deletions

1
.gitignore vendored
View File

@ -1 +1,2 @@
/target
.direnv

View File

@ -14,8 +14,11 @@ category "Spotify" {
category "Campaign tracking (itm)" {
params "itm_*"
}
category "Campaign tracking (stm)" disabled=true {
params "stm_*"
}
```
Categories do not have significance other than to make filter files better structured.
Categories are used to structure filter lists and allow disabling/enabling filters in groups.
Each parameter applies to all URLs, unless a domain like `@example.com` is specified at the end.
Both the parameter and the domain parts can contain wildcards. Use `*` to match 0 or more characters, and `?` to match exactly one character.
The structure is based on [NeatURL's format](https://github.com/Smile4ever/Neat-URL/#default-blocked-parameters), with a few differences (aside from a different file format):

View File

@ -9,6 +9,8 @@ use wildmatch::WildMatch;
struct Category {
#[knuffel(argument)]
name: String,
#[knuffel(property, default)]
disabled: bool,
#[knuffel(child, unwrap(arguments))]
params: Vec<String>,
}
@ -24,12 +26,15 @@ fn main() -> Result<()> {
.into_diagnostic()
.map_err(|err| err.context(format!("Could not read file `{filters}`")))?;
let filters = knuffel::parse::<Vec<Category>>("config.kdl", &filters)?;
let filters = knuffel::parse::<Vec<Category>>("config.kdl", &filters)?
.into_iter()
.filter(|v| !v.disabled)
.collect::<Vec<Category>>();
println!("Loaded with categories:");
filters.iter().for_each(|v| println!("\t{}", v.name));
// Flatten all patterns into a single list, as categories do not matter
// Flatten filters into patterns
let patterns: Vec<String> = filters.iter().map(|v| v.params.clone()).flatten().collect();
// Initialize clipboard context