diff options
author | Lars Wirzenius <liw@liw.fi> | 2021-10-04 17:32:10 +0300 |
---|---|---|
committer | Lars Wirzenius <liw@liw.fi> | 2021-10-04 17:32:10 +0300 |
commit | 9e9ed659650a2c8af9c3c8c8e0f6c23a6c45f090 (patch) | |
tree | ecbe230cde864aaf67ce8a4903f7811dc412e36d /src | |
parent | a44d3847590e5c24c78b2b129c464bd80b0d29ab (diff) | |
download | clab-9e9ed659650a2c8af9c3c8c8e0f6c23a6c45f090.tar.gz |
feat: allow optional tagging of address book entries
Sponsored-by: author
Diffstat (limited to 'src')
-rw-r--r-- | src/main.rs | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/src/main.rs b/src/main.rs index d25fdaa..2d847dd 100644 --- a/src/main.rs +++ b/src/main.rs @@ -25,6 +25,7 @@ fn main() -> anyhow::Result<()> { Cmd::Lint(x) => x.run(&opt, &book), Cmd::List(x) => x.run(&opt, &book)?, Cmd::Search(x) => x.run(&opt, &book)?, + Cmd::Tagged(x) => x.run(&opt, &book)?, Cmd::MuttQuery(x) => x.run(&opt, &book), } Ok(()) @@ -42,6 +43,7 @@ struct Entry { phone: Option<HashMap<String, String>>, irc: Option<HashMap<String, String>>, address: Option<HashMap<String, String>>, + tags: Option<Vec<String>>, last_checked: String, } @@ -116,6 +118,7 @@ enum Cmd { Lint(LintCommand), List(ListCommand), Search(SearchCommand), + Tagged(TaggedCommand), MuttQuery(MuttCommand), } @@ -171,6 +174,33 @@ impl SearchCommand { } #[derive(Debug, StructOpt)] +#[structopt(alias = "find")] +struct TaggedCommand { + #[structopt()] + wanted_tags: Vec<String>, +} + +impl TaggedCommand { + fn run(&self, _opt: &Opt, book: &AddressBook) -> anyhow::Result<()> { + let matches: Vec<Entry> = book.iter().filter(|e| self.is_match(e)).cloned().collect(); + output_entries(&matches) + } + + fn is_match(&self, entry: &Entry) -> bool { + if let Some(actual_tags) = &entry.tags { + for wanted_tag in self.wanted_tags.iter() { + if !actual_tags.contains(wanted_tag) { + return false; + } + } + true + } else { + false + } + } +} + +#[derive(Debug, StructOpt)] struct MuttCommand { #[structopt()] word: String, |