use crate::Location;
const RACKS: [(&str, Location, u32); 7] = [
("HiSlot", Location::High, 8),
("MedSlot", Location::Mid, 8),
("LoSlot", Location::Low, 8),
("RigSlot", Location::Rig, 8),
("SubSystemSlot", Location::Sub, 8),
("ServiceSlot", Location::Svc, 8),
("FighterTube", Location::Tube, 5),
];
const HOLDS: [(&str, Location); 22] = [
("Cargo", Location::Cargo),
("DroneBay", Location::Bay),
("FighterBay", Location::Bay),
("SpecializedAmmoHold", Location::Ammo),
("BoosterBay", Location::Booster),
("SpecializedCommandCenterHold", Location::Command),
("CorpseBay", Location::Corpse),
("MobileDepotHold", Location::Depot),
("ExpeditionHold", Location::Expedition),
("FleetHangar", Location::Fleet),
("FrigateEscapeBay", Location::Frigate),
("SpecializedFuelBay", Location::Fuel),
("SpecializedGasHold", Location::Gas),
("SpecializedIceHold", Location::Ice),
("InfrastructureHangar", Location::Infrastructure),
("ShipHangar", Location::Maintenance),
("SpecializedMineralHold", Location::Mineral),
("SpecializedOreHold", Location::Mining),
("MoonMaterialBay", Location::Moon),
("SpecializedPlanetaryCommoditiesHold", Location::Planetary),
("QuafeBay", Location::Quafe),
("SubSystemBay", Location::Subsystem),
];
pub fn location(flag: &str) -> Option<Location> {
rack_slot(flag).map(|(location, _)| location).or_else(|| {
HOLDS
.iter()
.find(|&&(name, _)| name == flag)
.map(|&(_, location)| location)
})
}
pub fn slot(flag: &str) -> Option<u32> {
rack_slot(flag).map(|(_, slot)| slot)
}
fn rack_slot(flag: &str) -> Option<(Location, u32)> {
RACKS.into_iter().find_map(|(prefix, location, size)| {
if let Some(index) = flag.strip_prefix(prefix)
&& let [digit @ b'0'..=b'9'] = index.as_bytes()
&& let index = u32::from(digit - b'0')
&& index < size
{
Some((location, index + 1))
} else {
None
}
})
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn slots() {
let cases = [
("HiSlot0", Location::High, 1),
("MedSlot7", Location::Mid, 8),
("LoSlot2", Location::Low, 3),
("RigSlot1", Location::Rig, 2),
("SubSystemSlot3", Location::Sub, 4),
("ServiceSlot5", Location::Svc, 6),
("FighterTube4", Location::Tube, 5),
];
for (flag, want, n) in cases {
assert_eq!(location(flag), Some(want), "{flag}");
assert_eq!(slot(flag), Some(n), "{flag}");
}
}
#[test]
fn every_hold() {
let flags = [
"Cargo",
"DroneBay",
"SpecializedAmmoHold",
"BoosterBay",
"SpecializedCommandCenterHold",
"CorpseBay",
"MobileDepotHold",
"ExpeditionHold",
"FleetHangar",
"FrigateEscapeBay",
"SpecializedFuelBay",
"SpecializedGasHold",
"SpecializedIceHold",
"InfrastructureHangar",
"ShipHangar",
"SpecializedMineralHold",
"SpecializedOreHold",
"MoonMaterialBay",
"SpecializedPlanetaryCommoditiesHold",
"QuafeBay",
"SubSystemBay",
];
let holds: Vec<Location> = Location::ALL
.into_iter()
.filter(|l| !Location::RACKS.contains(l))
.collect();
let got: Vec<Location> = flags.iter().map(|f| location(f).unwrap()).collect();
assert_eq!(got, holds);
assert!(flags.iter().all(|f| slot(f).is_none()));
assert_eq!(location("FighterBay"), Some(Location::Bay));
}
#[test]
fn unknown() {
for flag in [
"Hangar",
"AutoFit",
"HiSlot8",
"FighterTube5",
"hislot0",
"HiSlot01",
"HiSlot0\n",
"toString",
"",
] {
assert_eq!(location(flag), None, "{flag}");
assert_eq!(slot(flag), None, "{flag}");
}
}
}