template.rs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. use std::{cell::Cell, hash::Hash};
  2. use crate::{Attribute, ElementId, Listener, VNode};
  3. /// A reference to a template along with any context needed to hydrate it
  4. pub struct VTemplate<'a> {
  5. pub key: Option<&'a str>,
  6. // The ID assigned for all nodes in this template
  7. pub node_id: Cell<ElementId>,
  8. // All the IDs for the roots of this template
  9. // Non-assigned IDs are set to 0
  10. pub root_ids: &'a [Cell<ElementId>],
  11. pub template: Template<'static>,
  12. /// All the non-root dynamic nodes
  13. pub dynamic_nodes: &'a [NodeLocation<'a>],
  14. pub dynamic_attrs: &'a [AttributeLocation<'a>],
  15. pub listeners: &'a [Listener<'a>],
  16. }
  17. /// A template that is created at compile time
  18. #[derive(Clone, Copy)]
  19. pub struct Template<'a> {
  20. /// name, line, col, or some sort of identifier
  21. pub id: &'static str,
  22. /// All the roots of the template. ie rsx! { div {} div{} } would have two roots
  23. pub roots: &'a [TemplateNode<'a>],
  24. }
  25. impl<'a> Eq for Template<'a> {}
  26. impl<'a> PartialEq for Template<'a> {
  27. fn eq(&self, other: &Self) -> bool {
  28. self.id == other.id
  29. }
  30. }
  31. impl<'a> Hash for Template<'a> {
  32. fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
  33. self.id.hash(state);
  34. }
  35. }
  36. /// A weird-ish variant of VNodes with way more limited types
  37. pub enum TemplateNode<'a> {
  38. /// A simple element
  39. Element {
  40. tag: &'static str,
  41. namespace: Option<&'static str>,
  42. attrs: &'a [TemplateAttribute<'a>],
  43. children: &'a [TemplateNode<'a>],
  44. },
  45. Text(&'static str),
  46. Dynamic(usize),
  47. }
  48. pub struct TemplateAttribute<'a> {
  49. pub name: &'static str,
  50. pub value: &'a str,
  51. pub namespace: Option<&'static str>,
  52. pub volatile: bool,
  53. }
  54. pub struct AttributeLocation<'a> {
  55. pub pathway: &'static [u8],
  56. pub mounted_element: Cell<ElementId>,
  57. pub attrs: &'a [Attribute<'a>],
  58. pub listeners: &'a [Listener<'a>],
  59. }
  60. pub struct NodeLocation<'a> {
  61. pub pathway: &'static [u8],
  62. pub node: VNode<'a>,
  63. }