Skip to content

Commit

Permalink
fix: use derive_where to derive Clone, PartialEq, Debug, Eq for struc…
Browse files Browse the repository at this point in the history
…ts that has Generic types that is not bounded by the those traits
  • Loading branch information
ivanceras committed Mar 4, 2024
1 parent 707613a commit c1b9ad1
Show file tree
Hide file tree
Showing 12 changed files with 28 additions and 93 deletions.
1 change: 1 addition & 0 deletions crates/sauron-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
21 changes: 2 additions & 19 deletions crates/sauron-core/src/vdom/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<MSG> {
/// Element variant of a virtual node
Element(Element<MSG>),
Expand All @@ -33,25 +35,6 @@ pub enum Node<MSG> {
Leaf(Leaf),
}

impl<MSG> Clone for Node<MSG>{
fn clone(&self) -> Self {
todo!()
}
}

impl<MSG> fmt::Debug for Node<MSG>{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
todo!()
}
}

impl<MSG> PartialEq for Node<MSG>{
fn eq(&self, other: &Self) -> bool {
todo!()
}
}

impl<MSG> Eq for Node<MSG>{ }


#[derive(Debug, Copy, Clone)]
Expand Down
21 changes: 2 additions & 19 deletions crates/sauron-core/src/vdom/node/attribute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<MSG> {
/// namespace of an attribute.
/// This is specifically used by svg attributes
Expand All @@ -30,25 +32,6 @@ pub struct Attribute<MSG> {
pub value: Vec<AttributeValue<MSG>>,
}

impl<MSG> Clone for Attribute<MSG>{
fn clone(&self) -> Self {
todo!()
}
}

impl<MSG> fmt::Debug for Attribute<MSG>{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
todo!()
}
}

impl<MSG> PartialEq for Attribute<MSG>{
fn eq(&self, other: &Self) -> bool {
todo!()
}
}

impl<MSG> Eq for Attribute<MSG>{ }

impl<MSG> Attribute<MSG> {
/// create a plain attribute with namespace
Expand Down
21 changes: 2 additions & 19 deletions crates/sauron-core/src/vdom/node/element.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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<MSG> {
/// namespace of this element,
/// svg elements requires namespace to render correcly in the browser
Expand All @@ -31,25 +33,6 @@ pub struct Element<MSG> {
pub self_closing: bool,
}

impl<MSG> Clone for Element<MSG>{
fn clone(&self) -> Self {
todo!()
}
}

impl<MSG> fmt::Debug for Element<MSG>{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
todo!()
}
}

impl<MSG> PartialEq for Element<MSG>{
fn eq(&self, other: &Self) -> bool {
todo!()
}
}

impl<MSG> Eq for Element<MSG>{ }

impl<MSG> Element<MSG> {
/// create a new instance of an element
Expand Down
24 changes: 5 additions & 19 deletions crates/sauron-core/src/vdom/patch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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>,
Expand All @@ -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!()
Expand All @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
21 changes: 10 additions & 11 deletions crates/sauron-core/src/vdom/patch/tree_path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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![
Expand Down Expand Up @@ -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<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));
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();
Expand All @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion tests/diff_with_events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion tests/event_tests.rs
Original file line number Diff line number Diff line change
@@ -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::*;
Expand Down
2 changes: 1 addition & 1 deletion tests/html_test.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#![deny(warnings)]
use crate::mt_dom::TreePath;
use crate::vdom::TreePath;
use sauron::{
dom::Event,
events::on,
Expand Down
2 changes: 1 addition & 1 deletion tests/node_event_recycling.rs
Original file line number Diff line number Diff line change
@@ -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::*;
Expand Down
2 changes: 1 addition & 1 deletion tests/right_next_text_siblings.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#![deny(warnings)]
use crate::mt_dom::TreePath;
use crate::vdom::TreePath;
use sauron::html::*;
use sauron::*;

Expand Down
2 changes: 1 addition & 1 deletion tests/test_render.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#![deny(warnings)]
use crate::mt_dom::TreePath;
use crate::vdom::TreePath;
use sauron::{
html::{attributes::*, *},
*,
Expand Down

0 comments on commit c1b9ad1

Please sign in to comment.