From 7bd385d580992f4e575362918a59ef7740ac2505 Mon Sep 17 00:00:00 2001 From: jake Date: Sun, 29 Jan 2023 22:15:49 -0700 Subject: [PATCH] anyhowing continues --- src/ship/items/bank.rs | 20 +++++++++--------- src/ship/items/floor.rs | 6 +++--- src/ship/items/inventory.rs | 42 ++++++++++++++++++------------------- src/ship/items/state.rs | 2 ++ 4 files changed, 36 insertions(+), 34 deletions(-) diff --git a/src/ship/items/bank.rs b/src/ship/items/bank.rs index 5abef95..5494102 100644 --- a/src/ship/items/bank.rs +++ b/src/ship/items/bank.rs @@ -64,10 +64,10 @@ pub struct BankItem { } impl BankItem { - pub async fn with_entity_id(&self, mut param: T, mut func: F) -> Result + pub async fn with_entity_id(&self, mut param: T, mut func: F) -> Result where F: FnMut(T, ItemEntityId) -> Fut, - Fut: Future>, + Fut: Future>, { match &self.item { BankItemDetail::Individual(individual_item) => { @@ -125,27 +125,27 @@ impl BankState { self.item_id_counter = base_item_id + self.bank.0.len() as u32; } - pub fn add_meseta(&mut self, amount: u32) -> Result<(), ItemStateError> { + pub fn add_meseta(&mut self, amount: u32) -> Result<(), anyhow::Error> { if self.meseta.0 + amount > 999999 { - return Err(ItemStateError::FullOfMeseta) + return Err(ItemStateError::FullOfMeseta.into()) } self.meseta.0 += amount; Ok(()) } - pub fn remove_meseta(&mut self, amount: u32) -> Result<(), ItemStateError> { + pub fn remove_meseta(&mut self, amount: u32) -> Result<(), anyhow::Error> { if amount > self.meseta.0 { - return Err(ItemStateError::InvalidMesetaRemoval(amount)) + return Err(ItemStateError::InvalidMesetaRemoval(amount).into()) } self.meseta.0 -= amount; Ok(()) } - pub fn add_inventory_item(&mut self, item: InventoryItem) -> Result { + pub fn add_inventory_item(&mut self, item: InventoryItem) -> Result { match item.item { InventoryItemDetail::Individual(iitem) => { if self.bank.0.len() >= 30 { - Err(BankError::BankFull) + Err(BankError::BankFull.into()) } else { self.bank.0.push(BankItem { @@ -166,7 +166,7 @@ impl BankState { match existing_stack { Some(existing_stack) => { if existing_stack.entity_ids.len() + sitem.entity_ids.len() > sitem.tool.max_stack() { - Err(BankError::StackFull) + Err(BankError::StackFull.into()) } else { existing_stack.entity_ids.append(&mut sitem.entity_ids.clone()); @@ -175,7 +175,7 @@ impl BankState { }, None => { if self.bank.0.len() >= 30 { - Err(BankError::BankFull) + Err(BankError::BankFull.into()) } else { self.bank.0.push(BankItem { diff --git a/src/ship/items/floor.rs b/src/ship/items/floor.rs index 2aab742..c71b7fd 100644 --- a/src/ship/items/floor.rs +++ b/src/ship/items/floor.rs @@ -33,7 +33,7 @@ pub struct FloorItem { } impl FloorItem { - pub async fn with_entity_id(&self, mut param: T, mut func: F) -> Result + pub async fn with_entity_id(&self, mut param: T, mut func: F) -> Result where F: FnMut(T, ItemEntityId) -> Fut, Fut: Future>, @@ -53,10 +53,10 @@ impl FloorItem { Ok(param) } - pub async fn with_mag(&self, mut param: T, mut func: F) -> Result + pub async fn with_mag(&self, mut param: T, mut func: F) -> Result where F: FnMut(T, ItemEntityId, Mag) -> Fut, - Fut: Future>, + Fut: Future>, { if let FloorItemDetail::Individual(individual_item) = &self.item { if let ItemDetail::Mag(mag) = &individual_item.item { diff --git a/src/ship/items/inventory.rs b/src/ship/items/inventory.rs index e10a808..bfd709b 100644 --- a/src/ship/items/inventory.rs +++ b/src/ship/items/inventory.rs @@ -60,7 +60,7 @@ impl InventoryItemDetail { } // TODO: this should probably go somewhere a bit more fundamental like ItemDetail - pub fn sell_price(&self) -> Result { + pub fn sell_price(&self) -> Result { match self { InventoryItemDetail::Individual(individual_item) => { match &individual_item.item { @@ -102,7 +102,7 @@ impl InventoryItemDetail { Ok((ToolShopItem::from(d).price() / 8) as u32) }, ItemDetail::Mag(_m) => { - Err(ItemStateError::ItemNotSellable) + Err(ItemStateError::ItemNotSellable.into()) }, ItemDetail::ESWeapon(_e) => { Ok(10u32) @@ -126,10 +126,10 @@ pub struct InventoryItem { } impl InventoryItem { - pub async fn with_entity_id(&self, mut param: T, mut func: F) -> Result + pub async fn with_entity_id(&self, mut param: T, mut func: F) -> Result where F: FnMut(T, ItemEntityId) -> Fut, - Fut: Future>, + Fut: Future>, { match &self.item { InventoryItemDetail::Individual(individual_item) => { @@ -145,10 +145,10 @@ impl InventoryItem { Ok(param) } - pub async fn with_mag(&self, mut param: T, mut func: F) -> Result + pub async fn with_mag(&self, mut param: T, mut func: F) -> Result where F: FnMut(T, ItemEntityId, Mag) -> Fut, - Fut: Future>, + Fut: Future>, { if let InventoryItemDetail::Individual(individual_item) = &self.item { if let ItemDetail::Mag(mag) = &individual_item.item { @@ -205,11 +205,11 @@ impl InventoryState { self.inventory.0.len() } - pub fn add_floor_item(&mut self, item: FloorItem) -> Result { + pub fn add_floor_item(&mut self, item: FloorItem) -> Result { match item.item { FloorItemDetail::Individual(iitem) => { if self.inventory.0.len() >= 30 { - Err(InventoryError::InventoryFull) + Err(InventoryError::InventoryFull.into()) } else { self.inventory.0.push(InventoryItem { @@ -229,7 +229,7 @@ impl InventoryState { match existing_stack { Some(existing_stack) => { if existing_stack.entity_ids.len() + sitem.entity_ids.len() > sitem.tool.max_stack() { - Err(InventoryError::StackFull) + Err(InventoryError::StackFull.into()) } else { existing_stack.entity_ids.append(&mut sitem.entity_ids.clone()); @@ -238,7 +238,7 @@ impl InventoryState { }, None => { if self.inventory.0.len() >= 30 { - Err(InventoryError::InventoryFull) + Err(InventoryError::InventoryFull.into()) } else { self.inventory.0.push(InventoryItem { @@ -253,7 +253,7 @@ impl InventoryState { }, FloorItemDetail::Meseta(meseta) => { if self.meseta == Meseta(999999) { - Err(InventoryError::MesetaFull) + Err(InventoryError::MesetaFull.into()) } else { self.meseta.0 = std::cmp::min(self.meseta.0 + meseta.0, 999999); @@ -263,11 +263,11 @@ impl InventoryState { } } - pub fn add_item(&mut self, item: InventoryItem) -> Result<(AddItemResult, InventoryItem), InventoryError> { + pub fn add_item(&mut self, item: InventoryItem) -> Result<(AddItemResult, InventoryItem), anyhow::Error> { match &item.item { InventoryItemDetail::Individual(_) => { if self.inventory.0.len() >= 30 { - Err(InventoryError::InventoryFull) + Err(InventoryError::InventoryFull.into()) } else { self.inventory.0.push(item); @@ -290,7 +290,7 @@ impl InventoryState { match existing_stack { Some(existing_stack) => { if existing_stack.entity_ids.len() + sitem.entity_ids.len() > sitem.tool.max_stack() { - Err(InventoryError::StackFull) + Err(InventoryError::StackFull.into()) } else { existing_stack.entity_ids.append(&mut sitem.entity_ids.clone()); @@ -307,7 +307,7 @@ impl InventoryState { }, None => { if self.inventory.0.len() >= 30 { - Err(InventoryError::InventoryFull) + Err(InventoryError::InventoryFull.into()) } else { self.inventory.0.push(item); @@ -370,25 +370,25 @@ impl InventoryState { .find(|i| i.item_id == *item_id) } - pub fn add_meseta(&mut self, amount: u32) -> Result<(), ItemStateError> { + pub fn add_meseta(&mut self, amount: u32) -> Result<(), anyhow::Error> { if self.meseta.0 == 999999 { - return Err(ItemStateError::FullOfMeseta) + return Err(ItemStateError::FullOfMeseta.into()) } self.meseta.0 = std::cmp::min(self.meseta.0 + amount, 999999); Ok(()) } - pub fn add_meseta_no_overflow(&mut self, amount: u32) -> Result<(), ItemStateError> { + pub fn add_meseta_no_overflow(&mut self, amount: u32) -> Result<(), anyhow::Error> { if self.meseta.0 + amount > 999999 { - return Err(ItemStateError::FullOfMeseta) + return Err(ItemStateError::FullOfMeseta.into()) } self.meseta.0 += amount; Ok(()) } - pub fn remove_meseta(&mut self, amount: u32) -> Result<(), ItemStateError> { + pub fn remove_meseta(&mut self, amount: u32) -> Result<(), anyhow::Error> { if amount > self.meseta.0 { - return Err(ItemStateError::InvalidMesetaRemoval(amount)) + return Err(ItemStateError::InvalidMesetaRemoval(amount).into()) } self.meseta.0 -= amount; Ok(()) diff --git a/src/ship/items/state.rs b/src/ship/items/state.rs index 44df7f9..1267b1b 100644 --- a/src/ship/items/state.rs +++ b/src/ship/items/state.rs @@ -22,6 +22,8 @@ pub enum ItemStateError { NoCharacter(CharacterEntityId), #[error("room {0} not found")] NoRoom(RoomId), + #[error("inventory item {0} not found")] + NoInventoryItem(ClientItemId), #[error("floor item {0} not found")] NoFloorItem(ClientItemId), #[error("expected {0} to be a tool")]