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 | |
| parent | ccda716764ae84dee9f18f66e383b66c9e30b3b9 (diff) | |
| download | money-f0dbf13ab231bfc913571487ae9394214a70f7b9.tar.gz | |
date
| -rw-r--r-- | cli/Cargo.lock | 35 | ||||
| -rw-r--r-- | cli/Cargo.toml | 1 | ||||
| -rw-r--r-- | cli/src/main.rs | 19 | ||||
| -rw-r--r-- | cli/src/model.rs | 4 |
4 files changed, 55 insertions, 4 deletions
diff --git a/cli/Cargo.lock b/cli/Cargo.lock index a60da7c..3f79032 100644 --- a/cli/Cargo.lock +++ b/cli/Cargo.lock @@ -47,6 +47,20 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822" [[package]] +name = "chrono" +version = "0.4.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "670ad68c9088c2a963aaa298cb369688cf3f9465ce5e2d4ca10e6e0098a1ce73" +dependencies = [ + "libc", + "num-integer", + "num-traits", + "serde", + "time", + "winapi", +] + +[[package]] name = "clap" version = "2.33.3" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -101,6 +115,7 @@ checksum = "320cfe77175da3a483efed4bc0adc1968ca050b098ce4f2f1c13a56626128790" name = "money-cli" version = "0.1.0" dependencies = [ + "chrono", "rust_decimal", "serde", "serde_json", @@ -109,6 +124,16 @@ dependencies = [ ] [[package]] +name = "num-integer" +version = "0.1.44" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d2cc698a63b549a70bc047073d2949cce27cd1c7b0a4a862d08a8031bc2801db" +dependencies = [ + "autocfg", + "num-traits", +] + +[[package]] name = "num-traits" version = "0.2.14" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -265,6 +290,16 @@ dependencies = [ ] [[package]] +name = "time" +version = "0.1.43" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ca8a50ef2360fbd1eeb0ecd46795a87a19024eb4b53c5dc916ca1fd95fe62438" +dependencies = [ + "libc", + "winapi", +] + +[[package]] name = "twox-hash" version = "1.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" diff --git a/cli/Cargo.toml b/cli/Cargo.toml index 0fe0680..0b5650a 100644 --- a/cli/Cargo.toml +++ b/cli/Cargo.toml @@ -6,6 +6,7 @@ edition = "2018" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +chrono = { version = "0.4.19", features = ["serde"] } rust_decimal = { version = "1.15.0", features = ["serde-arbitrary-precision"] } serde = { version = "1.0.126", features = ["derive"] } serde_json = "1.0.64" 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() |
