use crate::{state::State, tree::NodeId}; use dioxus_core::{AnyValue, BorrowedAttributeValue, ElementId}; use rustc_hash::{FxHashMap, FxHashSet}; use std::fmt::Debug; /// The node is stored client side and stores only basic data about the node. #[derive(Debug, Clone)] pub struct Node { /// The transformed state of the node. pub state: S, /// The raw data for the node pub node_data: NodeData, } #[derive(Debug, Clone)] pub struct NodeData { /// The id of the node pub node_id: NodeId, /// The id of the node in the vdom. pub element_id: Option, /// Additional inforation specific to the node type pub node_type: NodeType, } /// 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 { text: String, }, Element { tag: String, namespace: Option, attributes: FxHashMap>, listeners: FxHashSet, }, Placeholder, } impl Node { pub(crate) fn new(node_type: NodeType) -> Self { Node { state: S::default(), node_data: NodeData { element_id: None, node_type, node_id: NodeId(0), }, } } /// get the mounted id of the node pub fn mounted_id(&self) -> Option { self.node_data.element_id } } #[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), None, } pub trait FromAnyValue { fn from_any_value(value: &dyn AnyValue) -> Self; } impl FromAnyValue for () { fn from_any_value(_: &dyn AnyValue) -> 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(), Self::None => write!(f, "None"), } } } impl From> for OwnedAttributeValue { fn from(value: BorrowedAttributeValue<'_>) -> Self { match value { BorrowedAttributeValue::Text(text) => Self::Text(text.to_string()), BorrowedAttributeValue::Float(float) => Self::Float(float), BorrowedAttributeValue::Int(int) => Self::Int(int), BorrowedAttributeValue::Bool(bool) => Self::Bool(bool), BorrowedAttributeValue::Any(any) => Self::Custom(V::from_any_value(&*any)), BorrowedAttributeValue::None => Self::None, } } } 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_none(&self) -> Option<()> { match self { OwnedAttributeValue::None => Some(()), _ => None, } } pub fn as_custom(&self) -> Option<&V> { match self { OwnedAttributeValue::Custom(custom) => Some(custom), _ => None, } } }