#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct StateFlags(pub u32);
impl StateFlags {
const VALID: u32 = 1; const FAVORITE: u32 = 2; const JUNK: u32 = 4; const LABEL1: u32 = 16; const LABEL2: u32 = 32; const LABEL3: u32 = 64; const LABEL4: u32 = 128; const IN_BACKPACK: u32 = 512;
const ALL_LABELS: u32 =
Self::FAVORITE | Self::JUNK | Self::LABEL1 | Self::LABEL2 | Self::LABEL3 | Self::LABEL4;
pub fn backpack() -> Self {
Self(Self::VALID | Self::IN_BACKPACK)
}
pub fn equipped() -> Self {
Self(Self::VALID)
}
pub fn bank() -> Self {
Self(Self::VALID)
}
pub fn from_raw(bits: u32) -> Self {
Self(bits)
}
pub fn to_raw(self) -> u32 {
self.0
}
pub fn with_favorite(mut self) -> Self {
self.0 = (self.0 & !Self::ALL_LABELS) | Self::FAVORITE;
self
}
pub fn with_junk(mut self) -> Self {
self.0 = (self.0 & !Self::ALL_LABELS) | Self::JUNK;
self
}
pub fn with_label1(mut self) -> Self {
self.0 = (self.0 & !Self::ALL_LABELS) | Self::LABEL1;
self
}
pub fn with_label2(mut self) -> Self {
self.0 = (self.0 & !Self::ALL_LABELS) | Self::LABEL2;
self
}
pub fn with_label3(mut self) -> Self {
self.0 = (self.0 & !Self::ALL_LABELS) | Self::LABEL3;
self
}
pub fn with_label4(mut self) -> Self {
self.0 = (self.0 & !Self::ALL_LABELS) | Self::LABEL4;
self
}
pub fn with_no_label(mut self) -> Self {
self.0 &= !Self::ALL_LABELS;
self
}
pub fn is_favorite(&self) -> bool {
self.0 & Self::FAVORITE != 0
}
pub fn is_junk(&self) -> bool {
self.0 & Self::JUNK != 0
}
pub fn has_label1(&self) -> bool {
self.0 & Self::LABEL1 != 0
}
pub fn has_label2(&self) -> bool {
self.0 & Self::LABEL2 != 0
}
pub fn has_label3(&self) -> bool {
self.0 & Self::LABEL3 != 0
}
pub fn has_label4(&self) -> bool {
self.0 & Self::LABEL4 != 0
}
pub fn is_in_backpack(&self) -> bool {
self.0 & Self::IN_BACKPACK != 0
}
pub fn is_equipped(&self) -> bool {
!self.is_in_backpack()
}
pub fn set_favorite(&mut self, value: bool) {
if value {
self.0 = (self.0 & !Self::ALL_LABELS) | Self::FAVORITE;
} else {
self.0 &= !Self::FAVORITE;
}
}
pub fn set_junk(&mut self, value: bool) {
if value {
self.0 = (self.0 & !Self::ALL_LABELS) | Self::JUNK;
} else {
self.0 &= !Self::JUNK;
}
}
pub fn set_label1(&mut self, value: bool) {
if value {
self.0 = (self.0 & !Self::ALL_LABELS) | Self::LABEL1;
} else {
self.0 &= !Self::LABEL1;
}
}
pub fn set_label2(&mut self, value: bool) {
if value {
self.0 = (self.0 & !Self::ALL_LABELS) | Self::LABEL2;
} else {
self.0 &= !Self::LABEL2;
}
}
pub fn set_label3(&mut self, value: bool) {
if value {
self.0 = (self.0 & !Self::ALL_LABELS) | Self::LABEL3;
} else {
self.0 &= !Self::LABEL3;
}
}
pub fn set_label4(&mut self, value: bool) {
if value {
self.0 = (self.0 & !Self::ALL_LABELS) | Self::LABEL4;
} else {
self.0 &= !Self::LABEL4;
}
}
pub fn clear_labels(&mut self) {
self.0 &= !Self::ALL_LABELS;
}
pub fn to_equipped(mut self) -> Self {
self.0 &= !Self::IN_BACKPACK;
self
}
pub fn to_backpack(mut self) -> Self {
self.0 |= Self::IN_BACKPACK;
self
}
}
impl From<u32> for StateFlags {
fn from(v: u32) -> Self {
Self(v)
}
}
impl From<StateFlags> for u32 {
fn from(f: StateFlags) -> Self {
f.0
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_state_flags_backpack() {
let flags = StateFlags::backpack();
assert_eq!(flags.0, 513); assert!(flags.is_in_backpack());
assert!(!flags.is_equipped());
}
#[test]
fn test_state_flags_equipped() {
let flags = StateFlags::equipped();
assert_eq!(flags.0, 1); assert!(!flags.is_in_backpack());
assert!(flags.is_equipped());
}
#[test]
fn test_state_flags_bank() {
let flags = StateFlags::bank();
assert_eq!(flags.0, 1); }
#[test]
fn test_state_flags_with_favorite() {
let flags = StateFlags::backpack().with_favorite();
assert_eq!(flags.0, 515); assert!(flags.is_favorite());
assert!(flags.is_in_backpack());
}
#[test]
fn test_state_flags_with_junk() {
let flags = StateFlags::backpack().with_junk();
assert_eq!(flags.0, 517); assert!(flags.is_junk());
}
#[test]
fn test_state_flags_labels() {
let flags = StateFlags::backpack()
.with_label2()
.with_label3()
.with_label4(); assert!(!flags.has_label2());
assert!(!flags.has_label3());
assert!(flags.has_label4());
assert_eq!(flags.0, 513 + 128);
let fav = StateFlags::backpack().with_favorite();
assert_eq!(fav.0, 515);
let junk = fav.with_junk(); assert!(!junk.is_favorite());
assert!(junk.is_junk());
assert_eq!(junk.0, 517); }
#[test]
fn test_state_flags_mutation() {
let mut flags = StateFlags::backpack();
assert!(!flags.is_favorite());
flags.set_favorite(true);
assert!(flags.is_favorite());
assert_eq!(flags.0, 515);
flags.set_favorite(false);
assert!(!flags.is_favorite());
assert_eq!(flags.0, 513);
}
#[test]
fn test_state_flags_to_equipped() {
let backpack = StateFlags::backpack().with_favorite();
let equipped = backpack.to_equipped();
assert!(!equipped.is_in_backpack());
assert!(equipped.is_favorite()); assert_eq!(equipped.0, 3); }
#[test]
fn test_state_flags_to_backpack() {
let equipped = StateFlags::equipped().with_junk();
let backpack = equipped.to_backpack();
assert!(backpack.is_in_backpack());
assert!(backpack.is_junk()); assert_eq!(backpack.0, 517); }
#[test]
fn test_state_flags_from_raw() {
let flags = StateFlags::from_raw(515); assert!(flags.is_in_backpack());
assert!(flags.is_favorite());
}
#[test]
fn test_state_flags_conversions() {
let flags = StateFlags::backpack();
let raw: u32 = flags.into();
assert_eq!(raw, 513);
let restored: StateFlags = 515.into(); assert!(restored.is_favorite());
}
#[test]
fn test_state_flags_set_junk_mutation() {
let mut flags = StateFlags::backpack();
flags.set_junk(true);
assert!(flags.is_junk());
assert!(!flags.is_favorite());
flags.set_junk(false);
assert!(!flags.is_junk());
}
#[test]
fn test_state_flags_set_label1() {
let mut flags = StateFlags::backpack();
flags.set_label1(true);
assert!(flags.has_label1());
assert!(!flags.is_favorite());
flags.set_label1(false);
assert!(!flags.has_label1());
}
#[test]
fn test_state_flags_set_label2() {
let mut flags = StateFlags::backpack();
flags.set_label2(true);
assert!(flags.has_label2());
flags.set_label2(false);
assert!(!flags.has_label2());
}
#[test]
fn test_state_flags_set_label3() {
let mut flags = StateFlags::backpack();
flags.set_label3(true);
assert!(flags.has_label3());
flags.set_label3(false);
assert!(!flags.has_label3());
}
#[test]
fn test_state_flags_set_label4() {
let mut flags = StateFlags::backpack();
flags.set_label4(true);
assert!(flags.has_label4());
flags.set_label4(false);
assert!(!flags.has_label4());
}
#[test]
fn test_state_flags_clear_labels() {
let mut flags = StateFlags::backpack().with_favorite();
assert!(flags.is_favorite());
flags.clear_labels();
assert!(!flags.is_favorite());
assert!(!flags.is_junk());
assert!(!flags.has_label1());
assert!(flags.is_in_backpack()); }
#[test]
fn test_state_flags_with_label1() {
let flags = StateFlags::backpack().with_label1();
assert!(flags.has_label1());
assert!(!flags.is_favorite());
assert!(!flags.is_junk());
}
#[test]
fn test_state_flags_with_no_label() {
let flags = StateFlags::backpack().with_favorite().with_no_label();
assert!(!flags.is_favorite());
assert!(!flags.is_junk());
assert!(!flags.has_label1());
assert!(flags.is_in_backpack());
}
#[test]
fn test_state_flags_to_raw() {
let flags = StateFlags::backpack();
assert_eq!(flags.to_raw(), 513);
}
}