anyhowing continues
This commit is contained in:
		
							parent
							
								
									f09c73611f
								
							
						
					
					
						commit
						7bd385d580
					
				| @ -64,10 +64,10 @@ pub struct BankItem { | ||||
| } | ||||
| 
 | ||||
| impl BankItem { | ||||
|     pub async fn with_entity_id<F, Fut, T>(&self, mut param: T, mut func: F) -> Result<T, ItemStateError> | ||||
|     pub async fn with_entity_id<F, Fut, T>(&self, mut param: T, mut func: F) -> Result<T, anyhow::Error> | ||||
|     where | ||||
|         F: FnMut(T, ItemEntityId) -> Fut, | ||||
|         Fut: Future<Output=Result<T, ItemStateError>>, | ||||
|         Fut: Future<Output=Result<T, anyhow::Error>>, | ||||
|     { | ||||
|         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<AddItemResult, BankError> { | ||||
|     pub fn add_inventory_item(&mut self, item: InventoryItem) -> Result<AddItemResult, anyhow::Error> { | ||||
|         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 { | ||||
|  | ||||
| @ -33,7 +33,7 @@ pub struct FloorItem { | ||||
| } | ||||
| 
 | ||||
| impl FloorItem { | ||||
|     pub async fn with_entity_id<F, Fut, T>(&self, mut param: T, mut func: F) -> Result<T, ItemStateError> | ||||
|     pub async fn with_entity_id<F, Fut, T>(&self, mut param: T, mut func: F) -> Result<T, anyhow::Error> | ||||
|     where | ||||
|         F: FnMut(T, ItemEntityId) -> Fut, | ||||
|         Fut: Future<Output=Result<T, ItemStateError>>, | ||||
| @ -53,10 +53,10 @@ impl FloorItem { | ||||
|         Ok(param) | ||||
|     } | ||||
| 
 | ||||
|     pub async fn with_mag<F, Fut, T>(&self, mut param: T, mut func: F) -> Result<T, ItemStateError> | ||||
|     pub async fn with_mag<F, Fut, T>(&self, mut param: T, mut func: F) -> Result<T, anyhow::Error> | ||||
|     where | ||||
|         F: FnMut(T, ItemEntityId, Mag) -> Fut, | ||||
|         Fut: Future<Output=Result<T, ItemStateError>>, | ||||
|         Fut: Future<Output=Result<T, anyhow::Error>>, | ||||
|     { | ||||
|         if let FloorItemDetail::Individual(individual_item) = &self.item { | ||||
|             if let ItemDetail::Mag(mag) = &individual_item.item { | ||||
|  | ||||
| @ -60,7 +60,7 @@ impl InventoryItemDetail { | ||||
|     } | ||||
| 
 | ||||
|     // TODO: this should probably go somewhere a bit more fundamental like ItemDetail
 | ||||
|     pub fn sell_price(&self) -> Result<u32, ItemStateError> { | ||||
|     pub fn sell_price(&self) -> Result<u32, anyhow::Error> { | ||||
|         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<F, Fut, T>(&self, mut param: T, mut func: F) -> Result<T, ItemStateError> | ||||
|     pub async fn with_entity_id<F, Fut, T>(&self, mut param: T, mut func: F) -> Result<T, anyhow::Error> | ||||
|     where | ||||
|         F: FnMut(T, ItemEntityId) -> Fut, | ||||
|         Fut: Future<Output=Result<T, ItemStateError>>, | ||||
|         Fut: Future<Output=Result<T, anyhow::Error>>, | ||||
|     { | ||||
|         match &self.item { | ||||
|             InventoryItemDetail::Individual(individual_item) => { | ||||
| @ -145,10 +145,10 @@ impl InventoryItem { | ||||
|         Ok(param) | ||||
|     } | ||||
| 
 | ||||
|     pub async fn with_mag<F, Fut, T>(&self, mut param: T, mut func: F) -> Result<T, ItemStateError> | ||||
|     pub async fn with_mag<F, Fut, T>(&self, mut param: T, mut func: F) -> Result<T, anyhow::Error> | ||||
|     where | ||||
|         F: FnMut(T, ItemEntityId, Mag) -> Fut, | ||||
|         Fut: Future<Output=Result<T, ItemStateError>>, | ||||
|         Fut: Future<Output=Result<T, anyhow::Error>>, | ||||
|     { | ||||
|         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<AddItemResult, InventoryError> { | ||||
|     pub fn add_floor_item(&mut self, item: FloorItem) -> Result<AddItemResult, anyhow::Error> { | ||||
|         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(()) | ||||
|  | ||||
| @ -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")] | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user