diff options
Diffstat (limited to 'src/document_tree')
| -rw-r--r-- | src/document_tree/element_categories.rs | 1 | ||||
| -rw-r--r-- | src/document_tree/elements.rs | 17 | ||||
| -rw-r--r-- | src/document_tree/extra_attributes.rs | 1 | ||||
| -rw-r--r-- | src/document_tree/mod.rs | 20 | 
4 files changed, 33 insertions, 6 deletions
| 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<C> { +	fn with_children(Vec<C>) -> Self;  	fn children(& self) -> &Vec<C>;  	fn children_mut(&mut self) -> &mut Vec<C>;  	fn append_child<R: Into<C>>(&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<extra_attributes::$name> 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<A> { +//	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); +} | 
