diff options
| -rw-r--r-- | cli/src/main.rs | 9 | ||||
| -rw-r--r-- | cli/src/store.rs (renamed from cli/src/model.rs) | 63 | ||||
| -rw-r--r-- | cli/src/transaction.rs | 64 |
3 files changed, 70 insertions, 66 deletions
diff --git a/cli/src/main.rs b/cli/src/main.rs index 18d422a..10fa02c 100644 --- a/cli/src/main.rs +++ b/cli/src/main.rs @@ -4,7 +4,8 @@ use std::path::PathBuf; use std::str::FromStr; use structopt::StructOpt; -mod model; +mod store; +mod transaction; //TODO relative ("yesterday", "-2d", etc) fn parse_date(s: &str) -> Result<NaiveDate, String> { @@ -15,7 +16,7 @@ fn parse_date(s: &str) -> Result<NaiveDate, String> { #[derive(StructOpt)] enum Command { Insert { - kind: model::TransactionKind, + kind: transaction::TransactionKind, #[structopt(long)] account: String, @@ -61,7 +62,7 @@ struct Mn { } fn main() { - let mut store = model::Store::open(PathBuf::from("store")).unwrap(); + let mut store = store::Store::open(PathBuf::from("store")).unwrap(); let args = Mn::from_args(); eprintln!("{:?}", args); match args.command { @@ -73,7 +74,7 @@ fn main() { description, date, } => { - let transaction = model::Transaction { + let transaction = transaction::Transaction { kind, to: account, from: "Default".to_string(), diff --git a/cli/src/model.rs b/cli/src/store.rs index f4b9910..2bba9c8 100644 --- a/cli/src/model.rs +++ b/cli/src/store.rs @@ -1,50 +1,6 @@ -use chrono::naive::NaiveDate; -use rust_decimal::Decimal; -use serde::{Deserialize, Serialize}; -use std::convert::AsRef; -use std::fs; -use std::hash::{Hash, Hasher}; use std::path::{Path, PathBuf}; -use structopt::StructOpt; -use twox_hash::XxHash64; -type Account = String; -type Category = String; - -#[derive(Debug)] -#[derive(Hash)] -#[derive(Deserialize, Serialize)] -#[derive(StructOpt)] -pub enum TransactionKind { - Expense, - Income, - //TODO Transfer, -} - -impl std::str::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 { - pub description: String, - pub date: NaiveDate, - pub category: Category, - pub amount: Decimal, - pub kind: TransactionKind, - pub from: Account, - pub to: Account, -} +use crate::transaction::{Category, Transaction}; #[derive(Debug)] pub struct Store { @@ -104,20 +60,3 @@ impl Store { categories } } - -impl Transaction { - fn write<P: AsRef<Path>>(&self, p: &P) -> std::io::Result<()> { - fs::write(p, serde_json::to_string_pretty(self).unwrap()) //TODO control pretty or not - } - - //TODO Result - fn open<P: AsRef<Path>>(p: &P) -> Option<Self> { - fs::read_to_string(p).ok().as_ref().and_then(|s| serde_json::from_str(s).ok()) - } - - pub fn id(&self) -> u64 { - let mut h = XxHash64::default(); - self.hash(&mut h); - h.finish() - } -} diff --git a/cli/src/transaction.rs b/cli/src/transaction.rs new file mode 100644 index 0000000..117edfb --- /dev/null +++ b/cli/src/transaction.rs @@ -0,0 +1,64 @@ +use chrono::naive::NaiveDate; +use rust_decimal::Decimal; +use serde::{Deserialize, Serialize}; +use std::convert::AsRef; +use std::fs; +use std::hash::{Hash, Hasher}; +use std::path::Path; +use structopt::StructOpt; +use twox_hash::XxHash64; + +pub(crate) type Account = String; +pub(crate) type Category = String; + +#[derive(Debug)] +#[derive(Hash)] +#[derive(Deserialize, Serialize)] +#[derive(StructOpt)] +pub enum TransactionKind { + Expense, + Income, + //TODO Transfer, +} + +impl std::str::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 { + pub description: String, + pub date: NaiveDate, + pub category: Category, + pub amount: Decimal, + pub kind: TransactionKind, + pub from: Account, + pub to: Account, +} + +impl Transaction { + pub(crate) fn write<P: AsRef<Path>>(&self, p: &P) -> std::io::Result<()> { + fs::write(p, serde_json::to_string_pretty(self).unwrap()) //TODO control pretty or not + } + + //TODO Result + pub(crate) fn open<P: AsRef<Path>>(p: &P) -> Option<Self> { + fs::read_to_string(p).ok().as_ref().and_then(|s| serde_json::from_str(s).ok()) + } + + pub fn id(&self) -> u64 { + let mut h = XxHash64::default(); + self.hash(&mut h); + h.finish() + } +} |
