diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/bill.rs | 11 | ||||
| -rw-r--r-- | src/calc.rs | 16 | ||||
| -rw-r--r-- | src/cli.rs | 7 | ||||
| -rw-r--r-- | src/edit.rs | 14 | ||||
| -rw-r--r-- | src/who.rs | 6 |
5 files changed, 19 insertions, 35 deletions
diff --git a/src/bill.rs b/src/bill.rs index 291b759..62a1d3b 100644 --- a/src/bill.rs +++ b/src/bill.rs @@ -62,8 +62,10 @@ impl Bills { /// Update the bill item with the specified ID pub fn update_item(&mut self, id: &Uuid, item: BillItem) -> bool { - if self.items.contains_key(&id.to_string()) { - self.items.insert(id.to_string(), item); + if let std::collections::btree_map::Entry::Occupied(mut e) = + self.items.entry(id.to_string()) + { + e.insert(item); true } else { false @@ -100,10 +102,7 @@ impl SplitResult { reason, }; - self.items - .entry(payer) - .or_insert_with(Vec::new) - .push(result_item); + self.items.entry(payer).or_default().push(result_item); } /// Get all bill items for a specified payer (immutable reference) 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); } } @@ -122,7 +122,7 @@ fn name_suggest(typed: Vec<String>) -> Suggest { suggest.insert(SuggestItem::Simple(member)); } } - return suggest; + suggest } #[completion(ListAllBillEntry)] @@ -353,10 +353,7 @@ fn read_bills() -> Bills { let state_file = state_file_path(); if state_file.exists() { match std::fs::read_to_string(&state_file) { - Ok(contents) => match serde_yaml::from_str(&contents) { - Ok(bills) => bills, - Err(_) => Bills::default(), - }, + Ok(contents) => serde_yaml::from_str(&contents).unwrap_or_default(), Err(_) => Bills::default(), } } else { diff --git a/src/edit.rs b/src/edit.rs index f9c751c..d505fc8 100644 --- a/src/edit.rs +++ b/src/edit.rs @@ -17,10 +17,7 @@ pub fn input_with_editor_cutsom( let status = Command::new(editor).arg(cache_path).status()?; if !status.success() { - return Err(std::io::Error::new( - std::io::ErrorKind::Other, - "Editor exited with non-zero status", - )); + return Err(std::io::Error::other("Editor exited with non-zero status")); } // Read the modified content @@ -29,14 +26,7 @@ pub fn input_with_editor_cutsom( // Remove comment lines and trim let processed_content: String = content .lines() - .filter_map(|line| { - let trimmed = line.trim(); - if trimmed.starts_with(comment_prefix) { - None - } else { - Some(line) - } - }) + .filter(|line| !line.trim().starts_with(comment_prefix)) .collect::<Vec<&str>>() .join("\n"); @@ -33,9 +33,9 @@ impl From<&str> for Who { } } -impl Into<String> for Who { - fn into(self) -> String { - self.name +impl From<Who> for String { + fn from(val: Who) -> Self { + val.name } } |
