summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGustav Sörnäs <gustav@sornas.net>2021-07-29 09:56:13 +0200
committerGustav Sörnäs <gustav@sornas.net>2021-07-29 10:19:31 +0200
commitccda716764ae84dee9f18f66e383b66c9e30b3b9 (patch)
tree40d5b679d76308233f599c57a4751b5ca14d1390
parent5216a6a67d1bde60f4c872b8d8d966efe02dd4e4 (diff)
downloadmoney-ccda716764ae84dee9f18f66e383b66c9e30b3b9.tar.gz
list categories
-rw-r--r--cli/src/main.rs30
-rw-r--r--cli/src/model.rs15
2 files changed, 40 insertions, 5 deletions
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<Self, Self::Err> {
+ 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<Self, Self::Err> {
@@ -91,6 +90,17 @@ impl Store {
}
Ok(())
}
+
+ pub fn categories(&self) -> Vec<Category> {
+ 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()
}
}
-