diff options
| author | Gustav Sörnäs <gustav@sornas.net> | 2021-07-29 13:09:48 +0200 |
|---|---|---|
| committer | Gustav Sörnäs <gustav@sornas.net> | 2021-07-29 13:17:09 +0200 |
| commit | f0dbf13ab231bfc913571487ae9394214a70f7b9 (patch) | |
| tree | bfb3cc38e65e999e73b2a1a56bc0b9c7bb31cc0e /cli/src | |
| parent | ccda716764ae84dee9f18f66e383b66c9e30b3b9 (diff) | |
| download | money-f0dbf13ab231bfc913571487ae9394214a70f7b9.tar.gz | |
date
Diffstat (limited to 'cli/src')
| -rw-r--r-- | cli/src/main.rs | 19 | ||||
| -rw-r--r-- | cli/src/model.rs | 4 |
2 files changed, 19 insertions, 4 deletions
diff --git a/cli/src/main.rs b/cli/src/main.rs index 6de8dbd..5a77ff4 100644 --- a/cli/src/main.rs +++ b/cli/src/main.rs @@ -1,3 +1,4 @@ +use chrono::naive::NaiveDate; use rust_decimal::Decimal; use std::path::PathBuf; use std::str::FromStr; @@ -5,6 +6,10 @@ use structopt::StructOpt; mod model; +fn parse_date(s: &str) -> Result<NaiveDate, String> { + NaiveDate::parse_from_str(s, "%Y-%m-%d").map_err(|e| e.to_string()) +} + #[derive(Debug)] #[derive(StructOpt)] enum Command { @@ -22,6 +27,8 @@ enum Command { amount: Decimal, description: String, + #[structopt(long, parse(try_from_str = parse_date))] + date: Option<NaiveDate>, }, List { target: ListTarget, @@ -57,7 +64,7 @@ struct Mn { fn main() { let mut store = model::Store::open(PathBuf::from("store")).unwrap(); let args = Mn::from_args(); - println!("{:?}", args); + eprintln!("{:?}", args); match args.command { Command::Insert { kind, @@ -65,6 +72,7 @@ fn main() { category, amount, description, + date, } => { let transaction = model::Transaction { kind, @@ -72,9 +80,14 @@ fn main() { from: "Default".to_string(), category, amount, - description + description, + date: match date { + Some(date) => date, + None => chrono::offset::Local::today().naive_utc(), + }, }; - println!("{:?}", transaction); + eprintln!("{:?}", transaction); + println!("{}", transaction.id()); store.push(transaction); store.write().unwrap(); } diff --git a/cli/src/model.rs b/cli/src/model.rs index c70e43d..f4b9910 100644 --- a/cli/src/model.rs +++ b/cli/src/model.rs @@ -1,3 +1,4 @@ +use chrono::naive::NaiveDate; use rust_decimal::Decimal; use serde::{Deserialize, Serialize}; use std::convert::AsRef; @@ -37,6 +38,7 @@ impl std::str::FromStr for TransactionKind { #[derive(Deserialize, Serialize)] pub struct Transaction { pub description: String, + pub date: NaiveDate, pub category: Category, pub amount: Decimal, pub kind: TransactionKind, @@ -113,7 +115,7 @@ impl Transaction { fs::read_to_string(p).ok().as_ref().and_then(|s| serde_json::from_str(s).ok()) } - fn id(&self) -> u64 { + pub fn id(&self) -> u64 { let mut h = XxHash64::default(); self.hash(&mut h); h.finish() |
