Skip to content

Commit

Permalink
Prevent panic in calls to mineral_type() when labs are empty (#305)
Browse files Browse the repository at this point in the history
  • Loading branch information
shanemadden authored Nov 18, 2020
1 parent bebd763 commit 56d7548
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ Unreleased
- Change `pathfinder::search_many` to return an incomplete result when called with no goals to
prevent a panic due to unexpected return data from javascript.
- Change `MemoryReference::get` to return a generic error type
- Change `StructureLab::mineral_type` to return `Option<ResourceType>`, avoiding panic when labs
are empty

### Misc:

Expand Down
18 changes: 16 additions & 2 deletions src/objects/impls/structure_lab.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,25 @@
use stdweb::Value;

use crate::{
constants::{ResourceType, ReturnCode},
objects::{Creep, StructureLab},
traits::TryFrom,
};

impl StructureLab {
pub fn mineral_type(&self) -> ResourceType {
js_unwrap!(__resource_type_str_to_num(@{self.as_ref()}.mineralType))
pub fn mineral_type(&self) -> Option<ResourceType> {
let mineral_v = js! {
const mineral = @{self.as_ref()}.mineralType;
if (mineral) {
return __resource_type_str_to_num(mineral);
}
};
match mineral_v {
Value::Number(_) => {
Some(ResourceType::try_from(mineral_v).expect("lab resource unknown."))
}
_ => None,
}
}

pub fn boost_creep(&self, creep: &Creep, body_part_count: Option<u32>) -> ReturnCode {
Expand Down

0 comments on commit 56d7548

Please sign in to comment.