pub(crate) mod syntax { use serde::Deserialize; #[derive(Debug, Deserialize)] pub struct OutfitSpace { pub outfit: Option, pub weapon: Option, pub engine: Option, } } /// Represents outfit space, either that available in a ship /// or that used by an outfit. #[derive(Debug, Clone, Copy)] pub struct OutfitSpace { /// Total available outfit space. /// This should be greater than weapon and engine. pub outfit: u32, /// Space for weapons pub weapon: u32, /// Space for engine pub engine: u32, } impl OutfitSpace { /// Make a new, zero OutfitSpace pub fn new() -> Self { Self { outfit: 0, weapon: 0, engine: 0, } } /// Does this outfit contain `smaller`? pub fn can_contain(&self, smaller: Self) -> bool { self.outfit >= (smaller.outfit + smaller.weapon + smaller.engine) && self.weapon >= smaller.weapon && self.engine >= smaller.engine } /// Free outfit space pub fn free(&mut self, rhs: &Self) { self.outfit += rhs.outfit + rhs.weapon + rhs.engine; self.weapon += rhs.weapon; self.engine += rhs.engine; } /// Occupy outfit space pub fn occupy(&mut self, rhs: &Self) { self.outfit -= rhs.outfit + rhs.weapon + rhs.engine; self.weapon -= rhs.weapon; self.engine -= rhs.engine; } } impl From for OutfitSpace { fn from(value: syntax::OutfitSpace) -> Self { Self { outfit: value.outfit.unwrap_or(0), engine: value.engine.unwrap_or(0), weapon: value.weapon.unwrap_or(0), } } }