diff options
| author | 魏曹先生 <1992414357@qq.com> | 2026-04-17 09:45:13 +0800 |
|---|---|---|
| committer | 魏曹先生 <1992414357@qq.com> | 2026-04-17 09:45:13 +0800 |
| commit | 497a667f49c8dea870a8a61ddabd95e24765a80d (patch) | |
| tree | 84bed9d9b604af593b1031ae62ca7b3fec4d7112 /src/calc.rs | |
| parent | 5e7d1968e94bf39d369de0e6ef98cf383cfd3d6f (diff) | |
Apply clippy suggestions
- Use `BTreeMap::entry` API in `update_item`
- Replace `or_insert_with(Vec::new)` with `or_default`
- Iterate over map values instead of key-value pairs
- Use `std::io::Error::other` for custom errors
- Simplify filter logic with direct predicate
- Replace manual `Into` implementation with `From`
- Remove unnecessary explicit returns
Diffstat (limited to 'src/calc.rs')
| -rw-r--r-- | src/calc.rs | 16 |
1 files changed, 7 insertions, 9 deletions
diff --git a/src/calc.rs b/src/calc.rs index fcd0f5b..d1209c3 100644 --- a/src/calc.rs +++ b/src/calc.rs @@ -27,7 +27,7 @@ pub fn calculate_from(item: Bills) -> Result<SplitResult, BillSplitError> { } fn precheck(item: &Bills) -> Result<(), BillSplitError> { - for (_, bill_item) in &item.items { + for bill_item in item.items.values() { // Check if the paid amount is negative if bill_item.paid < 0.0 { return Err(BillSplitError::NegativePaidAmount); @@ -44,16 +44,14 @@ fn precheck(item: &Bills) -> Result<(), BillSplitError> { Ok(()) } -fn calculate_balances_and_transactions( - item: &Bills, -) -> ( - BTreeMap<(Who, Who), f64>, - BTreeMap<Who, Vec<SplitResultItem>>, -) { +type DirectTransactions = BTreeMap<(Who, Who), f64>; +type ItemsByWho = BTreeMap<Who, Vec<SplitResultItem>>; + +fn calculate_balances_and_transactions(item: &Bills) -> (DirectTransactions, ItemsByWho) { let mut direct_transactions: BTreeMap<(Who, Who), f64> = BTreeMap::new(); let mut items: BTreeMap<Who, Vec<SplitResultItem>> = BTreeMap::new(); - for (_, bill_item) in &item.items { + for bill_item in item.items.values() { let who_paid = &bill_item.who_paid; let paid = bill_item.paid; let split_count = bill_item.split.len() as f64; @@ -82,7 +80,7 @@ fn calculate_balances_and_transactions( items .entry(person.clone()) - .or_insert_with(Vec::new) + .or_default() .push(bill_result_item); } } |
