1
0
Fork 0
mirror of https://github.com/Zedfrigg/ironbar.git synced 2025-04-20 11:54:23 +02:00
ironbar/src/ironvar.rs
Jake Stanger b2fa19ab6c
refactor: begin restructuring core code to better encapsulate
This is a first pass towards trying to structure things a bit better, with data generally encapsulated under a single hierarchical tree, rather than lots of globals all over the place. Lots of work is still required.

The plan is that with this and some more work, #291 should become a lot easier to sort.
2023-12-08 22:39:27 +00:00

103 lines
2.8 KiB
Rust

#![doc = include_str!("../docs/Ironvars.md")]
use crate::send;
use color_eyre::{Report, Result};
use std::collections::HashMap;
use tokio::sync::broadcast;
/// Global singleton manager for `IronVar` variables.
pub struct VariableManager {
variables: HashMap<Box<str>, IronVar>,
}
impl Default for VariableManager {
fn default() -> Self {
Self::new()
}
}
impl VariableManager {
pub fn new() -> Self {
Self {
variables: HashMap::new(),
}
}
/// Sets the value for a variable,
/// creating it if it does not exist.
pub fn set(&mut self, key: Box<str>, value: String) -> Result<()> {
if Self::key_is_valid(&key) {
if let Some(var) = self.variables.get_mut(&key) {
var.set(Some(value));
} else {
let var = IronVar::new(Some(value));
self.variables.insert(key, var);
}
Ok(())
} else {
Err(Report::msg("Invalid key"))
}
}
/// Gets the current value of an `ironvar`.
/// Prefer to use `subscribe` where possible.
pub fn get(&self, key: &str) -> Option<String> {
self.variables.get(key).and_then(IronVar::get)
}
/// Subscribes to an `ironvar`, creating it if it does not exist.
/// Any time the var is set, its value is sent on the channel.
pub fn subscribe(&mut self, key: Box<str>) -> broadcast::Receiver<Option<String>> {
self.variables
.entry(key)
.or_insert_with(|| IronVar::new(None))
.subscribe()
}
fn key_is_valid(key: &str) -> bool {
!key.is_empty()
&& key
.chars()
.all(|char| char.is_alphanumeric() || char == '_' || char == '-')
}
}
/// Ironbar dynamic variable representation.
/// Interact with them through the `VARIABLE_MANAGER` `VariableManager` singleton.
#[derive(Debug)]
struct IronVar {
value: Option<String>,
tx: broadcast::Sender<Option<String>>,
_rx: broadcast::Receiver<Option<String>>,
}
impl IronVar {
/// Creates a new variable.
fn new(value: Option<String>) -> Self {
let (tx, rx) = broadcast::channel(32);
Self { value, tx, _rx: rx }
}
/// Gets the current variable value.
/// Prefer to subscribe to changes where possible.
fn get(&self) -> Option<String> {
self.value.clone()
}
/// Sets the current variable value.
/// The change is broadcast to all receivers.
fn set(&mut self, value: Option<String>) {
self.value = value.clone();
send!(self.tx, value);
}
/// Subscribes to the variable.
/// The latest value is immediately sent to all receivers.
fn subscribe(&self) -> broadcast::Receiver<Option<String>> {
let rx = self.tx.subscribe();
send!(self.tx, self.value.clone());
rx
}
}