use crate::abi::TransactionInfo;
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum HeldState {
Inventory,
Growth,
FactoryStation,
NpcBed,
StoreAssociatedNpcIds,
CustomPlayerString,
Fuel,
LogicGraphNodeConnected,
LogicGraphTrigger,
}
impl HeldState {
#[cfg(any(test, feature = "test"))]
pub(crate) fn bit(self) -> u32 {
Self::ALL
.iter()
.find(|&&(kind, _)| kind == self)
.map_or(0, |&(_, bit)| bit)
}
const ALL: [(Self, u32); 9] = [
(Self::Inventory, TransactionInfo::HOLDS_INVENTORY),
(Self::Growth, TransactionInfo::HOLDS_GROWTH),
(Self::FactoryStation, TransactionInfo::HOLDS_FACTORY_STATION),
(Self::NpcBed, TransactionInfo::HOLDS_NPC_BED),
(
Self::StoreAssociatedNpcIds,
TransactionInfo::HOLDS_STORE_ASSOCIATED_NPC_IDS,
),
(
Self::CustomPlayerString,
TransactionInfo::HOLDS_CUSTOM_PLAYER_STRING,
),
(Self::Fuel, TransactionInfo::HOLDS_FUEL),
(
Self::LogicGraphNodeConnected,
TransactionInfo::HOLDS_LOGIC_GRAPH_NODE_CONNECTED,
),
(
Self::LogicGraphTrigger,
TransactionInfo::HOLDS_LOGIC_GRAPH_TRIGGER,
),
];
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub struct PropState(u32);
impl PropState {
pub(crate) const fn from_bits(bits: u32) -> Self {
Self(bits)
}
#[must_use]
pub fn holds(self, kind: HeldState) -> bool {
HeldState::ALL
.iter()
.any(|&(known, bit)| known == kind && self.0 & bit != 0)
}
pub fn iter(self) -> impl Iterator<Item = HeldState> {
HeldState::ALL
.into_iter()
.filter(move |&(_, bit)| self.0 & bit != 0)
.map(|(kind, _)| kind)
}
}
impl core::fmt::Display for PropState {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
for (i, kind) in self.iter().enumerate() {
if i > 0 {
f.write_str(", ")?;
}
write!(f, "{kind:?}")?;
}
Ok(())
}
}