Skip to content

Commit

Permalink
lint
Browse files Browse the repository at this point in the history
  • Loading branch information
carderne committed Jan 20, 2024
1 parent 699ef27 commit 6ec526a
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 28 deletions.
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,7 @@ fmt:
.PHONY: test
test:
cargo test

.PHONY: lint
lint:
cargo clippy
13 changes: 5 additions & 8 deletions src/balance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,13 @@ use std::collections::HashMap;
pub fn get_balances(directives: Vec<Directive>) -> AccBal {
let mut bals: AccBal = HashMap::new();
for d in directives {
match d {
Directive::Transaction(tx) => {
for p in tx.postings {
if let Some(amount) = p.amount {
let entry = bals.entry(p.account).or_insert_with(HashMap::new);
*entry.entry(amount.ccy.clone()).or_insert(0.0) += amount.number;
}
if let Directive::Transaction(tx) = d {
for p in tx.postings {
if let Some(amount) = p.amount {
let entry = bals.entry(p.account).or_default();
*entry.entry(amount.ccy.clone()).or_insert(0.0) += amount.number;
}
}
_ => (),
}
}
bals
Expand Down
9 changes: 4 additions & 5 deletions src/book.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,10 @@ fn balance_transaction(tx: &mut Transaction) {
tx.postings = postings;
}

pub fn balance_transactions(directives: &mut Vec<Directive>) {
for d in directives {
match d {
Directive::Transaction(d) => balance_transaction(d),
_ => (),
pub fn balance_transactions(directives: &mut [Directive]) {
for d in directives.iter_mut() {
if let Directive::Transaction(tx) = d {
balance_transaction(tx);
}
}
}
24 changes: 12 additions & 12 deletions src/directives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,12 @@ impl fmt::Display for ConfigCustom {
}

#[derive(Debug, PartialEq)]
pub struct EOI {
pub struct Eoi {
date: String,
debug: Debug,
}

impl EOI {
impl Eoi {
pub fn from_entry(entry: Pair<Rule>) -> Self {
let (line, _) = entry.line_col();
let debug = Debug { line };
Expand All @@ -91,7 +91,7 @@ impl EOI {
}
}

impl fmt::Display for EOI {
impl fmt::Display for Eoi {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{debug}-- EOI", debug = self.debug)
}
Expand Down Expand Up @@ -177,7 +177,7 @@ impl Commodity {
let date = pairs.next().unwrap().as_str().to_string();
let ccy = pairs.next().unwrap().as_str().to_string();
let mut meta: Vec<Metadata> = Vec::new();
while let Some(pair) = pairs.next() {
for pair in pairs {
if pair.as_rule() == Rule::metadata {
let p = Metadata::from_entry(pair);
meta.push(p)
Expand Down Expand Up @@ -455,7 +455,7 @@ impl Posting {
pub fn from_entry(entry: Pair<Rule>) -> Self {
let mut pairs = entry.clone().into_inner();
let account = pairs.next().unwrap().as_str().to_string();
let amount = if let Some(_) = pairs.peek() {
let amount = if pairs.peek().is_some() {
Some(Amount::from_entry(pairs.next().unwrap()))
} else {
None
Expand Down Expand Up @@ -505,9 +505,9 @@ pub fn get_payee_narration(pairs: &mut Pairs<Rule>) -> (Option<String>, String)
let first_val = pairs.next().unwrap().as_str().to_string();
if pairs.peek().unwrap().as_rule() == Rule::narration {
let narration = pairs.next().unwrap().as_str().to_string();
return (Some(first_val), narration);
(Some(first_val), narration)
} else {
return (None, first_val);
(None, first_val)
}
}

Expand All @@ -519,7 +519,7 @@ impl Transaction {
let (payee, narration) = get_payee_narration(&mut pairs);
let mut postings: Vec<Posting> = Vec::new();
let mut meta: Vec<Metadata> = Vec::new();
while let Some(pair) = pairs.next() {
for pair in pairs {
if pair.as_rule() == Rule::posting {
postings.push(Posting::from_entry(pair));
} else if pair.as_rule() == Rule::metadata {
Expand Down Expand Up @@ -576,7 +576,7 @@ impl fmt::Display for Transaction {
}

pub enum Directive {
EOI(EOI),
Eoi(Eoi),
ConfigCustom(ConfigCustom),
ConfigOption(ConfigOption),
Commodity(Commodity),
Expand All @@ -592,7 +592,7 @@ pub enum Directive {
impl Directive {
pub fn date(&self) -> &str {
match self {
Directive::EOI(d) => &d.date,
Directive::Eoi(d) => &d.date,
Directive::ConfigCustom(d) => &d.date,
Directive::ConfigOption(d) => &d.date,
Directive::Commodity(d) => &d.date,
Expand All @@ -607,7 +607,7 @@ impl Directive {
}
pub fn order(&self) -> i8 {
match self {
Directive::EOI(_) => 0,
Directive::Eoi(_) => 0,
Directive::ConfigCustom(_) => 0,
Directive::ConfigOption(_) => 0,
Directive::Commodity(_) => 0,
Expand All @@ -625,7 +625,7 @@ impl Directive {
impl fmt::Display for Directive {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Directive::EOI(d) => write!(f, "{d}"),
Directive::Eoi(d) => write!(f, "{d}"),
Directive::ConfigCustom(d) => write!(f, "{d}"),
Directive::ConfigOption(d) => write!(f, "{d}"),
Directive::Commodity(d) => write!(f, "{d}"),
Expand Down
6 changes: 3 additions & 3 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@ pub fn consume(entries: Pairs<'_, Rule>) -> Vec<Directive> {
Rule::transaction => {
Directive::Transaction(directives::Transaction::from_entry(entry))
}
Rule::EOI => Directive::EOI(directives::EOI::from_entry(entry)),
Rule::EOI => Directive::Eoi(directives::Eoi::from_entry(entry)),
_ => unreachable!("no rule for this entry!"),
}
})
.collect()
}

pub fn sort(directives: &mut Vec<Directive>) {
directives.sort_by(|a, b| match a.date().cmp(&b.date()) {
pub fn sort(directives: &mut [Directive]) {
directives.sort_by(|a, b| match a.date().cmp(b.date()) {
Ordering::Equal => a.order().cmp(&b.order()),
other => other,
});
Expand Down

0 comments on commit 6ec526a

Please sign in to comment.