diff options
| author | 魏曹先生 <1992414357@qq.com> | 2026-04-16 23:24:52 +0800 |
|---|---|---|
| committer | 魏曹先生 <1992414357@qq.com> | 2026-04-16 23:24:52 +0800 |
| commit | 6e36fc3707e791c3c748133d648957706b54fd3a (patch) | |
| tree | 3851ed69d60f331a803a6c19c97a56829a11f2f5 /src/bill.rs | |
| parent | 363fbc6e98f832471a17a10ec18e8823df6a2ed5 (diff) | |
Add CLI commands for bill management and persistence
Diffstat (limited to 'src/bill.rs')
| -rw-r--r-- | src/bill.rs | 24 |
1 files changed, 14 insertions, 10 deletions
diff --git a/src/bill.rs b/src/bill.rs index 16923f9..e03cd22 100644 --- a/src/bill.rs +++ b/src/bill.rs @@ -1,14 +1,16 @@ use std::collections::BTreeMap; +use serde::{Deserialize, Serialize}; use uuid::Uuid; use crate::who::Who; -#[derive(Default)] +#[derive(Debug, Default, Serialize, Deserialize)] pub struct Bills { - pub items: BTreeMap<Uuid, BillItem>, + pub items: BTreeMap<String, BillItem>, } +#[derive(Debug, Default, Serialize, Deserialize)] pub struct BillItem { pub who_paid: Who, pub reason: String, @@ -16,11 +18,13 @@ pub struct BillItem { pub split: Vec<Who>, } +#[derive(Debug, Default, Serialize, Deserialize)] pub struct SplitResult { pub items: BTreeMap<Who, Vec<SplitResultItem>>, pub final_result: BTreeMap<(Who, Who), f64>, } +#[derive(Debug, Serialize, Deserialize)] pub struct SplitResultItem { pub payee: Who, pub bill: f64, @@ -42,24 +46,24 @@ impl Bills { /// Add a new bill item pub fn add_item(&mut self, item: BillItem) -> Uuid { let id = Uuid::new_v4(); - self.items.insert(id, item); + self.items.insert(id.to_string(), item); id } /// Get a bill item by ID (immutable reference) pub fn get_item(&self, id: &Uuid) -> Option<&BillItem> { - self.items.get(id) + self.items.get(&id.to_string()) } /// Get a bill item by ID (mutable reference) pub fn get_item_mut(&mut self, id: &Uuid) -> Option<&mut BillItem> { - self.items.get_mut(id) + self.items.get_mut(&id.to_string()) } /// 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) { - self.items.insert(*id, item); + if self.items.contains_key(&id.to_string()) { + self.items.insert(id.to_string(), item); true } else { false @@ -68,17 +72,17 @@ impl Bills { /// Delete the bill item with the specified ID pub fn delete_item(&mut self, id: &Uuid) -> Option<BillItem> { - self.items.remove(id) + self.items.remove(&id.to_string()) } /// Get all bill items - pub fn get_all_items(&self) -> &BTreeMap<Uuid, BillItem> { + pub fn get_all_items(&self) -> &BTreeMap<String, BillItem> { &self.items } /// Check if a bill item with the specified ID exists pub fn contains_item(&self, id: &Uuid) -> bool { - self.items.contains_key(id) + self.items.contains_key(&id.to_string()) } /// Clear all bill items |
