use std::collections::HashMap;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct HashMapBool {
map: HashMap<(), ()>,
}
impl HashMapBool {
pub fn new(value: bool) -> Self {
let mut map = HashMap::new();
if value {
map.insert((), ());
}
HashMapBool { map }
}
pub fn get_value(&self) -> bool {
match self.map.get(&()) {
Some(_) => true,
None => false,
}
}
pub fn is_true(&self) -> bool {
self.map.contains_key(&())
}
pub fn is_false(&self) -> bool {
!self.is_true()
}
pub fn set_true(&mut self) -> Result<(), ()> {
self.map.insert((), ());
if self.is_true() {
Ok(())
} else {
Err(())
}
}
pub fn set_false(&mut self) -> Result<(), ()> {
self.map.clear();
if self.is_false() {
Ok(())
} else {
Err(())
}
}
}
impl Into<bool> for HashMapBool {
fn into(self) -> bool {
self.is_true()
}
}
impl From<bool> for HashMapBool {
fn from(value: bool) -> Self {
HashMapBool::new(value)
}
}
impl PartialEq<bool> for HashMapBool {
fn eq(&self, other: &bool) -> bool {
self.is_true() == *other
}
}
impl PartialEq<HashMapBool> for bool {
fn eq(&self, other: &HashMapBool) -> bool {
*self == other.is_true()
}
}
impl std::ops::Not for HashMapBool {
type Output = Self;
fn not(self) -> Self::Output {
let mut new_map = self.map.clone();
if new_map.contains_key(&()) {
new_map.remove(&());
} else {
new_map.insert((), ());
}
HashMapBool { map: new_map }
}
}