diff --git a/crates/sauron-core/Cargo.toml b/crates/sauron-core/Cargo.toml index 8b8187e99..317bf3c3f 100644 --- a/crates/sauron-core/Cargo.toml +++ b/crates/sauron-core/Cargo.toml @@ -29,6 +29,7 @@ phf = { version = "0.11.2", features = ["macros"] } futures = "=0.3.30" indexmap = "2.2.5" longest-increasing-subsequence = "0.1.0" +derive-where = "1.2.7" [dependencies.wasm-bindgen] diff --git a/crates/sauron-core/src/vdom/node.rs b/crates/sauron-core/src/vdom/node.rs index 1ce31f47f..b7e63bc32 100644 --- a/crates/sauron-core/src/vdom/node.rs +++ b/crates/sauron-core/src/vdom/node.rs @@ -4,6 +4,7 @@ use std::fmt; use std::fmt::{Debug, Formatter}; pub use element::Element; use crate::vdom::Leaf; +use derive_where::derive_where; pub(crate) mod attribute; mod element; @@ -21,6 +22,7 @@ mod element; /// virtual dom implementation /// AttributeValue - is the type for the value of the attribute, this will be String, f64, or just another /// generics that suits the implementing library which used mt-dom for just dom-diffing purposes +#[derive_where(Clone, Debug, PartialEq, Eq)] pub enum Node { /// Element variant of a virtual node Element(Element), @@ -33,25 +35,6 @@ pub enum Node { Leaf(Leaf), } -impl Clone for Node{ - fn clone(&self) -> Self { - todo!() - } -} - -impl fmt::Debug for Node{ - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - todo!() - } -} - -impl PartialEq for Node{ - fn eq(&self, other: &Self) -> bool { - todo!() - } -} - -impl Eq for Node{ } #[derive(Debug, Copy, Clone)] diff --git a/crates/sauron-core/src/vdom/node/attribute.rs b/crates/sauron-core/src/vdom/node/attribute.rs index 32543a03f..b0f99be9f 100644 --- a/crates/sauron-core/src/vdom/node/attribute.rs +++ b/crates/sauron-core/src/vdom/node/attribute.rs @@ -3,6 +3,7 @@ use std::fmt::Debug; use indexmap::IndexMap; use std::fmt; use crate::vdom::AttributeValue; +use derive_where::derive_where; /// The type of the Namspace pub type Namespace = &'static str; @@ -18,6 +19,7 @@ pub type AttributeName = &'static str; pub static KEY: &AttributeName = &"key"; /// These are the plain attributes of an element +#[derive_where(Clone, Debug, PartialEq, Eq)] pub struct Attribute { /// namespace of an attribute. /// This is specifically used by svg attributes @@ -30,25 +32,6 @@ pub struct Attribute { pub value: Vec>, } -impl Clone for Attribute{ - fn clone(&self) -> Self { - todo!() - } -} - -impl fmt::Debug for Attribute{ - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - todo!() - } -} - -impl PartialEq for Attribute{ - fn eq(&self, other: &Self) -> bool { - todo!() - } -} - -impl Eq for Attribute{ } impl Attribute { /// create a plain attribute with namespace diff --git a/crates/sauron-core/src/vdom/node/element.rs b/crates/sauron-core/src/vdom/node/element.rs index e8f2dae1a..2801a2bb7 100644 --- a/crates/sauron-core/src/vdom/node/element.rs +++ b/crates/sauron-core/src/vdom/node/element.rs @@ -3,6 +3,7 @@ use super::{Attribute, Node}; use std::fmt::Debug; use std::fmt; use crate::vdom::AttributeValue; +use derive_where::derive_where; /// Represents an element of the virtual node /// An element has a generic tag, this tag could be a static str tag, such as usage in html dom. @@ -17,6 +18,7 @@ use crate::vdom::AttributeValue; /// /// The namespace is also needed in attributes where namespace are necessary such as `xlink:href` /// where the namespace `xlink` is needed in order for the linked element in an svg image to work. +#[derive_where(Clone, Debug, PartialEq, Eq)] pub struct Element { /// namespace of this element, /// svg elements requires namespace to render correcly in the browser @@ -31,25 +33,6 @@ pub struct Element { pub self_closing: bool, } -impl Clone for Element{ - fn clone(&self) -> Self { - todo!() - } -} - -impl fmt::Debug for Element{ - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - todo!() - } -} - -impl PartialEq for Element{ - fn eq(&self, other: &Self) -> bool { - todo!() - } -} - -impl Eq for Element{ } impl Element { /// create a new instance of an element diff --git a/crates/sauron-core/src/vdom/patch.rs b/crates/sauron-core/src/vdom/patch.rs index ab3b96d24..ea65791e4 100644 --- a/crates/sauron-core/src/vdom/patch.rs +++ b/crates/sauron-core/src/vdom/patch.rs @@ -4,6 +4,7 @@ use super::Tag; use super::{Attribute, Node}; use std::fmt::Debug; use std::fmt; +use derive_where::derive_where; pub use tree_path::TreePath; @@ -59,6 +60,7 @@ mod tree_path; /// 0 - is the root element which is always zero. /// 1 - is the `footer` element since it is the 2nd element of the body. /// 2 - is the `nav` element since it is the 3rd node in the `footer` element. +#[derive_where(Clone, Debug, PartialEq, Eq)] pub struct Patch<'a, MSG> { /// the tag of the node at patch_path pub tag: Option<&'a Tag>, @@ -68,6 +70,7 @@ pub struct Patch<'a, MSG> { pub patch_type: PatchType<'a, MSG>, } +/* impl<'a, MSG> Clone for Patch<'a, MSG>{ fn clone(&self) -> Self { todo!() @@ -87,8 +90,10 @@ impl<'a, MSG> PartialEq for Patch<'a, MSG>{ } impl<'a, MSG> Eq for Patch<'a, MSG>{ } +*/ /// the patch variant +#[derive_where(Clone, Debug, PartialEq, Eq)] pub enum PatchType<'a, MSG> { /// insert the nodes before the node at patch_path InsertBeforeNode { @@ -142,25 +147,6 @@ pub enum PatchType<'a, MSG> { }, } -impl<'a, MSG> Clone for PatchType<'a, MSG>{ - fn clone(&self) -> Self { - todo!() - } -} - -impl<'a, MSG> fmt::Debug for PatchType<'a, MSG>{ - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - todo!() - } -} - -impl<'a, MSG> PartialEq for PatchType<'a, MSG>{ - fn eq(&self, other: &Self) -> bool { - todo!() - } -} - -impl<'a, MSG> Eq for PatchType<'a, MSG>{ } impl<'a, MSG> Patch<'a, MSG> { /// return the path to traverse for this patch to get to the target Node diff --git a/crates/sauron-core/src/vdom/patch/tree_path.rs b/crates/sauron-core/src/vdom/patch/tree_path.rs index b24d20a35..87ef06f63 100644 --- a/crates/sauron-core/src/vdom/patch/tree_path.rs +++ b/crates/sauron-core/src/vdom/patch/tree_path.rs @@ -161,9 +161,8 @@ fn traverse_node_by_path<'a, MSG>( mod tests { use super::*; use crate::*; - use alloc::format; - use alloc::string::String; - use alloc::string::ToString; + use crate::vdom::*; + use crate::render::Render; #[test] fn test_traverse() { @@ -172,8 +171,8 @@ mod tests { assert_eq!(path.traverse(1), TreePath::from([0, 1])); } - fn sample_node() -> Node { - let node: Node = element( + fn sample_node() -> Node<()> { + let node: Node<()> = element( "div", vec![attr("class", "[]"), attr("id", "0")], vec![ @@ -221,14 +220,14 @@ mod tests { // index is the index of this code with respect to it's sibling fn assert_traverse_match( - node: &Node, + node: &Node<()>, node_idx: &mut usize, path: Vec, ) { let id = node.attribute_value(&"id").unwrap()[0]; let class = node.attribute_value(&"class").unwrap()[0]; - assert_eq!(id.to_string(), node_idx.to_string()); - assert_eq!(class.to_string(), format_vec(&path)); + assert_eq!(id.as_str(), Some(node_idx.to_string()).as_deref()); + assert_eq!(class.as_str(), Some(format_vec(&path)).as_deref()); for (i, child) in node.children().iter().enumerate() { *node_idx += 1; let mut child_path = path.clone(); @@ -237,11 +236,11 @@ mod tests { } } - fn traverse_tree_path(node: &Node, path: &TreePath, node_idx: &mut usize) { + fn traverse_tree_path(node: &Node<()>, path: &TreePath, node_idx: &mut usize) { let id = node.attribute_value(&"id").unwrap()[0]; let class = node.attribute_value(&"class").unwrap()[0]; - assert_eq!(id.to_string(), node_idx.to_string()); - assert_eq!(class.to_string(), format_vec(&path.path)); + assert_eq!(id.as_str(), Some(node_idx.to_string()).as_deref()); + assert_eq!(class.as_str(), Some(format_vec(&path.path)).as_deref()); for (i, child) in node.children().iter().enumerate() { *node_idx += 1; let mut child_path = path.clone(); diff --git a/tests/diff_with_events.rs b/tests/diff_with_events.rs index 8123f48a4..5946eabb8 100644 --- a/tests/diff_with_events.rs +++ b/tests/diff_with_events.rs @@ -2,7 +2,7 @@ #[macro_use] extern crate log; -use crate::mt_dom::TreePath; +use crate::vdom::TreePath; use sauron::{html::attributes::*, html::events::*, html::*, *}; use test_fixtures::simple_program; diff --git a/tests/event_tests.rs b/tests/event_tests.rs index 527f39cde..d7685c8a4 100644 --- a/tests/event_tests.rs +++ b/tests/event_tests.rs @@ -1,5 +1,5 @@ #![deny(warnings)] -use sauron::{html::attributes::*, html::events::*, html::*, mt_dom::TreePath, *}; +use sauron::{html::attributes::*, html::events::*, html::*, vdom::TreePath, *}; use std::{cell::RefCell, rc::Rc}; use test_fixtures::simple_program; use wasm_bindgen_test::*; diff --git a/tests/html_test.rs b/tests/html_test.rs index 91a26751f..70fa55b5f 100644 --- a/tests/html_test.rs +++ b/tests/html_test.rs @@ -1,5 +1,5 @@ #![deny(warnings)] -use crate::mt_dom::TreePath; +use crate::vdom::TreePath; use sauron::{ dom::Event, events::on, diff --git a/tests/node_event_recycling.rs b/tests/node_event_recycling.rs index 5ccbb8f02..f99abdbdc 100644 --- a/tests/node_event_recycling.rs +++ b/tests/node_event_recycling.rs @@ -1,6 +1,6 @@ #![deny(warnings)] use sauron::{html::attributes::*, html::events::*, html::*}; -use sauron::{mt_dom::TreePath, *}; +use sauron::{vdom::TreePath, *}; use std::{cell::RefCell, rc::Rc}; use test_fixtures::simple_program; use wasm_bindgen_test::*; diff --git a/tests/right_next_text_siblings.rs b/tests/right_next_text_siblings.rs index 22411ff52..f05b112d6 100644 --- a/tests/right_next_text_siblings.rs +++ b/tests/right_next_text_siblings.rs @@ -1,5 +1,5 @@ #![deny(warnings)] -use crate::mt_dom::TreePath; +use crate::vdom::TreePath; use sauron::html::*; use sauron::*; diff --git a/tests/test_render.rs b/tests/test_render.rs index 5f59ffb31..b4798a0f4 100644 --- a/tests/test_render.rs +++ b/tests/test_render.rs @@ -1,5 +1,5 @@ #![deny(warnings)] -use crate::mt_dom::TreePath; +use crate::vdom::TreePath; use sauron::{ html::{attributes::*, *}, *,