From ccda716764ae84dee9f18f66e383b66c9e30b3b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustav=20S=C3=B6rn=C3=A4s?= Date: Thu, 29 Jul 2021 09:56:13 +0200 Subject: list categories --- cli/src/main.rs | 30 ++++++++++++++++++++++++++++-- cli/src/model.rs | 15 ++++++++++++--- 2 files changed, 40 insertions(+), 5 deletions(-) (limited to 'cli') diff --git a/cli/src/main.rs b/cli/src/main.rs index a760401..6de8dbd 100644 --- a/cli/src/main.rs +++ b/cli/src/main.rs @@ -23,6 +23,26 @@ enum Command { description: String, }, + List { + target: ListTarget, + } +} + +#[derive(Debug)] +#[derive(StructOpt)] +enum ListTarget { + Categories, +} + +impl std::str::FromStr for ListTarget { + type Err = String; + + fn from_str(s: &str) -> Result { + match s { + "categories" => Ok(ListTarget::Categories), + _ => Err(format!("Unknown listable: {:?}", s)), + } + } } #[derive(Debug)] @@ -36,8 +56,9 @@ struct Mn { fn main() { let mut store = model::Store::open(PathBuf::from("store")).unwrap(); - println!("{:?}", store); - match Mn::from_args().command { + let args = Mn::from_args(); + println!("{:?}", args); + match args.command { Command::Insert { kind, account, @@ -57,5 +78,10 @@ fn main() { store.push(transaction); store.write().unwrap(); } + Command::List { + target: ListTarget::Categories + } => { + println!("{:?}", store.categories()); + } } } diff --git a/cli/src/model.rs b/cli/src/model.rs index 01b645a..c70e43d 100644 --- a/cli/src/model.rs +++ b/cli/src/model.rs @@ -4,7 +4,6 @@ use std::convert::AsRef; use std::fs; use std::hash::{Hash, Hasher}; use std::path::{Path, PathBuf}; -use std::str::FromStr; use structopt::StructOpt; use twox_hash::XxHash64; @@ -21,7 +20,7 @@ pub enum TransactionKind { //TODO Transfer, } -impl FromStr for TransactionKind { +impl std::str::FromStr for TransactionKind { type Err = String; fn from_str(s: &str) -> Result { @@ -91,6 +90,17 @@ impl Store { } Ok(()) } + + pub fn categories(&self) -> Vec { + let mut categories: Vec<_> = self + .transactions + .iter() + .map(|t| t.category.clone()) + .collect(); + categories.sort(); + categories.dedup(); + categories + } } impl Transaction { @@ -109,4 +119,3 @@ impl Transaction { h.finish() } } - -- cgit v1.2.1