From 6e36fc3707e791c3c748133d648957706b54fd3a Mon Sep 17 00:00:00 2001 From: 魏曹先生 <1992414357@qq.com> Date: Thu, 16 Apr 2026 23:24:52 +0800 Subject: Add CLI commands for bill management and persistence --- src/bill.rs | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) (limited to 'src/bill.rs') 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, + pub items: BTreeMap, } +#[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, } +#[derive(Debug, Default, Serialize, Deserialize)] pub struct SplitResult { pub items: BTreeMap>, 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 { - self.items.remove(id) + self.items.remove(&id.to_string()) } /// Get all bill items - pub fn get_all_items(&self) -> &BTreeMap { + pub fn get_all_items(&self) -> &BTreeMap { &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 -- cgit