From 29d3813d14f32be9067a7a6d3040824637d8987e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustav=20S=C3=B6rn=C3=A4s?= Date: Thu, 29 Jul 2021 02:00:13 +0200 Subject: structopt for inserting posts --- cli/src/main.rs | 63 +++++++++++++++++++++++++++++++++++++++++++++++--------- cli/src/model.rs | 27 ++++++++++++++++++------ 2 files changed, 74 insertions(+), 16 deletions(-) (limited to 'cli/src') 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, + #[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 { + 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)] -- cgit v1.2.1