pub mod event;
pub mod iter;
pub mod property;
pub mod store;
pub use event::ChangeEvent;
pub use iter::{ChangeIterator, TimeoutIter, TryIter};
pub use property::Property;
pub use store::{PropertyBag, StateStore};
pub mod prelude {
pub use crate::event::ChangeEvent;
pub use crate::iter::ChangeIterator;
pub use crate::property::Property;
pub use crate::store::{PropertyBag, StateStore};
}
#[cfg(test)]
mod tests {
use super::*;
#[derive(Clone, PartialEq, Debug)]
struct Volume(u8);
impl Property for Volume {
const KEY: &'static str = "volume";
}
#[derive(Clone, PartialEq, Debug)]
struct Mute(bool);
impl Property for Mute {
const KEY: &'static str = "mute";
}
#[test]
fn test_full_workflow() {
let store = StateStore::<String>::new();
store.set(&"speaker-1".to_string(), Volume(50));
store.set(&"speaker-1".to_string(), Mute(false));
assert_eq!(
store.get::<Volume>(&"speaker-1".to_string()),
Some(Volume(50))
);
assert_eq!(
store.get::<Mute>(&"speaker-1".to_string()),
Some(Mute(false))
);
store.watch("speaker-1".to_string(), Volume::KEY);
store.set(&"speaker-1".to_string(), Volume(75));
let event = store
.iter()
.recv_timeout(std::time::Duration::from_millis(100));
assert!(event.is_some());
assert_eq!(event.unwrap().property_key, Volume::KEY);
}
#[test]
fn test_multiple_entities() {
let store = StateStore::<String>::new();
store.set(&"speaker-1".to_string(), Volume(50));
store.set(&"speaker-2".to_string(), Volume(75));
store.set(&"speaker-3".to_string(), Volume(100));
assert_eq!(store.entity_count(), 3);
assert_eq!(
store.get::<Volume>(&"speaker-1".to_string()),
Some(Volume(50))
);
assert_eq!(
store.get::<Volume>(&"speaker-2".to_string()),
Some(Volume(75))
);
assert_eq!(
store.get::<Volume>(&"speaker-3".to_string()),
Some(Volume(100))
);
}
#[test]
fn test_store_clone_shares_state() {
let store1 = StateStore::<String>::new();
let store2 = store1.clone();
store1.set(&"speaker-1".to_string(), Volume(50));
assert_eq!(
store2.get::<Volume>(&"speaker-1".to_string()),
Some(Volume(50))
);
}
}