From f0dbf13ab231bfc913571487ae9394214a70f7b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustav=20S=C3=B6rn=C3=A4s?= Date: Thu, 29 Jul 2021 13:09:48 +0200 Subject: date --- cli/src/main.rs | 19 ++++++++++++++++--- cli/src/model.rs | 4 +++- 2 files changed, 19 insertions(+), 4 deletions(-) (limited to 'cli/src') 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::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, }, 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() -- cgit v1.2.1