diff options
author | tom barrett <spalf0@gmail.com> | 2019-02-03 05:45:35 -0600 |
---|---|---|
committer | tom barrett <spalf0@gmail.com> | 2019-02-03 05:45:35 -0600 |
commit | 46fa862e04bc43311ba79ef3db70abf9014b9104 (patch) | |
tree | 3507873ec788af1f1e0885bbb676dbb4a373d48e /src/storage.rs | |
parent | 828f0d83dcb258a8e5efd55a7775592c6e5f77bc (diff) |
bringing to 2018
Diffstat (limited to 'src/storage.rs')
-rw-r--r-- | src/storage.rs | 38 |
1 files changed, 17 insertions, 21 deletions
diff --git a/src/storage.rs b/src/storage.rs index 72a3b27..f43bec4 100644 --- a/src/storage.rs +++ b/src/storage.rs @@ -1,30 +1,27 @@ -use item::Item; +use crate::item::Item; #[derive(Serialize, Deserialize, Debug, Clone)] pub struct Storage { - items : Vec<Item>, - carrying : usize, - capacity : usize, + items: Vec<Item>, + carrying: usize, + capacity: usize, } impl Storage { - pub fn new(items : Vec<Item>) -> Storage { + pub fn new(items: Vec<Item>) -> Storage { let mut carrying = 0; for item in items.iter() { carrying += item.size; } Storage { - items : items, - capacity : 10, - carrying : carrying, + items, + capacity: 10, + carrying, } } pub fn has_minerals(&self) -> bool { - match self.items.iter().position(|item| item.is_mineral()) { - Some(_) => true, - None => false, - } + self.items.iter().any(|item| item.is_mineral()) } pub fn refined_count(&self) -> usize { @@ -33,7 +30,7 @@ impl Storage { items.len() } - pub fn take(&mut self, name : &str) -> Option<Item> { + pub fn take(&mut self, name: &str) -> Option<Item> { match self.items.iter().position(|item| item.name == name) { Some(index) => { let item = self.items.remove(index); @@ -44,14 +41,13 @@ impl Storage { } } - pub fn give(&mut self, item : Item) -> bool { - match self.capacity >= self.carrying + item.size { - true => { - self.carrying += item.size; - self.items.push(item); - true - }, - false => false, + pub fn give(&mut self, item: Item) -> bool { + if self.capacity >= self.carrying + item.size { + self.carrying += item.size; + self.items.push(item); + true + } else { + false } } } |