use serde::{Deserialize, Serialize};
use std::any::Any;
use std::fmt;
use std::sync::{Arc, RwLock};
type SubscriberFn<T> = Box<dyn Fn(&T) + Send + Sync>;
type Subscribers<T> = Arc<RwLock<Vec<SubscriberFn<T>>>>;
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct PropertyPath {
segments: Vec<String>,
}
impl PropertyPath {
#[must_use]
pub fn new(path: &str) -> Self {
let segments = path
.split('.')
.filter(|s| !s.is_empty())
.map(String::from)
.collect();
Self { segments }
}
#[must_use]
pub const fn root() -> Self {
Self {
segments: Vec::new(),
}
}
#[must_use]
pub fn segments(&self) -> &[String] {
&self.segments
}
#[must_use]
pub fn is_root(&self) -> bool {
self.segments.is_empty()
}
#[must_use]
pub fn len(&self) -> usize {
self.segments.len()
}
#[must_use]
pub fn is_empty(&self) -> bool {
self.segments.is_empty()
}
#[must_use]
pub fn join(&self, segment: &str) -> Self {
let mut segments = self.segments.clone();
segments.push(segment.to_string());
Self { segments }
}
#[must_use]
pub fn parent(&self) -> Option<Self> {
if self.segments.is_empty() {
None
} else {
let mut segments = self.segments.clone();
segments.pop();
Some(Self { segments })
}
}
#[must_use]
pub fn leaf(&self) -> Option<&str> {
self.segments.last().map(String::as_str)
}
#[must_use]
pub fn to_string_path(&self) -> String {
self.segments.join(".")
}
}
impl fmt::Display for PropertyPath {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.to_string_path())
}
}
impl From<&str> for PropertyPath {
fn from(s: &str) -> Self {
Self::new(s)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum BindingDirection {
#[default]
OneWay,
TwoWay,
OneTime,
}
#[derive(Debug, Clone)]
pub struct BindingConfig {
pub source: PropertyPath,
pub target: String,
pub direction: BindingDirection,
pub transform: Option<String>,
pub fallback: Option<String>,
}
impl BindingConfig {
#[must_use]
pub fn one_way(source: impl Into<PropertyPath>, target: impl Into<String>) -> Self {
Self {
source: source.into(),
target: target.into(),
direction: BindingDirection::OneWay,
transform: None,
fallback: None,
}
}
#[must_use]
pub fn two_way(source: impl Into<PropertyPath>, target: impl Into<String>) -> Self {
Self {
source: source.into(),
target: target.into(),
direction: BindingDirection::TwoWay,
transform: None,
fallback: None,
}
}
#[must_use]
pub fn transform(mut self, name: impl Into<String>) -> Self {
self.transform = Some(name.into());
self
}
#[must_use]
pub fn fallback(mut self, value: impl Into<String>) -> Self {
self.fallback = Some(value.into());
self
}
}
pub trait Bindable: Any + Send + Sync {
fn bindings(&self) -> Vec<BindingConfig>;
fn set_bindings(&mut self, bindings: Vec<BindingConfig>);
fn apply_binding(&mut self, target: &str, value: &dyn Any) -> bool;
fn get_binding_value(&self, target: &str) -> Option<Box<dyn Any + Send>>;
}
pub struct ReactiveCell<T> {
value: Arc<RwLock<T>>,
subscribers: Subscribers<T>,
}
impl<T: Clone + Send + Sync + 'static> ReactiveCell<T> {
pub fn new(value: T) -> Self {
Self {
value: Arc::new(RwLock::new(value)),
subscribers: Arc::new(RwLock::new(Vec::new())),
}
}
pub fn get(&self) -> T {
self.value
.read()
.expect("ReactiveCell lock poisoned")
.clone()
}
pub fn set(&self, value: T) {
{
let mut guard = self.value.write().expect("ReactiveCell lock poisoned");
*guard = value;
}
self.notify();
}
pub fn update<F>(&self, f: F)
where
F: FnOnce(&mut T),
{
{
let mut guard = self.value.write().expect("ReactiveCell lock poisoned");
f(&mut guard);
}
self.notify();
}
pub fn subscribe<F>(&self, callback: F)
where
F: Fn(&T) + Send + Sync + 'static,
{
self.subscribers
.write()
.expect("ReactiveCell lock poisoned")
.push(Box::new(callback));
}
fn notify(&self) {
let value = self.value.read().expect("ReactiveCell lock poisoned");
let subscribers = self.subscribers.read().expect("ReactiveCell lock poisoned");
for sub in subscribers.iter() {
sub(&value);
}
}
}
impl<T: Clone + Send + Sync> Clone for ReactiveCell<T> {
fn clone(&self) -> Self {
Self {
value: self.value.clone(),
subscribers: Arc::new(RwLock::new(Vec::new())), }
}
}
impl<T: Clone + Send + Sync + Default + 'static> Default for ReactiveCell<T> {
fn default() -> Self {
Self::new(T::default())
}
}
impl<T: Clone + Send + Sync + fmt::Debug + 'static> fmt::Debug for ReactiveCell<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("ReactiveCell")
.field(
"value",
&*self.value.read().expect("ReactiveCell lock poisoned"),
)
.finish_non_exhaustive()
}
}
pub struct Computed<T> {
#[allow(dead_code)]
compute: Box<dyn Fn() -> T + Send + Sync>,
cached: Arc<RwLock<Option<T>>>,
dirty: Arc<RwLock<bool>>,
}
impl<T: Clone + Send + Sync + 'static> Computed<T> {
pub fn new<F>(compute: F) -> Self
where
F: Fn() -> T + Send + Sync + 'static,
{
Self {
compute: Box::new(compute),
cached: Arc::new(RwLock::new(None)),
dirty: Arc::new(RwLock::new(true)),
}
}
pub fn get(&self) -> T {
let dirty = *self.dirty.read().expect("Computed lock poisoned");
if dirty {
let value = (self.compute)();
*self.cached.write().expect("Computed lock poisoned") = Some(value.clone());
*self.dirty.write().expect("Computed lock poisoned") = false;
value
} else {
self.cached
.read()
.expect("Computed lock poisoned")
.clone()
.expect("Computed cache should contain value when not dirty")
}
}
pub fn invalidate(&self) {
*self.dirty.write().expect("Computed lock poisoned") = true;
}
}
impl<T: Clone + Send + Sync + fmt::Debug + 'static> fmt::Debug for Computed<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Computed")
.field(
"cached",
&*self.cached.read().expect("Computed lock poisoned"),
)
.field(
"dirty",
&*self.dirty.read().expect("Computed lock poisoned"),
)
.finish_non_exhaustive()
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BindingExpression {
pub expression: String,
pub dependencies: Vec<PropertyPath>,
}
impl BindingExpression {
#[must_use]
pub fn new(expression: impl Into<String>) -> Self {
let expression = expression.into();
let dependencies = Self::parse_dependencies(&expression);
Self {
expression,
dependencies,
}
}
#[must_use]
pub fn property(path: impl Into<PropertyPath>) -> Self {
let path: PropertyPath = path.into();
let expression = format!("{{{{ {} }}}}", path.to_string_path());
Self {
expression,
dependencies: vec![path],
}
}
#[must_use]
pub fn is_simple_property(&self) -> bool {
self.dependencies.len() == 1
&& self.expression.trim().starts_with("{{")
&& self.expression.trim().ends_with("}}")
}
#[must_use]
pub fn as_property(&self) -> Option<&PropertyPath> {
if self.is_simple_property() {
self.dependencies.first()
} else {
None
}
}
fn parse_dependencies(expression: &str) -> Vec<PropertyPath> {
let mut deps = Vec::new();
let mut in_binding = false;
let mut current = String::new();
let chars: Vec<char> = expression.chars().collect();
let mut i = 0;
while i < chars.len() {
if i + 1 < chars.len() && chars[i] == '{' && chars[i + 1] == '{' {
in_binding = true;
i += 2;
continue;
}
if i + 1 < chars.len() && chars[i] == '}' && chars[i + 1] == '}' {
if !current.is_empty() {
let path_str = current.split('|').next().unwrap_or("").trim();
if !path_str.is_empty() && !path_str.contains(|c: char| c.is_whitespace()) {
deps.push(PropertyPath::new(path_str));
}
current.clear();
}
in_binding = false;
i += 2;
continue;
}
if in_binding {
current.push(chars[i]);
}
i += 1;
}
deps
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EventBinding {
pub event: String,
pub action: ActionBinding,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum ActionBinding {
SetProperty {
path: PropertyPath,
value: String,
},
ToggleProperty {
path: PropertyPath,
},
IncrementProperty {
path: PropertyPath,
amount: Option<f64>,
},
Navigate {
route: String,
},
Dispatch {
message: String,
payload: Option<String>,
},
Batch {
actions: Vec<Self>,
},
}
impl EventBinding {
#[must_use]
pub fn new(event: impl Into<String>, action: ActionBinding) -> Self {
Self {
event: event.into(),
action,
}
}
#[must_use]
pub fn on_click(action: ActionBinding) -> Self {
Self::new("click", action)
}
#[must_use]
pub fn on_change(action: ActionBinding) -> Self {
Self::new("change", action)
}
}
impl ActionBinding {
#[must_use]
pub fn set(path: impl Into<PropertyPath>, value: impl Into<String>) -> Self {
Self::SetProperty {
path: path.into(),
value: value.into(),
}
}
#[must_use]
pub fn toggle(path: impl Into<PropertyPath>) -> Self {
Self::ToggleProperty { path: path.into() }
}
#[must_use]
pub fn increment(path: impl Into<PropertyPath>) -> Self {
Self::IncrementProperty {
path: path.into(),
amount: None,
}
}
#[must_use]
pub fn increment_by(path: impl Into<PropertyPath>, amount: f64) -> Self {
Self::IncrementProperty {
path: path.into(),
amount: Some(amount),
}
}
#[must_use]
pub fn navigate(route: impl Into<String>) -> Self {
Self::Navigate {
route: route.into(),
}
}
#[must_use]
pub fn dispatch(message: impl Into<String>) -> Self {
Self::Dispatch {
message: message.into(),
payload: None,
}
}
#[must_use]
pub fn dispatch_with(message: impl Into<String>, payload: impl Into<String>) -> Self {
Self::Dispatch {
message: message.into(),
payload: Some(payload.into()),
}
}
#[must_use]
pub fn batch(actions: impl IntoIterator<Item = Self>) -> Self {
Self::Batch {
actions: actions.into_iter().collect(),
}
}
}
#[derive(Debug, Default)]
pub struct BindingManager {
bindings: Vec<ActiveBinding>,
debounce_ms: Option<u32>,
pending_updates: Vec<PendingUpdate>,
}
#[derive(Debug, Clone)]
pub struct ActiveBinding {
pub id: BindingId,
pub widget_id: String,
pub config: BindingConfig,
pub current_value: Option<String>,
pub active: bool,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub struct BindingId(pub u64);
#[derive(Debug, Clone)]
pub struct PendingUpdate {
pub source: UpdateSource,
pub path: PropertyPath,
pub value: String,
pub timestamp: u64,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum UpdateSource {
State,
Widget,
}
impl BindingManager {
#[must_use]
pub fn new() -> Self {
Self::default()
}
#[must_use]
pub fn with_debounce(mut self, ms: u32) -> Self {
self.debounce_ms = Some(ms);
self
}
pub fn register(&mut self, widget_id: impl Into<String>, config: BindingConfig) -> BindingId {
let id = BindingId(self.bindings.len() as u64);
self.bindings.push(ActiveBinding {
id,
widget_id: widget_id.into(),
config,
current_value: None,
active: true,
});
id
}
pub fn unregister(&mut self, id: BindingId) {
if let Some(binding) = self.bindings.iter_mut().find(|b| b.id == id) {
binding.active = false;
}
}
#[must_use]
pub fn bindings_for_widget(&self, widget_id: &str) -> Vec<&ActiveBinding> {
self.bindings
.iter()
.filter(|b| b.active && b.widget_id == widget_id)
.collect()
}
#[must_use]
pub fn bindings_for_path(&self, path: &PropertyPath) -> Vec<&ActiveBinding> {
self.bindings
.iter()
.filter(|b| b.active && &b.config.source == path)
.collect()
}
pub fn on_state_change(&mut self, path: &PropertyPath, value: &str) -> Vec<WidgetUpdate> {
let mut updates = Vec::new();
for binding in &mut self.bindings {
if !binding.active {
continue;
}
if &binding.config.source == path
|| path
.to_string_path()
.starts_with(&binding.config.source.to_string_path())
{
binding.current_value = Some(value.to_string());
updates.push(WidgetUpdate {
widget_id: binding.widget_id.clone(),
property: binding.config.target.clone(),
value: value.to_string(),
});
}
}
updates
}
pub fn on_widget_change(
&mut self,
widget_id: &str,
property: &str,
value: &str,
) -> Vec<StateUpdate> {
let mut updates = Vec::new();
for binding in &self.bindings {
if !binding.active {
continue;
}
if binding.config.direction != BindingDirection::TwoWay {
continue;
}
if binding.widget_id == widget_id && binding.config.target == property {
updates.push(StateUpdate {
path: binding.config.source.clone(),
value: value.to_string(),
});
}
}
updates
}
pub fn queue_update(&mut self, source: UpdateSource, path: PropertyPath, value: String) {
self.pending_updates.push(PendingUpdate {
source,
path,
value,
timestamp: 0, });
}
pub fn flush(&mut self) -> (Vec<WidgetUpdate>, Vec<StateUpdate>) {
let mut widget_updates = Vec::new();
let mut state_updates = Vec::new();
let updates: Vec<PendingUpdate> = self.pending_updates.drain(..).collect();
for update in updates {
match update.source {
UpdateSource::State => {
widget_updates.extend(self.on_state_change(&update.path, &update.value));
}
UpdateSource::Widget => {
state_updates.push(StateUpdate {
path: update.path,
value: update.value,
});
}
}
}
(widget_updates, state_updates)
}
#[must_use]
pub fn active_count(&self) -> usize {
self.bindings.iter().filter(|b| b.active).count()
}
pub fn clear(&mut self) {
self.bindings.clear();
self.pending_updates.clear();
}
}
#[derive(Debug, Clone)]
pub struct WidgetUpdate {
pub widget_id: String,
pub property: String,
pub value: String,
}
#[derive(Debug, Clone)]
pub struct StateUpdate {
pub path: PropertyPath,
pub value: String,
}
pub trait ValueConverter: Send + Sync {
fn convert(&self, value: &str) -> Result<String, ConversionError>;
fn convert_back(&self, value: &str) -> Result<String, ConversionError>;
}
#[derive(Debug, Clone)]
pub struct ConversionError {
pub message: String,
}
impl fmt::Display for ConversionError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "conversion error: {}", self.message)
}
}
impl std::error::Error for ConversionError {}
#[derive(Debug, Default)]
pub struct IdentityConverter;
impl ValueConverter for IdentityConverter {
fn convert(&self, value: &str) -> Result<String, ConversionError> {
Ok(value.to_string())
}
fn convert_back(&self, value: &str) -> Result<String, ConversionError> {
Ok(value.to_string())
}
}
#[derive(Debug, Default)]
pub struct BoolToStringConverter {
pub true_string: String,
pub false_string: String,
}
impl BoolToStringConverter {
#[must_use]
pub fn new() -> Self {
Self {
true_string: "true".to_string(),
false_string: "false".to_string(),
}
}
#[must_use]
pub fn with_strings(true_str: impl Into<String>, false_str: impl Into<String>) -> Self {
Self {
true_string: true_str.into(),
false_string: false_str.into(),
}
}
}
impl ValueConverter for BoolToStringConverter {
fn convert(&self, value: &str) -> Result<String, ConversionError> {
match value {
"true" | "1" | "yes" => Ok(self.true_string.clone()),
"false" | "0" | "no" => Ok(self.false_string.clone()),
_ => Err(ConversionError {
message: format!("cannot convert '{value}' to bool"),
}),
}
}
fn convert_back(&self, value: &str) -> Result<String, ConversionError> {
if value == self.true_string {
Ok("true".to_string())
} else if value == self.false_string {
Ok("false".to_string())
} else {
Err(ConversionError {
message: format!("cannot convert '{value}' back to bool"),
})
}
}
}
#[derive(Debug, Default)]
pub struct NumberFormatConverter {
pub decimals: usize,
pub prefix: String,
pub suffix: String,
}
impl NumberFormatConverter {
#[must_use]
pub fn new() -> Self {
Self::default()
}
#[must_use]
pub fn decimals(mut self, places: usize) -> Self {
self.decimals = places;
self
}
#[must_use]
pub fn prefix(mut self, prefix: impl Into<String>) -> Self {
self.prefix = prefix.into();
self
}
#[must_use]
pub fn suffix(mut self, suffix: impl Into<String>) -> Self {
self.suffix = suffix.into();
self
}
}
impl ValueConverter for NumberFormatConverter {
fn convert(&self, value: &str) -> Result<String, ConversionError> {
let num: f64 = value.parse().map_err(|_| ConversionError {
message: format!("cannot parse '{value}' as number"),
})?;
let formatted = format!("{:.prec$}", num, prec = self.decimals);
Ok(format!("{}{}{}", self.prefix, formatted, self.suffix))
}
fn convert_back(&self, value: &str) -> Result<String, ConversionError> {
let stripped = value
.strip_prefix(&self.prefix)
.unwrap_or(value)
.strip_suffix(&self.suffix)
.unwrap_or(value)
.trim();
let _: f64 = stripped.parse().map_err(|_| ConversionError {
message: format!("cannot parse '{stripped}' as number"),
})?;
Ok(stripped.to_string())
}
}
#[cfg(test)]
#[allow(clippy::unwrap_used, clippy::disallowed_methods)]
#[path = "binding_tests.rs"]
mod tests;