cargo fmt the previous changes.

This commit is contained in:
Joseph Gerardot
2019-10-31 10:26:25 -04:00
parent eb1b42be0f
commit 7325757066
2 changed files with 57 additions and 34 deletions

View File

@ -70,8 +70,7 @@ impl Inventory {
let mut leftovers = Vec::new();
for item in &mut items {
if self.contains(&item).not() {
self.push(item)
.map(|overflow| leftovers.push(overflow));
self.push(item).map(|overflow| leftovers.push(overflow));
} // else drop item if it was already in
}
if leftovers.len() > 0 {
@ -100,12 +99,18 @@ impl Inventory {
/// O(n) count the number of items in this inventory.
pub fn count(&self) -> usize {
self.slots.iter().fold(0, |count, slot| count + if slot.is_some() { 1 } else { 0 })
self.slots
.iter()
.fold(0, |count, slot| count + if slot.is_some() { 1 } else { 0 })
}
/// O(n) check if an item is in this inventory.
pub fn contains(&self, item: &Item) -> bool {
self.slots.iter().any(|slot| slot.as_ref().map(|other_item| item == other_item).unwrap_or(false))
self.slots.iter().any(|slot| {
slot.as_ref()
.map(|other_item| item == other_item)
.unwrap_or(false)
})
}
/// Get content of a slot
@ -152,11 +157,10 @@ impl Component for InventoryUpdate {
#[cfg(test)]
mod test {
use lazy_static::lazy_static;
use super::*;
use lazy_static::lazy_static;
lazy_static! {
static ref TEST_ITEMS: Vec<Item> =
vec![
static ref TEST_ITEMS: Vec<Item> = vec![
assets::load_expect_cloned("common.items.debug.boost"),
assets::load_expect_cloned("common.items.debug.possess")
];
@ -170,7 +174,9 @@ mod test {
/// Attempting to push into a full inventory should return the same item.
#[test]
fn push_full() {
let mut inv = Inventory { slots: TEST_ITEMS.iter().map(|a| Some(a.clone())).collect() };
let mut inv = Inventory {
slots: TEST_ITEMS.iter().map(|a| Some(a.clone())).collect(),
};
assert_eq!(
inv.push(TEST_ITEMS[0].clone()).unwrap(),
TEST_ITEMS[0].clone()
@ -180,27 +186,33 @@ mod test {
/// Attempting to push a series into a full inventory should return them all.
#[test]
fn push_all_full() {
let mut inv = Inventory { slots: TEST_ITEMS.iter().map(|a| Some(a.clone())).collect() };
let Error::Full(leftovers) = inv.push_all(TEST_ITEMS.iter().map(|a| a.clone()))
let mut inv = Inventory {
slots: TEST_ITEMS.iter().map(|a| Some(a.clone())).collect(),
};
let Error::Full(leftovers) = inv
.push_all(TEST_ITEMS.iter().map(|a| a.clone()))
.expect_err("Pushing into a full inventory somehow worked!");
assert_eq!(
leftovers,
TEST_ITEMS.clone()
)
assert_eq!(leftovers, TEST_ITEMS.clone())
}
/// Attempting to push uniquely into an inventory containing all the items should work fine.
#[test]
fn push_unique_all_full() {
let mut inv = Inventory { slots: TEST_ITEMS.iter().map(|a| Some(a.clone())).collect() };
let mut inv = Inventory {
slots: TEST_ITEMS.iter().map(|a| Some(a.clone())).collect(),
};
inv.push_all_unique(TEST_ITEMS.iter().map(|a| a.clone()))
.expect("Pushing unique items into an inventory that already contains them didn't work!");
.expect(
"Pushing unique items into an inventory that already contains them didn't work!",
);
}
/// Attempting to push uniquely into an inventory containing all the items should work fine.
#[test]
fn push_all_empty() {
let mut inv = Inventory { slots: vec![None, None] };
let mut inv = Inventory {
slots: vec![None, None],
};
inv.push_all(TEST_ITEMS.iter().map(|a| a.clone()))
.expect("Pushing items into an empty inventory didn't work!");
}
@ -208,8 +220,12 @@ mod test {
/// Attempting to push uniquely into an inventory containing all the items should work fine.
#[test]
fn push_all_unique_empty() {
let mut inv = Inventory { slots: vec![None, None] };
let mut inv = Inventory {
slots: vec![None, None],
};
inv.push_all_unique(TEST_ITEMS.iter().map(|a| a.clone()))
.expect("Pushing unique items into an empty inventory that didn't contain them didn't work!");
.expect(
"Pushing unique items into an empty inventory that didn't contain them didn't work!",
);
}
}

View File

@ -997,7 +997,12 @@ fn handle_exp(server: &mut Server, entity: EcsEntity, args: String, action: &Cha
}
use common::comp::Item;
fn handle_debug_items(server: &mut Server, entity: EcsEntity, _args: String, _action: &ChatCommand) {
fn handle_debug_items(
server: &mut Server,
entity: EcsEntity,
_args: String,
_action: &ChatCommand,
) {
if let Ok(items) = assets::load_glob::<Item>("common.items.debug.*") {
server
.state()
@ -1014,7 +1019,9 @@ fn handle_debug_items(server: &mut Server, entity: EcsEntity, _args: String, _ac
} else {
server.notify_client(
entity,
ServerMsg::private(String::from("Debug items not found? Something is very broken."))
ServerMsg::private(String::from(
"Debug items not found? Something is very broken.",
)),
);
}
}