2023-06-22 23:07:40 +01:00
|
|
|
#![doc = include_str!("../docs/Ironvars.md")]
|
|
|
|
|
2025-05-18 15:17:09 +01:00
|
|
|
use crate::channels::SyncSenderExt;
|
|
|
|
use crate::{arc_rw, read_lock, write_lock};
|
2023-06-22 23:07:40 +01:00
|
|
|
use color_eyre::{Report, Result};
|
|
|
|
use std::collections::HashMap;
|
2025-02-23 16:12:02 +00:00
|
|
|
use std::sync::{Arc, RwLock};
|
2023-06-22 23:07:40 +01:00
|
|
|
use tokio::sync::broadcast;
|
|
|
|
|
2025-02-23 16:12:02 +00:00
|
|
|
type NamespaceTrait = Arc<dyn Namespace + Sync + Send>;
|
|
|
|
|
|
|
|
pub trait Namespace {
|
|
|
|
fn get(&self, key: &str) -> Option<String>;
|
|
|
|
fn list(&self) -> Vec<String>;
|
|
|
|
|
|
|
|
fn get_all(&self) -> HashMap<Box<str>, String> {
|
|
|
|
self.list()
|
|
|
|
.into_iter()
|
|
|
|
.filter_map(|name| self.get(&name).map(|value| (name.into(), value)))
|
|
|
|
.collect()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn namespaces(&self) -> Vec<String>;
|
|
|
|
fn get_namespace(&self, key: &str) -> Option<NamespaceTrait>;
|
|
|
|
}
|
|
|
|
|
|
|
|
pub trait WritableNamespace: Namespace {
|
|
|
|
fn set(&self, key: &str, value: String) -> Result<()>;
|
|
|
|
}
|
|
|
|
|
2023-06-22 23:07:40 +01:00
|
|
|
/// Global singleton manager for `IronVar` variables.
|
|
|
|
pub struct VariableManager {
|
2025-02-23 16:12:02 +00:00
|
|
|
variables: Arc<RwLock<HashMap<Box<str>, IronVar>>>,
|
|
|
|
namespaces: Arc<RwLock<HashMap<Box<str>, NamespaceTrait>>>,
|
2023-06-22 23:07:40 +01:00
|
|
|
}
|
|
|
|
|
2023-12-08 22:39:27 +00:00
|
|
|
impl Default for VariableManager {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self::new()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-22 23:07:40 +01:00
|
|
|
impl VariableManager {
|
|
|
|
pub fn new() -> Self {
|
|
|
|
Self {
|
2025-02-23 16:12:02 +00:00
|
|
|
variables: arc_rw!(HashMap::new()),
|
|
|
|
namespaces: arc_rw!(HashMap::new()),
|
2023-06-22 23:07:40 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Subscribes to an `ironvar`, creating it if it does not exist.
|
|
|
|
/// Any time the var is set, its value is sent on the channel.
|
2025-02-23 16:12:02 +00:00
|
|
|
pub fn subscribe(&self, key: Box<str>) -> broadcast::Receiver<Option<String>> {
|
|
|
|
write_lock!(self.variables)
|
2023-06-22 23:07:40 +01:00
|
|
|
.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 == '-')
|
|
|
|
}
|
2025-02-23 16:12:02 +00:00
|
|
|
|
|
|
|
pub fn register_namespace<N>(&self, name: &str, namespace: Arc<N>)
|
|
|
|
where
|
|
|
|
N: Namespace + Sync + Send + 'static,
|
|
|
|
{
|
|
|
|
write_lock!(self.namespaces).insert(name.into(), namespace);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Namespace for VariableManager {
|
|
|
|
fn get(&self, key: &str) -> Option<String> {
|
|
|
|
if key.contains('.') {
|
2025-05-18 15:17:09 +01:00
|
|
|
let (ns, key) = key.split_once('.')?;
|
2025-02-23 16:12:02 +00:00
|
|
|
|
|
|
|
let namespaces = read_lock!(self.namespaces);
|
2025-05-18 15:17:09 +01:00
|
|
|
let ns = namespaces.get(ns)?;
|
2025-02-23 16:12:02 +00:00
|
|
|
|
|
|
|
ns.get(key).map(|v| v.to_owned())
|
|
|
|
} else {
|
|
|
|
read_lock!(self.variables).get(key).and_then(IronVar::get)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn list(&self) -> Vec<String> {
|
|
|
|
read_lock!(self.variables)
|
|
|
|
.keys()
|
|
|
|
.map(ToString::to_string)
|
|
|
|
.collect()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_all(&self) -> HashMap<Box<str>, String> {
|
|
|
|
read_lock!(self.variables)
|
|
|
|
.iter()
|
|
|
|
.filter_map(|(k, v)| v.get().map(|value| (k.clone(), value)))
|
|
|
|
.collect()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn namespaces(&self) -> Vec<String> {
|
|
|
|
read_lock!(self.namespaces)
|
|
|
|
.keys()
|
|
|
|
.map(ToString::to_string)
|
|
|
|
.collect()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_namespace(&self, key: &str) -> Option<NamespaceTrait> {
|
|
|
|
read_lock!(self.namespaces).get(key).cloned()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl WritableNamespace for VariableManager {
|
|
|
|
/// Sets the value for a variable,
|
|
|
|
/// creating it if it does not exist.
|
|
|
|
fn set(&self, key: &str, value: String) -> Result<()> {
|
|
|
|
if Self::key_is_valid(key) {
|
|
|
|
if let Some(var) = write_lock!(self.variables).get_mut(&Box::from(key)) {
|
|
|
|
var.set(Some(value));
|
|
|
|
} else {
|
|
|
|
let var = IronVar::new(Some(value));
|
|
|
|
write_lock!(self.variables).insert(key.into(), var);
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
} else {
|
|
|
|
Err(Report::msg("Invalid key"))
|
|
|
|
}
|
|
|
|
}
|
2023-06-22 23:07:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Ironbar dynamic variable representation.
|
|
|
|
/// Interact with them through the `VARIABLE_MANAGER` `VariableManager` singleton.
|
|
|
|
#[derive(Debug)]
|
2024-04-13 23:08:45 +01:00
|
|
|
pub struct IronVar {
|
2023-06-22 23:07:40 +01:00
|
|
|
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.
|
2024-04-13 23:08:45 +01:00
|
|
|
pub fn get(&self) -> Option<String> {
|
2023-06-22 23:07:40 +01:00
|
|
|
self.value.clone()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Sets the current variable value.
|
|
|
|
/// The change is broadcast to all receivers.
|
|
|
|
fn set(&mut self, value: Option<String>) {
|
2024-05-11 20:55:43 +01:00
|
|
|
self.value.clone_from(&value);
|
2025-05-18 15:17:09 +01:00
|
|
|
self.tx.send_expect(value);
|
2023-06-22 23:07:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// 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();
|
2025-05-18 15:17:09 +01:00
|
|
|
self.tx.send_expect(self.value.clone());
|
2023-06-22 23:07:40 +01:00
|
|
|
rx
|
|
|
|
}
|
|
|
|
}
|