aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
author魏曹先生 <1992414357@qq.com>2026-04-17 09:45:13 +0800
committer魏曹先生 <1992414357@qq.com>2026-04-17 09:45:13 +0800
commit497a667f49c8dea870a8a61ddabd95e24765a80d (patch)
tree84bed9d9b604af593b1031ae62ca7b3fec4d7112 /src
parent5e7d1968e94bf39d369de0e6ef98cf383cfd3d6f (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')
-rw-r--r--src/bill.rs11
-rw-r--r--src/calc.rs16
-rw-r--r--src/cli.rs7
-rw-r--r--src/edit.rs14
-rw-r--r--src/who.rs6
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);
}
}
diff --git a/src/cli.rs b/src/cli.rs
index 44c1f8f..9c17ebb 100644
--- a/src/cli.rs
+++ b/src/cli.rs
@@ -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");
diff --git a/src/who.rs b/src/who.rs
index 32f536b..8e16765 100644
--- a/src/who.rs
+++ b/src/who.rs
@@ -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
}
}