summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGustav Sörnäs <gustav@sornas.net>2021-08-03 15:31:40 +0200
committerGustav Sörnäs <gustav@sornas.net>2021-08-03 15:32:06 +0200
commit469e084e70fbdcc62b375e3091da6cde53a338fb (patch)
tree49128e9316021781bf21469f520a714a56fdcdce
parenteba7f10c8f389250a6a89062dff006a77a2cafc0 (diff)
downloadmoney-469e084e70fbdcc62b375e3091da6cde53a338fb.tar.gz
cargo fmt
-rw-r--r--cli/rustfmt.toml1
-rw-r--r--cli/src/main.rs7
-rw-r--r--cli/src/search.rs208
-rw-r--r--cli/src/transaction.rs5
4 files changed, 106 insertions, 115 deletions
diff --git a/cli/rustfmt.toml b/cli/rustfmt.toml
new file mode 100644
index 0000000..d4fcd2a
--- /dev/null
+++ b/cli/rustfmt.toml
@@ -0,0 +1 @@
+merge_derives = false
diff --git a/cli/src/main.rs b/cli/src/main.rs
index bf19f29..7b5f9e2 100644
--- a/cli/src/main.rs
+++ b/cli/src/main.rs
@@ -128,14 +128,11 @@ fn main() {
store.write().unwrap();
}
Command::List {
- target: ListTarget::Categories
+ target: ListTarget::Categories,
} => {
println!("{}", store.categories().join("\n"));
}
- Command::Show {
- sort,
- filters,
- } => {
+ Command::Show { sort, filters } => {
let mut search = Search::new(&store.transactions());
if !filters.is_empty() {
let filters = parse_filters(&filters.join(" "));
diff --git a/cli/src/search.rs b/cli/src/search.rs
index 744dc67..99a18bd 100644
--- a/cli/src/search.rs
+++ b/cli/src/search.rs
@@ -24,37 +24,28 @@ impl DateIsh {
fn parse(i: &str) -> nom::IResult<&str, Self> {
alt((
map(
- map_res(
- take(10usize),
- |s| NaiveDate::parse_from_str(s, "%Y-%m-%d"),
- ),
- DateIsh::Absolute
+ map_res(take(10usize), |s| NaiveDate::parse_from_str(s, "%Y-%m-%d")),
+ DateIsh::Absolute,
),
- map(
- Self::parse_relative,
- DateIsh::Relative
- )
+ map(Self::parse_relative, DateIsh::Relative),
))(i)
}
fn parse_relative(i: &str) -> nom::IResult<&str, Duration> {
map(
- pair(
- digit1,
- anychar
- ),
+ pair(digit1, anychar),
|(amount, unit): (&str, char)| match unit {
'd' => Duration::days(amount.parse().unwrap()),
'w' => Duration::days(amount.parse().unwrap()),
_ => unimplemented!(),
- }
+ },
)(i)
}
pub fn get(self) -> NaiveDate {
match self {
DateIsh::Absolute(date) => date,
- DateIsh::Relative(offset) => chrono::offset::Local::today().naive_utc() + offset
+ DateIsh::Relative(offset) => chrono::offset::Local::today().naive_utc() + offset,
}
}
}
@@ -95,12 +86,9 @@ fn parse_comparison(i: &str) -> nom::IResult<&str, (Decimal, Comparison)> {
tag("<="),
tag(""),
)),
- |comparison| Comparison::try_from(comparison).unwrap()
- )(i)?;
- let (i, amount) = map_res(
- digit1,
- Decimal::from_str
+ |comparison| Comparison::try_from(comparison).unwrap(),
)(i)?;
+ let (i, amount) = map_res(digit1, Decimal::from_str)(i)?;
Ok((i, (amount, comparison)))
}
@@ -128,68 +116,33 @@ impl Constraint {
Comparison::GreaterOrEqual => &transaction.amount >= amount,
Comparison::Less => &transaction.amount < amount,
Comparison::LessOrEqual => &transaction.amount <= amount,
- }
+ },
Constraint::Filters(filters) => {
- filters
- .iter()
- .fold(true, |include, filter| match filter {
- Filter::Union(constraint) => include || constraint.satisfies(transaction),
- Filter::Intersect(constraint) => include && constraint.satisfies(transaction),
- Filter::Subtract(constraint) => include && !constraint.satisfies(transaction),
- }
- )
+ filters.iter().fold(true, |include, filter| match filter {
+ Filter::Union(constraint) => include || constraint.satisfies(transaction),
+ Filter::Intersect(constraint) => include && constraint.satisfies(transaction),
+ Filter::Subtract(constraint) => include && !constraint.satisfies(transaction),
+ })
}
}
}
fn parse(i: &str) -> nom::IResult<&str, Self> {
alt((
+ map(preceded(tag("category:"), string), |c| {
+ Constraint::Category(c.to_string())
+ }),
+ map(preceded(tag("before:"), DateIsh::parse), Constraint::Before),
+ map(preceded(tag("after:"), DateIsh::parse), Constraint::After),
+ map(preceded(tag("on:"), DateIsh::parse), Constraint::On),
map(
- preceded(
- tag("category:"),
- string,
- ),
- |c| Constraint::Category(c.to_string())
- ),
- map(
- preceded(
- tag("before:"),
- DateIsh::parse
- ),
- Constraint::Before
+ preceded(tag("amount:"), parse_comparison),
+ |(amount, comparison)| Constraint::AmountCompare(amount, comparison),
),
map(
- preceded(
- tag("after:"),
- DateIsh::parse
- ),
- Constraint::After
+ delimited(char('('), separated_list0(space1, Filter::parse), char(')')),
+ Constraint::Filters,
),
- map(
- preceded(
- tag("on:"),
- DateIsh::parse
- ),
- Constraint::On
- ),
- map(
- preceded(
- tag("amount:"),
- parse_comparison,
- ),
- |(amount, comparison)| Constraint::AmountCompare(amount, comparison)
- ),
- map(
- delimited(
- char('('),
- separated_list0(
- space1,
- Filter::parse
- ),
- char(')')
- ),
- Constraint::Filters
- )
))(i)
}
}
@@ -212,20 +165,8 @@ pub enum Filter {
impl Filter {
pub fn parse(i: &str) -> nom::IResult<&str, Self> {
alt((
- map(
- preceded(
- char('+'),
- Constraint::parse,
- ),
- Filter::Union
- ),
- map(
- preceded(
- char('-'),
- Constraint::parse,
- ),
- Filter::Subtract
- ),
+ map(preceded(char('+'), Constraint::parse), Filter::Union),
+ map(preceded(char('-'), Constraint::parse), Filter::Subtract),
map(Constraint::parse, Filter::Intersect),
))(i)
}
@@ -248,8 +189,7 @@ impl<'t> Search<'t> {
}
pub fn get(&self) -> Vec<&'t Transaction> {
- self
- .filtered
+ self.filtered
.iter()
.map(|idx| &self.transactions[*idx])
.collect()
@@ -293,13 +233,11 @@ impl<'t> Search<'t> {
}
pub fn parse_filters(i: &str) -> Filter {
- map(
- separated_list0(
- space1,
- Filter::parse
- ),
- |filters| Filter::Intersect(Constraint::Filters(filters))
- )(i).unwrap().1
+ map(separated_list0(space1, Filter::parse), |filters| {
+ Filter::Intersect(Constraint::Filters(filters))
+ })(i)
+ .unwrap()
+ .1
}
#[cfg(test)]
@@ -348,7 +286,11 @@ mod test {
search = search.apply(category_filter);
assert_eq!(search.get().len(), 2);
assert_eq!(
- search.get().iter().map(|t| transaction_id(t)).collect::<Vec<_>>(),
+ search
+ .get()
+ .iter()
+ .map(|t| transaction_id(t))
+ .collect::<Vec<_>>(),
vec![0, 1],
);
}
@@ -383,7 +325,11 @@ mod test {
search_before = search_before.apply(before_filter);
assert_eq!(search_before.get().len(), 1);
assert_eq!(
- search_before.get().iter().map(|t| transaction_id(t)).collect::<Vec<_>>(),
+ search_before
+ .get()
+ .iter()
+ .map(|t| transaction_id(t))
+ .collect::<Vec<_>>(),
vec![0],
);
@@ -392,7 +338,11 @@ mod test {
search_after = search_after.apply(after_filter);
assert_eq!(search_after.get().len(), 3);
assert_eq!(
- search_after.get().iter().map(|t| transaction_id(t)).collect::<Vec<_>>(),
+ search_after
+ .get()
+ .iter()
+ .map(|t| transaction_id(t))
+ .collect::<Vec<_>>(),
vec![1, 2, 3],
);
@@ -401,7 +351,11 @@ mod test {
search_on = search_on.apply(on_filter);
assert_eq!(search_on.get().len(), 1);
assert_eq!(
- search_on.get().iter().map(|t| transaction_id(t)).collect::<Vec<_>>(),
+ search_on
+ .get()
+ .iter()
+ .map(|t| transaction_id(t))
+ .collect::<Vec<_>>(),
vec![1],
);
}
@@ -430,20 +384,32 @@ mod test {
assert_eq!(search.get().len(), 4);
let mut search_less = search.clone();
- let less_filter = Filter::Intersect(Constraint::AmountCompare(160.into(), Comparison::Less));
+ let less_filter =
+ Filter::Intersect(Constraint::AmountCompare(160.into(), Comparison::Less));
search_less = search_less.apply(less_filter);
assert_eq!(search_less.get().len(), 1);
assert_eq!(
- search_less.get().iter().map(|t| transaction_id(t)).collect::<Vec<_>>(),
+ search_less
+ .get()
+ .iter()
+ .map(|t| transaction_id(t))
+ .collect::<Vec<_>>(),
vec![0],
);
let mut search_less_eq = search.clone();
- let less_eq_filter = Filter::Intersect(Constraint::AmountCompare(160.into(), Comparison::LessOrEqual));
+ let less_eq_filter = Filter::Intersect(Constraint::AmountCompare(
+ 160.into(),
+ Comparison::LessOrEqual,
+ ));
search_less_eq = search_less_eq.apply(less_eq_filter);
assert_eq!(search_less_eq.get().len(), 2);
assert_eq!(
- search_less_eq.get().iter().map(|t| transaction_id(t)).collect::<Vec<_>>(),
+ search_less_eq
+ .get()
+ .iter()
+ .map(|t| transaction_id(t))
+ .collect::<Vec<_>>(),
vec![0, 1],
);
@@ -452,25 +418,41 @@ mod test {
search_eq = search_eq.apply(eq_filter);
assert_eq!(search_eq.get().len(), 1);
assert_eq!(
- search_eq.get().iter().map(|t| transaction_id(t)).collect::<Vec<_>>(),
+ search_eq
+ .get()
+ .iter()
+ .map(|t| transaction_id(t))
+ .collect::<Vec<_>>(),
vec![1],
);
let mut search_greater = search.clone();
- let greater_filter = Filter::Intersect(Constraint::AmountCompare(160.into(), Comparison::Greater));
+ let greater_filter =
+ Filter::Intersect(Constraint::AmountCompare(160.into(), Comparison::Greater));
search_greater = search_greater.apply(greater_filter);
assert_eq!(search_greater.get().len(), 2);
assert_eq!(
- search_greater.get().iter().map(|t| transaction_id(t)).collect::<Vec<_>>(),
+ search_greater
+ .get()
+ .iter()
+ .map(|t| transaction_id(t))
+ .collect::<Vec<_>>(),
vec![2, 3],
);
let mut search_greater_eq = search.clone();
- let greater_eq_filter = Filter::Intersect(Constraint::AmountCompare(160.into(), Comparison::GreaterOrEqual));
+ let greater_eq_filter = Filter::Intersect(Constraint::AmountCompare(
+ 160.into(),
+ Comparison::GreaterOrEqual,
+ ));
search_greater_eq = search_greater_eq.apply(greater_eq_filter);
assert_eq!(search_greater_eq.get().len(), 3);
assert_eq!(
- search_greater_eq.get().iter().map(|t| transaction_id(t)).collect::<Vec<_>>(),
+ search_greater_eq
+ .get()
+ .iter()
+ .map(|t| transaction_id(t))
+ .collect::<Vec<_>>(),
vec![1, 2, 3],
);
}
@@ -509,7 +491,11 @@ mod test {
search_filters_1 = search_filters_1.apply(Filter::Union(amount_150.clone()));
assert_eq!(search_filters_1.get().len(), 3);
assert_eq!(
- search_filters_1.get().iter().map(|t| transaction_id(t)).collect::<Vec<_>>(),
+ search_filters_1
+ .get()
+ .iter()
+ .map(|t| transaction_id(t))
+ .collect::<Vec<_>>(),
vec![0, 1, 2],
);
@@ -520,7 +506,11 @@ mod test {
search_filters_2 = search_filters_2.apply(Filter::Union(inner_filters));
assert_eq!(search_filters_2.get().len(), 3);
assert_eq!(
- search_filters_2.get().iter().map(|t| transaction_id(t)).collect::<Vec<_>>(),
+ search_filters_2
+ .get()
+ .iter()
+ .map(|t| transaction_id(t))
+ .collect::<Vec<_>>(),
vec![0, 1, 2],
);
}
diff --git a/cli/src/transaction.rs b/cli/src/transaction.rs
index 8e33aa4..9053aa3 100644
--- a/cli/src/transaction.rs
+++ b/cli/src/transaction.rs
@@ -65,7 +65,10 @@ impl Transaction {
//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())
+ fs::read_to_string(p)
+ .ok()
+ .as_ref()
+ .and_then(|s| serde_json::from_str(s).ok())
}
pub fn id(&self) -> u64 {