use rustc_hash::{FxHashMap, FxHashSet}; use std::{any::Any, fmt::Debug}; #[derive(Debug, Clone)] pub struct ElementNode { pub tag: String, pub namespace: Option, pub attributes: FxHashMap>, pub listeners: FxHashSet, } /// A type of node with data specific to the node type. The types are a subset of the [VNode] types. #[derive(Debug, Clone)] pub enum NodeType { Text(String), Element(ElementNode), Placeholder, } #[derive(Debug, Clone, Hash, PartialEq, Eq)] pub struct OwnedAttributeDiscription { pub name: String, pub namespace: Option, pub volatile: bool, } /// An attribute on a DOM node, such as `id="my-thing"` or /// `href="https://example.com"`. #[derive(Clone, Copy, Debug)] pub struct OwnedAttributeView<'a, V: FromAnyValue = ()> { /// The discription of the attribute. pub attribute: &'a OwnedAttributeDiscription, /// The value of the attribute. pub value: &'a OwnedAttributeValue, } #[derive(Clone)] pub enum OwnedAttributeValue { Text(String), Float(f64), Int(i64), Bool(bool), Custom(V), } pub trait FromAnyValue: Clone + 'static { fn from_any_value(value: &dyn Any) -> Self; } impl FromAnyValue for () { fn from_any_value(_: &dyn Any) -> Self {} } impl Debug for OwnedAttributeValue { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { Self::Text(arg0) => f.debug_tuple("Text").field(arg0).finish(), Self::Float(arg0) => f.debug_tuple("Float").field(arg0).finish(), Self::Int(arg0) => f.debug_tuple("Int").field(arg0).finish(), Self::Bool(arg0) => f.debug_tuple("Bool").field(arg0).finish(), Self::Custom(_) => f.debug_tuple("Any").finish(), } } } #[cfg(feature = "dioxus")] impl From> for OwnedAttributeValue { fn from(value: dioxus_core::BorrowedAttributeValue<'_>) -> Self { match value { dioxus_core::BorrowedAttributeValue::Text(text) => Self::Text(text.to_string()), dioxus_core::BorrowedAttributeValue::Float(float) => Self::Float(float), dioxus_core::BorrowedAttributeValue::Int(int) => Self::Int(int), dioxus_core::BorrowedAttributeValue::Bool(bool) => Self::Bool(bool), dioxus_core::BorrowedAttributeValue::Any(any) => Self::Custom(V::from_any_value(any.as_any())), dioxus_core::BorrowedAttributeValue::None => panic!("None attribute values result in removing the attribute, not converting it to a None value.") } } } impl OwnedAttributeValue { pub fn as_text(&self) -> Option<&str> { match self { OwnedAttributeValue::Text(text) => Some(text), _ => None, } } pub fn as_float(&self) -> Option { match self { OwnedAttributeValue::Float(float) => Some(*float), _ => None, } } pub fn as_int(&self) -> Option { match self { OwnedAttributeValue::Int(int) => Some(*int), _ => None, } } pub fn as_bool(&self) -> Option { match self { OwnedAttributeValue::Bool(bool) => Some(*bool), _ => None, } } pub fn as_custom(&self) -> Option<&V> { match self { OwnedAttributeValue::Custom(custom) => Some(custom), _ => None, } } }