mj 0.4.3

My Journal - personal tool to capture ideas, work with journals, notes and tasks in your favourite text $EDITOR.
Documentation
use clap::ArgMatches;
use crate::core::*;

#[derive(Debug)]
pub enum NotesArgs {
  EditNote(String, String),
  RemoveNote(String, Option<String>),
  ListAll,
  ListCategories,
  ListNotes(String),
}

pub fn run(vault: &Vault, args: &NotesArgs) -> Result<()> {
  match args {
    NotesArgs::EditNote(category, name) => {
      let _saved = notes::edit(&vault, &category, &name)?;
      Ok(())
    },
    NotesArgs::RemoveNote(category, name) => {
      notes::remove(&vault, &category, &name)
    },
    NotesArgs::ListAll => {
      notes::list(&vault, true, None)
    },
    NotesArgs::ListCategories => {
      notes::list(&vault, false, None)
    },
    NotesArgs::ListNotes(category) => {
      notes::list(&vault, true, Some(category.to_owned()))
    },
  }
}

pub fn match_args(matches: &ArgMatches) -> Option<NotesArgs> {
  if let Some(matches) = matches.subcommand_matches("notes") {
    if let Some(matches) = matches.subcommand_matches("edit") {
      let category = matches.value_of("CATEGORY").unwrap().to_owned();
      let name = matches.value_of("NAME").unwrap().to_owned();
      return Some(NotesArgs::EditNote(category, name))
    }
    if let Some(matches) = matches.subcommand_matches("remove") {
      let category = matches.value_of("CATEGORY").unwrap().to_owned();
      let name = matches.value_of("NAME").map(|x| x.to_owned());
      return Some(NotesArgs::RemoveNote(category, name))
    }
    if let Some(matches) = matches.subcommand_matches("list") {
      let is_categories = matches.is_present("CATEGORIES");
      if is_categories {
        return Some(NotesArgs::ListCategories);
      }
      return
        match matches.value_of("CATEGORY") {
          Some(category) => Some(NotesArgs::ListNotes(category.to_owned())),
          None => Some(NotesArgs::ListAll),
        };
    }
  }
  None
}