mj/commands/
notes.rs

1use clap::ArgMatches;
2use crate::core::*;
3
4#[derive(Debug)]
5pub enum NotesArgs {
6  EditNote(String, String),
7  RemoveNote(String, Option<String>),
8  ListAll,
9  ListCategories,
10  ListNotes(String),
11}
12
13pub fn run(vault: &Vault, args: &NotesArgs) -> Result<()> {
14  match args {
15    NotesArgs::EditNote(category, name) => {
16      let _saved = notes::edit(&vault, &category, &name)?;
17      Ok(())
18    },
19    NotesArgs::RemoveNote(category, name) => {
20      notes::remove(&vault, &category, &name)
21    },
22    NotesArgs::ListAll => {
23      notes::list(&vault, true, None)
24    },
25    NotesArgs::ListCategories => {
26      notes::list(&vault, false, None)
27    },
28    NotesArgs::ListNotes(category) => {
29      notes::list(&vault, true, Some(category.to_owned()))
30    },
31  }
32}
33
34pub fn match_args(matches: &ArgMatches) -> Option<NotesArgs> {
35  if let Some(matches) = matches.subcommand_matches("notes") {
36    if let Some(matches) = matches.subcommand_matches("edit") {
37      let category = matches.value_of("CATEGORY").unwrap().to_owned();
38      let name = matches.value_of("NAME").unwrap().to_owned();
39      return Some(NotesArgs::EditNote(category, name))
40    }
41    if let Some(matches) = matches.subcommand_matches("remove") {
42      let category = matches.value_of("CATEGORY").unwrap().to_owned();
43      let name = matches.value_of("NAME").map(|x| x.to_owned());
44      return Some(NotesArgs::RemoveNote(category, name))
45    }
46    if let Some(matches) = matches.subcommand_matches("list") {
47      let is_categories = matches.is_present("CATEGORIES");
48      if is_categories {
49        return Some(NotesArgs::ListCategories);
50      }
51      return
52        match matches.value_of("CATEGORY") {
53          Some(category) => Some(NotesArgs::ListNotes(category.to_owned())),
54          None => Some(NotesArgs::ListAll),
55        };
56    }
57  }
58  None
59}