node.rs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. use crate::{state::State, tree::NodeId};
  2. use dioxus_core::{AnyValueRc, AttributeValue, ElementId};
  3. use rustc_hash::{FxHashMap, FxHashSet};
  4. use std::fmt::Debug;
  5. /// The node is stored client side and stores only basic data about the node.
  6. #[derive(Debug, Clone)]
  7. pub struct Node<S: State> {
  8. /// The transformed state of the node.
  9. pub state: S,
  10. /// The raw data for the node
  11. pub node_data: NodeData,
  12. }
  13. #[derive(Debug, Clone)]
  14. pub struct NodeData {
  15. /// The id of the node
  16. pub node_id: NodeId,
  17. /// The id of the node in the vdom.
  18. pub element_id: Option<ElementId>,
  19. /// Additional inforation specific to the node type
  20. pub node_type: NodeType,
  21. }
  22. /// A type of node with data specific to the node type. The types are a subset of the [VNode] types.
  23. #[derive(Debug, Clone)]
  24. pub enum NodeType {
  25. Text {
  26. text: String,
  27. },
  28. Element {
  29. tag: String,
  30. namespace: Option<String>,
  31. attributes: FxHashMap<OwnedAttributeDiscription, OwnedAttributeValue>,
  32. listeners: FxHashSet<String>,
  33. },
  34. Placeholder,
  35. }
  36. impl<S: State> Node<S> {
  37. pub(crate) fn new(node_type: NodeType) -> Self {
  38. Node {
  39. state: S::default(),
  40. node_data: NodeData {
  41. element_id: None,
  42. node_type,
  43. node_id: NodeId(0),
  44. },
  45. }
  46. }
  47. /// get the mounted id of the node
  48. pub fn mounted_id(&self) -> Option<ElementId> {
  49. self.node_data.element_id
  50. }
  51. }
  52. #[derive(Debug, Clone, Hash, PartialEq, Eq)]
  53. pub struct OwnedAttributeDiscription {
  54. pub name: String,
  55. pub namespace: Option<String>,
  56. pub volatile: bool,
  57. }
  58. /// An attribute on a DOM node, such as `id="my-thing"` or
  59. /// `href="https://example.com"`.
  60. #[derive(Clone, Copy, Debug)]
  61. pub struct OwnedAttributeView<'a> {
  62. /// The discription of the attribute.
  63. pub attribute: &'a OwnedAttributeDiscription,
  64. /// The value of the attribute.
  65. pub value: &'a OwnedAttributeValue,
  66. }
  67. #[derive(Clone)]
  68. pub enum OwnedAttributeValue {
  69. Text(String),
  70. Float(f64),
  71. Int(i64),
  72. Bool(bool),
  73. Any(AnyValueRc),
  74. None,
  75. }
  76. impl Debug for OwnedAttributeValue {
  77. fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
  78. match self {
  79. Self::Text(arg0) => f.debug_tuple("Text").field(arg0).finish(),
  80. Self::Float(arg0) => f.debug_tuple("Float").field(arg0).finish(),
  81. Self::Int(arg0) => f.debug_tuple("Int").field(arg0).finish(),
  82. Self::Bool(arg0) => f.debug_tuple("Bool").field(arg0).finish(),
  83. Self::Any(_) => f.debug_tuple("Any").finish(),
  84. Self::None => write!(f, "None"),
  85. }
  86. }
  87. }
  88. impl From<AttributeValue<'_>> for OwnedAttributeValue {
  89. fn from(value: AttributeValue<'_>) -> Self {
  90. match value {
  91. AttributeValue::Text(text) => Self::Text(text.to_string()),
  92. AttributeValue::Float(float) => Self::Float(float),
  93. AttributeValue::Int(int) => Self::Int(int),
  94. AttributeValue::Bool(bool) => Self::Bool(bool),
  95. AttributeValue::Any(any) => Self::Any(any),
  96. AttributeValue::None => Self::None,
  97. _ => Self::None,
  98. }
  99. }
  100. }
  101. impl OwnedAttributeValue {
  102. pub fn as_text(&self) -> Option<&str> {
  103. match self {
  104. OwnedAttributeValue::Text(text) => Some(text),
  105. _ => None,
  106. }
  107. }
  108. pub fn as_float(&self) -> Option<f64> {
  109. match self {
  110. OwnedAttributeValue::Float(float) => Some(*float),
  111. _ => None,
  112. }
  113. }
  114. pub fn as_int(&self) -> Option<i64> {
  115. match self {
  116. OwnedAttributeValue::Int(int) => Some(*int),
  117. _ => None,
  118. }
  119. }
  120. pub fn as_bool(&self) -> Option<bool> {
  121. match self {
  122. OwnedAttributeValue::Bool(bool) => Some(*bool),
  123. _ => None,
  124. }
  125. }
  126. pub fn as_none(&self) -> Option<()> {
  127. match self {
  128. OwnedAttributeValue::None => Some(()),
  129. _ => None,
  130. }
  131. }
  132. }