Skip to content
This repository has been archived by the owner on Jun 10, 2024. It is now read-only.

Restructuring #13

Merged
merged 20 commits into from
Jul 31, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
285 changes: 24 additions & 261 deletions Cargo.lock

Large diffs are not rendered by default.

13 changes: 4 additions & 9 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "rust_ledger"
version = "0.2.0"
version = "0.3.0"
authors = ["ebcrowder <[email protected]>"]
edition = "2018"
license = "GPL-3.0"
Expand All @@ -11,14 +11,9 @@ repository = "https://github.com/ebcrowder/rust_ledger"
readme = "README.md"

[dependencies]
serde_yaml = "0.8.12"
serde = { version = "1.0.110", features = ["derive"] }
serde_yaml = "0.8.13"
serde = { version = "1.0.114", features = ["derive"] }
csv = "1.1.3"
pargs = "0.1.4"
colored = "1.9.3"
colored = "2.0.0"
monee = "0.0.5"

[dev-dependencies]
assert_cmd = "1.0.1"
predicates = "1.0.4"
tempfile = "3.1.0"
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ RLEDGER_FILE=~/rledger.yaml rust_ledger balances
export RLEDGER_FILE="$HOME/rledger.yaml"
```

COMMAND - ledger command (accounts, balance, register, or csv)
COMMAND - ledger command (account, balance, register, or csv)

OPTION (denoted by `-f`) - allows you to filter the output of the `register` command by account type. For example, if you wish to only see "expense" transactions in the output, you would pass in `expense` as the option here.

Expand Down Expand Up @@ -104,7 +104,7 @@ Transactions that only involve two accounts can also be expressed in the above f

### API

- accounts
- account
- lists accounts
- example output:

Expand Down
33 changes: 0 additions & 33 deletions src/accounts.rs

This file was deleted.

154 changes: 0 additions & 154 deletions src/balance.rs

This file was deleted.

56 changes: 56 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
mod account;
mod balance;
mod csv;
mod register;

use crate::error::{Error, Result};
use pargs;
use std::env;

pub fn run() -> Result<()> {
let args: Vec<String> = env::args().collect();

// define expected args for pargs
let command_args: Vec<String> = vec![
String::from("account"),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this change going to singular. Clean and consistent with the other commands.

String::from("balance"),
String::from("register"),
String::from("csv"),
];
let flag_args: Vec<String> = vec![];
let option_args: Vec<String> = vec![String::from("-l"), String::from("-f")];

// pargs will parse the args and return the result
let pargs_result = pargs::parse(args, command_args, flag_args, option_args)?;

let pargs_options = pargs_result.option_args;
let pargs_commands = pargs_result.command_args;

let ledger_file = match pargs_options.get("-l") {
Some(value) => value.to_string(),
None => {
let ledger_file_env = match std::env::var("RLEDGER_FILE") {
Ok(p) => format!("{}", p),
Err(err) => format!("{}", err),
};

ledger_file_env.to_string()
}
};

let options_arg = match pargs_options.get("-f") {
Some(value) => value,
None => "",
};

match &pargs_commands.len() {
0 => Err(Error::InvalidArg("please enter a command.".to_string())),
_ => match &pargs_commands[0][..] {
"account" => account::account(&ledger_file.to_string()),
"balance" => balance::balance(&ledger_file.to_string()),
"register" => register::register(&ledger_file.to_string(), &options_arg.to_string()),
"csv" => csv::csv(&ledger_file.to_string(), &options_arg.to_string()),
_ => panic!("command not found.".to_string()),
},
}
}
14 changes: 14 additions & 0 deletions src/cli/account.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
extern crate serde_yaml;

use crate::error::Result;
use crate::model::ledger::LedgerFile;

/// returns all general ledger accounts
pub fn account(filename: &String) -> Result<()> {
let file = std::fs::File::open(filename)?;
let deserialized_file: LedgerFile = serde_yaml::from_reader(file).unwrap();

LedgerFile::print_accounts(deserialized_file);

Ok(())
}
14 changes: 14 additions & 0 deletions src/cli/balance.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
extern crate serde_yaml;

use crate::error::Result;
use crate::model::ledger::LedgerFile;

/// returns balances of all general ledger accounts
pub fn balance(filename: &String) -> Result<()> {
let file = std::fs::File::open(filename)?;
let deserialized_file: LedgerFile = serde_yaml::from_reader(file).unwrap();

LedgerFile::print_balances(deserialized_file);

Ok(())
}
5 changes: 3 additions & 2 deletions src/csv.rs → src/cli/csv.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
extern crate csv;

use super::models::LedgerFile;
use crate::error::Error;
use crate::model::ledger::LedgerFile;
use serde::{Deserialize, Serialize};
use std::{fs, io::Write};

Expand Down Expand Up @@ -59,7 +60,7 @@ fn insert_match_acct(csv_matches: &[CSVMatches], record: &CSV) -> String {
}

/// convert csv to yaml format
pub fn csv(ledger_file: &String, csv_file: &String) -> Result<(), std::io::Error> {
pub fn csv(ledger_file: &String, csv_file: &String) -> Result<(), Error> {
// open csv file
let raw_csv_file = fs::File::open(csv_file)?;
let mut csv_reader = csv::Reader::from_reader(raw_csv_file);
Expand Down
14 changes: 14 additions & 0 deletions src/cli/register.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
extern crate serde_yaml;

use crate::error::Result;
use crate::model::ledger::LedgerFile;

/// returns all general ledger transactions
pub fn register(filename: &String, option: &String) -> Result<()> {
let file = std::fs::File::open(filename)?;
let deserialized_file: LedgerFile = serde_yaml::from_reader(file).unwrap();

LedgerFile::print_register(deserialized_file, option);

Ok(())
}
Loading