2022-11-02 01:42:29 +00:00
|
|
|
use crate::{any_props::AnyProps, arena::ElementId};
|
2022-11-03 00:29:18 +00:00
|
|
|
use std::{
|
|
|
|
any::Any,
|
|
|
|
cell::{Cell, RefCell},
|
|
|
|
hash::Hasher,
|
|
|
|
};
|
2022-10-28 04:58:47 +00:00
|
|
|
|
|
|
|
pub type TemplateId = &'static str;
|
|
|
|
|
|
|
|
/// A reference to a template along with any context needed to hydrate it
|
2022-11-02 01:42:29 +00:00
|
|
|
pub struct VNode<'a> {
|
2022-10-28 04:58:47 +00:00
|
|
|
// The ID assigned for the root of this template
|
|
|
|
pub node_id: Cell<ElementId>,
|
|
|
|
|
2022-11-03 00:29:18 +00:00
|
|
|
// When rendered, this template will be linked to its parent manually
|
2022-11-02 01:42:29 +00:00
|
|
|
pub parent: Option<(*mut VNode<'static>, usize)>,
|
|
|
|
|
2022-11-02 08:00:37 +00:00
|
|
|
pub template: Template<'static>,
|
2022-10-28 04:58:47 +00:00
|
|
|
|
|
|
|
pub root_ids: &'a [Cell<ElementId>],
|
|
|
|
|
2022-11-03 00:29:18 +00:00
|
|
|
pub dynamic_nodes: &'a [DynamicNode<'a>],
|
2022-10-28 04:58:47 +00:00
|
|
|
|
2022-11-03 00:29:18 +00:00
|
|
|
pub dynamic_attrs: &'a [Attribute<'a>],
|
2022-10-28 04:58:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, Copy)]
|
2022-11-02 08:00:37 +00:00
|
|
|
pub struct Template<'a> {
|
|
|
|
pub id: &'a str,
|
|
|
|
pub roots: &'a [TemplateNode<'a>],
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> std::hash::Hash for Template<'a> {
|
|
|
|
fn hash<H: Hasher>(&self, state: &mut H) {
|
|
|
|
self.id.hash(state);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl PartialEq for Template<'_> {
|
|
|
|
fn eq(&self, other: &Self) -> bool {
|
|
|
|
self.id == other.id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Eq for Template<'_> {}
|
|
|
|
impl PartialOrd for Template<'_> {
|
|
|
|
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
|
|
|
|
self.id.partial_cmp(other.id)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
impl Ord for Template<'_> {
|
|
|
|
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
|
|
|
|
self.id.cmp(other.id)
|
|
|
|
}
|
2022-10-28 04:58:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// A weird-ish variant of VNodes with way more limited types
|
|
|
|
#[derive(Debug, Clone, Copy)]
|
|
|
|
pub enum TemplateNode<'a> {
|
|
|
|
Element {
|
|
|
|
tag: &'a str,
|
|
|
|
namespace: Option<&'a str>,
|
|
|
|
attrs: &'a [TemplateAttribute<'a>],
|
|
|
|
children: &'a [TemplateNode<'a>],
|
|
|
|
},
|
|
|
|
Text(&'a str),
|
|
|
|
Dynamic(usize),
|
|
|
|
DynamicText(usize),
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct DynamicNode<'a> {
|
|
|
|
pub path: &'static [u8],
|
|
|
|
pub kind: DynamicNodeKind<'a>,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub enum DynamicNodeKind<'a> {
|
|
|
|
// Anything declared in component form
|
|
|
|
// IE in caps or with underscores
|
|
|
|
Component {
|
|
|
|
name: &'static str,
|
2022-11-03 00:29:18 +00:00
|
|
|
can_memoize: bool,
|
2022-11-02 01:42:29 +00:00
|
|
|
props: *mut dyn AnyProps,
|
2022-10-28 04:58:47 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
// Comes in with string interpolation or from format_args, include_str, etc
|
|
|
|
Text {
|
|
|
|
id: Cell<ElementId>,
|
2022-11-02 08:00:37 +00:00
|
|
|
value: &'a str,
|
2022-10-28 04:58:47 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
// Anything that's coming in as an iterator
|
|
|
|
Fragment {
|
2022-11-02 01:42:29 +00:00
|
|
|
children: &'a [VNode<'a>],
|
2022-10-28 04:58:47 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
2022-11-02 08:00:37 +00:00
|
|
|
pub enum TemplateAttribute<'a> {
|
|
|
|
Static {
|
|
|
|
name: &'static str,
|
|
|
|
value: &'a str,
|
|
|
|
namespace: Option<&'static str>,
|
|
|
|
volatile: bool,
|
|
|
|
},
|
|
|
|
Dynamic {
|
|
|
|
name: &'static str,
|
|
|
|
index: usize,
|
|
|
|
},
|
2022-10-28 04:58:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Attribute<'a> {
|
2022-11-03 00:29:18 +00:00
|
|
|
pub name: &'a str,
|
|
|
|
pub value: AttributeValue<'a>,
|
2022-10-28 04:58:47 +00:00
|
|
|
pub namespace: Option<&'static str>,
|
2022-11-03 00:29:18 +00:00
|
|
|
pub mounted_element: Cell<ElementId>,
|
|
|
|
pub path: &'static [u8],
|
2022-10-28 04:58:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub enum AttributeValue<'a> {
|
|
|
|
Text(&'a str),
|
|
|
|
Float(f32),
|
|
|
|
Int(i32),
|
|
|
|
Bool(bool),
|
2022-11-03 00:29:18 +00:00
|
|
|
Listener(RefCell<&'a mut dyn FnMut(&dyn Any)>),
|
2022-10-28 04:58:47 +00:00
|
|
|
Any(&'a dyn AnyValue),
|
2022-11-03 00:29:18 +00:00
|
|
|
None,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> AttributeValue<'a> {
|
|
|
|
fn is_listener(&self) -> bool {
|
|
|
|
matches!(self, AttributeValue::Listener(_))
|
|
|
|
}
|
2022-10-28 04:58:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub trait AnyValue {
|
|
|
|
fn any_cmp(&self, other: &dyn Any) -> bool;
|
|
|
|
}
|
|
|
|
impl<T> AnyValue for T
|
|
|
|
where
|
|
|
|
T: PartialEq + Any,
|
|
|
|
{
|
|
|
|
fn any_cmp(&self, other: &dyn Any) -> bool {
|
|
|
|
if self.type_id() != other.type_id() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
self == unsafe { &*(other as *const _ as *const T) }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn what_are_the_sizes() {
|
2022-11-02 01:42:29 +00:00
|
|
|
dbg!(std::mem::size_of::<VNode>());
|
2022-10-28 04:58:47 +00:00
|
|
|
dbg!(std::mem::size_of::<Template>());
|
|
|
|
dbg!(std::mem::size_of::<TemplateNode>());
|
|
|
|
}
|