Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make dyn Node more usable. #83

Merged
merged 3 commits into from
Sep 27, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/node/blob.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ impl fmt::Display for Blob {
}
}

impl Node for Blob {}
impl Node for Blob {
fn get_name(&self) -> &str {
mobiusklein marked this conversation as resolved.
Show resolved Hide resolved
"blob"
}
}

impl super::NodeDefaultHash for Blob {
#[inline]
Expand Down
4 changes: 4 additions & 0 deletions src/node/comment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ impl Node for Comment {
U: Into<Value>,
{
}

fn get_name(&self) -> &str {
"comment"
}
}

impl super::NodeDefaultHash for Comment {
Expand Down
47 changes: 47 additions & 0 deletions src/node/element/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,26 @@ impl Node for Element {
{
self.attributes.insert(name.into(), value.into());
}

fn get_attribute(&self, k: &str) -> Option<&Value> {
self.attributes.get(k)
}

fn set_attribute(&mut self, name: String, value: Value) {
self.attributes.insert(name, value);
}

fn get_name(&self) -> &str {
&self.name
}

fn iter_children(&self) -> super::ChildrenIter {
self.children.iter()
}

fn iter_children_mut(&mut self) -> super::ChildrenIterMut {
self.children.iter_mut()
}
}

macro_rules! implement {
Expand Down Expand Up @@ -410,6 +430,8 @@ fn escape(value: &str) -> String {

#[cfg(test)]
mod tests {
use std::ops::Deref;

use super::{Element, Rectangle, Style, Title};
use crate::node::{element, Node};

Expand Down Expand Up @@ -494,4 +516,29 @@ mod tests {
&["<style>", "* { font-family: foo; }", "</style>"],
);
}

#[test]
fn node_traversal() {
let group = element::Group::new().add(
element::Text::new("foo").set("font-family", "Times New Roman")
).add(
element::Text::new("bar").set("font-family", "Arial, Helvetica, sans-serif")
).add(
element::Text::new("baz").set("font-family", "Comic Sans")
);

for (i, child) in group.iter_children().enumerate() {
assert_eq!(child.get_name(), "text");
if i == 0 {
assert_eq!(child.get_attribute("font-family").unwrap().deref(), "Times New Roman");
assert_eq!(child.iter_children().next().unwrap().to_string(), "foo");
} else if i == 1 {
assert_eq!(child.get_attribute("font-family").unwrap().deref(), "Arial, Helvetica, sans-serif");
assert_eq!(child.iter_children().next().unwrap().to_string(), "bar");
} else if i == 2 {
assert_eq!(child.get_attribute("font-family").unwrap().deref(), "Comic Sans");
assert_eq!(child.iter_children().next().unwrap().to_string(), "baz");
}
}
}
}
60 changes: 60 additions & 0 deletions src/node/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ pub type Attributes = HashMap<String, Value>;
/// Child nodes.
pub type Children = Vec<Box<dyn Node>>;

pub type ChildrenIter<'a> = std::slice::Iter<'a, Box<dyn Node>>;
pub type ChildrenIterMut<'a> = std::slice::IterMut<'a, Box<dyn Node>>;

const FAKE_CHILDREN: &[Box<dyn Node>] = &[];
static mut FAKE_CHILDREN_MUT: &mut [Box<dyn Node>] = &mut [];

/// A node.
pub trait Node:
'static + fmt::Debug + fmt::Display + NodeClone + NodeDefaultHash + Send + Sync
Expand All @@ -43,6 +49,37 @@ pub trait Node:
{
}

#[allow(unused_variables)]
/// Get a reference to the [`Value`] associated with attribute name `k`,
/// if it is present.
fn get_attribute(&self, k: &str) -> Option<&Value>
{
None
}

#[allow(unused)]
/// Assign an attribute with a value explicitly.
///
/// Ergonomically, [`Node::assign`] is easier to use
/// but is not dispatch-safe, while this method is.
fn set_attribute(&mut self, name: String, value: Value) {}

/// Get the name of the node type.
fn get_name(&self) -> &str;
mobiusklein marked this conversation as resolved.
Show resolved Hide resolved

/// Iterate over references to the children of this [`Node`].
fn iter_children(&self) -> ChildrenIter {
FAKE_CHILDREN.iter()
}

/// Iterate over mutable references to the children of this [`Node`]
fn iter_children_mut(&mut self) -> ChildrenIterMut {
// The default implementation uses a global shared mutable slice
// which requires using unsafe. Since this slice is private and is never
// filled with anything, this is reasonable to do.
unsafe { FAKE_CHILDREN_MUT.iter_mut() }
}

#[doc(hidden)]
fn is_bare(&self) -> bool {
false
Expand Down Expand Up @@ -102,6 +139,7 @@ macro_rules! node(
($struct_name:ident::$field_name:ident) => (
node!($struct_name::$field_name []);
);
// ($struct_name:ident::$field_name:ident [$($indicator_name:ident),*])
($struct_name:ident::$field_name:ident [$($indicator_name:ident),*]) => (
impl $struct_name {
/// Append a node.
Expand Down Expand Up @@ -134,6 +172,22 @@ macro_rules! node(
self.$field_name.append(node);
}

fn get_attribute(&self, k: &str) -> Option<&Value> {
self.$field_name.get_attribute(k)
}

fn get_name(&self) -> &str {
self.$field_name.get_name()
}

fn iter_children(&self) -> crate::node::ChildrenIter {
self.$field_name.iter_children()
}

fn iter_children_mut(&mut self) -> crate::node::ChildrenIterMut {
self.$field_name.iter_children_mut()
}

#[inline]
fn assign<T, U>(&mut self, name: T, value: U)
where
Expand All @@ -143,6 +197,12 @@ macro_rules! node(
self.$field_name.assign(name, value);
}

#[inline]
fn set_attribute(&mut self, name: String, value: Value)
{
self.$field_name.set_attribute(name, value);
}

$(
#[inline]
fn $indicator_name(&self) -> bool {
Expand Down
4 changes: 4 additions & 0 deletions src/node/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ impl Node for Text {
fn is_bare(&self) -> bool {
true
}

fn get_name(&self) -> &str {
"text-node"
}
}

impl super::NodeDefaultHash for Text {
Expand Down
8 changes: 7 additions & 1 deletion src/node/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,15 @@ use std::fmt;
use std::ops::Deref;

/// A value of an attribute.
#[derive(Clone, Debug)]
#[derive(Clone, Debug, PartialEq)]
pub struct Value(String);

impl PartialEq<&str> for Value {
fn eq(&self, other: &&str) -> bool {
self.0 == *other
}
}

impl Deref for Value {
type Target = str;

Expand Down