mod.rs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. use std::{cell::Cell, num::NonZeroUsize};
  2. /// A reference to a template along with any context needed to hydrate it
  3. pub struct VTemplate<'a> {
  4. // The ID assigned for the root of this template
  5. pub node_id: Cell<ElementId>,
  6. pub template: &'static Template,
  7. /// All the dynamic nodes for a template
  8. pub dynamic_nodes: &'a [DynamicNode<'a>],
  9. pub dynamic_attrs: &'a [AttributeLocation<'a>],
  10. }
  11. #[derive(Debug, Clone, Copy)]
  12. pub struct Template {
  13. pub id: &'static str,
  14. pub root: TemplateNode<'static>,
  15. // todo: locations of dynamic nodes
  16. pub node_pathways: &'static [&'static [u8]],
  17. // todo: locations of dynamic nodes
  18. pub attr_pathways: &'static [&'static [u8]],
  19. }
  20. /// A weird-ish variant of VNodes with way more limited types
  21. #[derive(Debug, Clone, Copy)]
  22. pub enum TemplateNode<'a> {
  23. /// A simple element
  24. Element {
  25. tag: &'a str,
  26. namespace: Option<&'a str>,
  27. attrs: &'a [TemplateAttribute<'a>],
  28. children: &'a [TemplateNode<'a>],
  29. },
  30. Text(&'a str),
  31. Dynamic(usize),
  32. DynamicText(usize),
  33. }
  34. pub enum DynamicNode<'a> {
  35. // Anything declared in component form
  36. // IE in caps or with underscores
  37. Component {
  38. name: &'static str,
  39. },
  40. // Comes in with string interpolation or from format_args, include_str, etc
  41. Text {
  42. id: Cell<ElementId>,
  43. value: &'static str,
  44. },
  45. // Anything that's coming in as an iterator
  46. Fragment {
  47. children: &'a [VTemplate<'a>],
  48. },
  49. }
  50. #[derive(Debug)]
  51. pub struct TemplateAttribute<'a> {
  52. pub name: &'static str,
  53. pub value: &'a str,
  54. pub namespace: Option<&'static str>,
  55. pub volatile: bool,
  56. }
  57. pub struct AttributeLocation<'a> {
  58. pub mounted_element: Cell<ElementId>,
  59. pub attrs: &'a [Attribute<'a>],
  60. }
  61. #[derive(Debug)]
  62. pub struct Attribute<'a> {
  63. pub name: &'static str,
  64. pub value: &'a str,
  65. pub namespace: Option<&'static str>,
  66. }
  67. #[test]
  68. fn what_are_the_sizes() {
  69. dbg!(std::mem::size_of::<VTemplate>());
  70. dbg!(std::mem::size_of::<Template>());
  71. dbg!(std::mem::size_of::<TemplateNode>());
  72. }