diff options
| author | Gustav Sörnäs <gustav@sornas.net> | 2021-07-29 02:00:13 +0200 |
|---|---|---|
| committer | Gustav Sörnäs <gustav@sornas.net> | 2021-07-29 02:00:13 +0200 |
| commit | 29d3813d14f32be9067a7a6d3040824637d8987e (patch) | |
| tree | f42971de9e7d8f555463ed0673bb84e3971e73e0 | |
| parent | 447647a01d345246afd8b103be0f4ff506f45bcc (diff) | |
| download | money-29d3813d14f32be9067a7a6d3040824637d8987e.tar.gz | |
structopt for inserting posts
| -rw-r--r-- | cli/Cargo.lock | 181 | ||||
| -rw-r--r-- | cli/Cargo.toml | 1 | ||||
| -rw-r--r-- | cli/src/main.rs | 63 | ||||
| -rw-r--r-- | cli/src/model.rs | 27 |
4 files changed, 256 insertions, 16 deletions
diff --git a/cli/Cargo.lock b/cli/Cargo.lock index b69f6d6..a60da7c 100644 --- a/cli/Cargo.lock +++ b/cli/Cargo.lock @@ -3,36 +3,108 @@ version = 3 [[package]] +name = "ansi_term" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ee49baf6cb617b853aa8d93bf420db2383fab46d314482ca2803b40d5fde979b" +dependencies = [ + "winapi", +] + +[[package]] name = "arrayvec" version = "0.5.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "23b62fc65de8e4e7f52534fb52b0f3ed04746ae267519eef2a83941e8085068b" [[package]] +name = "atty" +version = "0.2.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8" +dependencies = [ + "hermit-abi", + "libc", + "winapi", +] + +[[package]] name = "autocfg" version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a" [[package]] +name = "bitflags" +version = "1.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693" + +[[package]] name = "cfg-if" version = "0.1.10" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822" [[package]] +name = "clap" +version = "2.33.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "37e58ac78573c40708d45522f0d80fa2f01cc4f9b4e2bf749807255454312002" +dependencies = [ + "ansi_term", + "atty", + "bitflags", + "strsim", + "textwrap", + "unicode-width", + "vec_map", +] + +[[package]] +name = "heck" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6d621efb26863f0e9924c6ac577e8275e5e6b77455db64ffa6c65c904e9e132c" +dependencies = [ + "unicode-segmentation", +] + +[[package]] +name = "hermit-abi" +version = "0.1.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33" +dependencies = [ + "libc", +] + +[[package]] name = "itoa" version = "0.4.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dd25036021b0de88a0aff6b850051563c6516d0bf53f8638938edbb9de732736" [[package]] +name = "lazy_static" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" + +[[package]] +name = "libc" +version = "0.2.98" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "320cfe77175da3a483efed4bc0adc1968ca050b098ce4f2f1c13a56626128790" + +[[package]] name = "money-cli" version = "0.1.0" dependencies = [ "rust_decimal", "serde", "serde_json", + "structopt", "twox-hash", ] @@ -46,6 +118,30 @@ dependencies = [ ] [[package]] +name = "proc-macro-error" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "da25490ff9892aab3fcf7c36f08cfb902dd3e71ca0f9f9517bea02a73a5ce38c" +dependencies = [ + "proc-macro-error-attr", + "proc-macro2", + "quote", + "syn", + "version_check", +] + +[[package]] +name = "proc-macro-error-attr" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a1be40180e52ecc98ad80b184934baf3d0d29f979574e439af5a55274b35f869" +dependencies = [ + "proc-macro2", + "quote", + "version_check", +] + +[[package]] name = "proc-macro2" version = "1.0.28" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -119,6 +215,36 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f" [[package]] +name = "strsim" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a" + +[[package]] +name = "structopt" +version = "0.3.22" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "69b041cdcb67226aca307e6e7be44c8806423d83e018bd662360a93dabce4d71" +dependencies = [ + "clap", + "lazy_static", + "structopt-derive", +] + +[[package]] +name = "structopt-derive" +version = "0.4.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7813934aecf5f51a54775e00068c237de98489463968231a51746bbbc03f9c10" +dependencies = [ + "heck", + "proc-macro-error", + "proc-macro2", + "quote", + "syn", +] + +[[package]] name = "syn" version = "1.0.74" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -130,6 +256,15 @@ dependencies = [ ] [[package]] +name = "textwrap" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d326610f408c7a4eb6f51c37c330e496b08506c9457c9d34287ecc38809fb060" +dependencies = [ + "unicode-width", +] + +[[package]] name = "twox-hash" version = "1.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -140,7 +275,53 @@ dependencies = [ ] [[package]] +name = "unicode-segmentation" +version = "1.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8895849a949e7845e06bd6dc1aa51731a103c42707010a5b591c0038fb73385b" + +[[package]] +name = "unicode-width" +version = "0.1.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9337591893a19b88d8d87f2cec1e73fad5cdfd10e5a6f349f498ad6ea2ffb1e3" + +[[package]] name = "unicode-xid" version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8ccb82d61f80a663efe1f787a51b16b5a51e3314d6ac365b08639f52387b33f3" + +[[package]] +name = "vec_map" +version = "0.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f1bddf1187be692e79c5ffeab891132dfb0f236ed36a43c7ed39f1165ee20191" + +[[package]] +name = "version_check" +version = "0.9.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5fecdca9a5291cc2b8dcf7dc02453fee791a280f3743cb0905f8822ae463b3fe" + +[[package]] +name = "winapi" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" +dependencies = [ + "winapi-i686-pc-windows-gnu", + "winapi-x86_64-pc-windows-gnu", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" + +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" diff --git a/cli/Cargo.toml b/cli/Cargo.toml index bdaa3e3..0fe0680 100644 --- a/cli/Cargo.toml +++ b/cli/Cargo.toml @@ -9,4 +9,5 @@ edition = "2018" rust_decimal = { version = "1.15.0", features = ["serde-arbitrary-precision"] } serde = { version = "1.0.126", features = ["derive"] } serde_json = "1.0.64" +structopt = "0.3.22" twox-hash = { version = "1.6.0", default-features = false } diff --git a/cli/src/main.rs b/cli/src/main.rs index 9c6edb6..606d3c1 100644 --- a/cli/src/main.rs +++ b/cli/src/main.rs @@ -1,18 +1,61 @@ +use rust_decimal::Decimal; use std::path::PathBuf; +use std::str::FromStr; +use structopt::StructOpt; mod model; +#[derive(Debug)] +#[derive(StructOpt)] +enum Command { + Insert { + kind: model::TransactionKind, + #[structopt(long)] + account: String, + + // #[structopt(long = "category", multiple = true, number_of_values = 1)] + // category: Vec<String>, + #[structopt(long)] + category: String, + + #[structopt(long, parse(try_from_str = Decimal::from_str))] + amount: Decimal, + + description: String, + }, +} + +#[derive(Debug)] +#[derive(StructOpt)] +struct Mn { + #[structopt(subcommand)] + command: Command, +} + +// mn insert expense --account Kortkonto --category a --category b + fn main() { - // let post = Post::new(Transaction { - // description: "Test".to_string(), - // category: "Mat".to_string(), - // amount: Decimal::try_from(120.4_f64).unwrap(), - // kind: TransactionKind::Expense, - // from: "Kortkonto".to_string(), - // to: "Coop Lambohov".to_string(), - // }); + match Mn::from_args().command { + Command::Insert { + kind, + account, + category, + amount, + description, + } => { + let post = model::Post::new(model::Transaction { + kind, + to: account, + from: "Default".to_string(), + category, + amount, + description + }); + println!("{:?}", post); + } + } // post.write(&"test").unwrap(); - let store = model::Store::open(PathBuf::from("store")).unwrap(); + // let store = model::Store::open(PathBuf::from("store")).unwrap(); // println!("{:#?}", store); - store.write().unwrap(); + // store.write().unwrap(); } diff --git a/cli/src/model.rs b/cli/src/model.rs index b0dab7a..c93897f 100644 --- a/cli/src/model.rs +++ b/cli/src/model.rs @@ -4,6 +4,8 @@ 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; type Account = String; @@ -12,22 +14,35 @@ type Category = String; #[derive(Debug)] #[derive(Hash)] #[derive(Deserialize, Serialize)] +#[derive(StructOpt)] pub enum TransactionKind { Expense, Income, //TODO Transfer, } +impl FromStr for TransactionKind { + type Err = String; + + fn from_str(s: &str) -> Result<Self, Self::Err> { + match s { + "expense" => Ok(TransactionKind::Expense), + "income" => Ok(TransactionKind::Income), + _ => Err(format!("Unknown transaction kind: {:?}", s)), + } + } +} + #[derive(Debug)] #[derive(Hash)] #[derive(Deserialize, Serialize)] pub struct Transaction { - description: String, - category: Category, - amount: Decimal, - kind: TransactionKind, - from: Account, - to: Account, + pub description: String, + pub category: Category, + pub amount: Decimal, + pub kind: TransactionKind, + pub from: Account, + pub to: Account, } #[derive(Debug)] |
