Skip to content

Commit

Permalink
enable command to use a cached ledger #116
Browse files Browse the repository at this point in the history
  • Loading branch information
frosklis committed Aug 16, 2021
1 parent 32fef41 commit a14602f
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 37 deletions.
10 changes: 5 additions & 5 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -329,28 +329,28 @@ fn execute_command(opt: Opt, maybe_ledger: Option<Ledger>) -> Result<(), ()> {
env::set_var("CLICOLOR_FORCE", "1");
}

commodities::execute(options.input_file.clone(), &options)
commodities::execute(&options, maybe_ledger)
}
Command::Payees(options) => {
if options.force_color {
env::set_var("CLICOLOR_FORCE", "1");
}

payees::execute(options.input_file.clone(), &options)
payees::execute(&options, maybe_ledger)
}
Command::Prices(options) => prices::execute(options.input_file.clone(), &options),
Command::Prices(options) => prices::execute(&options, maybe_ledger),
Command::Accounts(options) => {
if options.force_color {
env::set_var("CLICOLOR_FORCE", "1");
}

accounts::execute(options.input_file.clone(), &options)
accounts::execute(&options, maybe_ledger)
}
Command::Statistics(options) => {
if options.force_color {
env::set_var("CLICOLOR_FORCE", "1");
}
statistics::execute(options.input_file.clone(), &options)
statistics::execute(&options, maybe_ledger)
}
} {
let err_str = format!("{}", e);
Expand Down
14 changes: 7 additions & 7 deletions src/commands/accounts.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
use std::path::PathBuf;
use std::convert::TryFrom;

use crate::models::{Account, HasName};
use crate::parser::Tokenizer;
use crate::models::{Account, HasName, Ledger};
use crate::{error::Error, CommonOpts};
use std::ops::Deref;

pub fn execute(path: PathBuf, options: &CommonOpts) -> Result<(), Error> {
let mut tokenizer: Tokenizer = Tokenizer::from(&path);
let items = tokenizer.tokenize(options);
let ledger = items.to_ledger(options)?;
pub fn execute(options: &CommonOpts, maybe_ledger: Option<Ledger>) -> Result<(), Error> {
let ledger = match maybe_ledger {
Some(ledger) => ledger,
None => Ledger::try_from(options)?,
};
let mut accounts = ledger
.accounts
.iter()
Expand Down
14 changes: 8 additions & 6 deletions src/commands/commodities.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
use crate::parser::Tokenizer;
use std::convert::TryFrom;

use crate::models::Ledger;
use crate::Error;
use crate::{
models::{Currency, HasName},
CommonOpts,
};
use std::ops::Deref;
use std::path::PathBuf;

pub fn execute(path: PathBuf, options: &CommonOpts) -> Result<(), Error> {
let mut tokenizer: Tokenizer = Tokenizer::from(&path);
let items = tokenizer.tokenize(options);
let ledger = items.to_ledger(options)?;
pub fn execute(options: &CommonOpts, maybe_ledger: Option<Ledger>) -> Result<(), Error> {
let ledger = match maybe_ledger {
Some(ledger) => ledger,
None => Ledger::try_from(options)?,
};
let mut commodities = ledger
.commodities
.iter()
Expand Down
13 changes: 7 additions & 6 deletions src/commands/payees.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
use crate::parser::Tokenizer;
use crate::models::Ledger;
use crate::Error;
use crate::{
models::{HasName, Payee},
CommonOpts,
};
use std::convert::TryFrom;
use std::ops::Deref;
use std::path::PathBuf;

pub fn execute(path: PathBuf, options: &CommonOpts) -> Result<(), Error> {
let mut tokenizer: Tokenizer = Tokenizer::from(&path);
let items = tokenizer.tokenize(options);
let ledger = items.to_ledger(options)?;
pub fn execute(options: &CommonOpts, maybe_ledger: Option<Ledger>) -> Result<(), Error> {
let ledger = match maybe_ledger {
Some(ledger) => ledger,
None => Ledger::try_from(options)?,
};
let mut payees = ledger
.payees
.iter()
Expand Down
15 changes: 8 additions & 7 deletions src/commands/prices.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
use std::convert::TryFrom;
use std::ops::Deref;
use std::path::PathBuf;

use crate::models::Ledger;
use crate::CommonOpts;
use crate::Error;
use crate::{parser::Tokenizer, CommonOpts};

pub fn execute(path: PathBuf, options: &CommonOpts) -> Result<(), Error> {
let mut tokenizer: Tokenizer = Tokenizer::from(&path);
let items = tokenizer.tokenize(options);

let ledger = items.to_ledger(options)?;
pub fn execute(options: &CommonOpts, maybe_ledger: Option<Ledger>) -> Result<(), Error> {
let ledger = match maybe_ledger {
Some(ledger) => ledger,
None => Ledger::try_from(options)?,
};
for price in ledger.prices.deref() {
println!("{}", price);
}
Expand Down
13 changes: 7 additions & 6 deletions src/commands/statistics.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
use std::path::PathBuf;
use std::convert::TryFrom;

use crate::parser::Tokenizer;
use crate::models::Ledger;
use crate::{error::Error, CommonOpts};

/// Statistics command
///
/// Prints summary statistics from the ledger
pub fn execute(path: PathBuf, options: &CommonOpts) -> Result<(), Error> {
let mut tokenizer: Tokenizer = Tokenizer::from(&path);
let items = tokenizer.tokenize(options);
let ledger = items.to_ledger(options)?;
pub fn execute(options: &CommonOpts, maybe_ledger: Option<Ledger>) -> Result<(), Error> {
let ledger = match maybe_ledger {
Some(ledger) => ledger,
None => Ledger::try_from(options)?,
};

// Number of transactions
let mut num_postings = 0;
Expand Down

0 comments on commit a14602f

Please sign in to comment.