From 087afdbd9b439a5fd37965b71df99038b28b7872 Mon Sep 17 00:00:00 2001 From: Phil Schaf Date: Sun, 15 Nov 2015 00:29:26 +0100 Subject: added some constructors --- src/document_tree/element_categories.rs | 1 + src/document_tree/elements.rs | 17 +++++++++++++++++ src/document_tree/extra_attributes.rs | 1 + src/document_tree/mod.rs | 20 ++++++++++++++------ 4 files changed, 33 insertions(+), 6 deletions(-) (limited to 'src') diff --git a/src/document_tree/element_categories.rs b/src/document_tree/element_categories.rs index e9d07a6..85ac340 100644 --- a/src/document_tree/element_categories.rs +++ b/src/document_tree/element_categories.rs @@ -3,6 +3,7 @@ use std::fmt::{self,Debug,Formatter}; use super::elements::*; pub trait HasChildren { + fn with_children(Vec) -> Self; fn children(& self) -> &Vec; fn children_mut(&mut self) -> &mut Vec; fn append_child>(&mut self, child: R) { diff --git a/src/document_tree/elements.rs b/src/document_tree/elements.rs index 024a25a..4928cee 100644 --- a/src/document_tree/elements.rs +++ b/src/document_tree/elements.rs @@ -47,6 +47,7 @@ macro_rules! impl_element(($name:ident) => { macro_rules! impl_children(($name:ident, $childtype:ident) => { impl HasChildren<$childtype> for $name { + fn with_children(children: Vec<$childtype>) -> $name { $name { children: children, ..Default::default() } } fn children (& self) -> & Vec<$childtype> { & self.children } fn children_mut(&mut self) -> &mut Vec<$childtype> { &mut self.children } } @@ -54,6 +55,7 @@ macro_rules! impl_children(($name:ident, $childtype:ident) => { macro_rules! impl_extra(($name:ident) => { impl ExtraAttributes for $name { +// fn with_extra(extra: extra_attributes::$name) -> $name { $name { extra: extra, ..Default::default() } } fn extra (& self) -> & extra_attributes::$name { & self.extra } fn extra_mut(&mut self) -> &mut extra_attributes::$name { &mut self.extra } } @@ -63,26 +65,41 @@ macro_rules! impl_elem( ($name:ident) => { #[derive(Default,Debug)] pub struct $name { common: CommonAttributes } + impl $name { + pub fn new(common: CommonAttributes) -> $name { $name { common: common } } + } impl_element!($name); }; ($name:ident; +) => { #[derive(Default,Debug)] pub struct $name { common: CommonAttributes, extra: extra_attributes::$name } + impl $name { + pub fn new(common: CommonAttributes, extra: extra_attributes::$name ) -> $name { $name { common: common, extra: extra } } + } impl_element!($name); impl_extra!($name); }; ($name:ident; *) => { //same as above with no default #[derive(Debug)] pub struct $name { common: CommonAttributes, extra: extra_attributes::$name } + impl $name { + pub fn new(common: CommonAttributes, extra: extra_attributes::$name ) -> $name { $name { common: common, extra: extra } } + } impl_element!($name); impl_extra!($name); }; ($name:ident, $childtype:ident) => { #[derive(Default,Debug)] pub struct $name { common: CommonAttributes, children: Vec<$childtype> } + impl $name { + pub fn new(common: CommonAttributes, children: Vec<$childtype> ) -> $name { $name { common: common, children: children } } + } impl_element!($name); impl_children!($name, $childtype); }; ($name:ident, $childtype:ident; +) => { #[derive(Default,Debug)] pub struct $name { common: CommonAttributes, extra: extra_attributes::$name, children: Vec<$childtype> } + impl $name { + pub fn new(common: CommonAttributes, extra: extra_attributes::$name, children: Vec<$childtype> ) -> $name { $name { common: common, extra: extra, children: children } } + } impl_element!($name); impl_extra!($name); impl_children!($name, $childtype); }; ); diff --git a/src/document_tree/extra_attributes.rs b/src/document_tree/extra_attributes.rs index 16e261f..d73013e 100644 --- a/src/document_tree/extra_attributes.rs +++ b/src/document_tree/extra_attributes.rs @@ -3,6 +3,7 @@ use url::Url; use super::attribute_types::{FixedSpace,ID,NameToken,AlignHV,AlignH,Measure,EnumeratedListType}; pub trait ExtraAttributes { +// fn with_extra(extra: A) -> Self; fn extra (& self) -> & A; fn extra_mut(&mut self) -> &mut A; } diff --git a/src/document_tree/mod.rs b/src/document_tree/mod.rs index a9c4760..9bb4678 100644 --- a/src/document_tree/mod.rs +++ b/src/document_tree/mod.rs @@ -11,14 +11,22 @@ pub use self::extra_attributes::ExtraAttributes; pub use self::element_categories::HasChildren; #[test] -fn test() { - use document_tree as dt; - use document_tree::HasChildren; - - let mut doc = dt::Document::default(); - let mut title = dt::Title::default(); +fn test_imperative() { + let mut doc = Document::default(); + let mut title = Title::default(); title.append_child("Hi"); doc.append_child(title); println!("{:?}", doc); } + +#[test] +fn test_descriptive() { + let doc = Document::with_children(vec![ + Title::with_children(vec![ + "Hi".into() + ]).into() + ]); + + println!("{:?}", doc); +} -- cgit v1.2.3