diff --git a/src/element.rs b/src/element.rs index 38c6068..c5a518f 100644 --- a/src/element.rs +++ b/src/element.rs @@ -100,8 +100,56 @@ impl Element { self.children.push(child.into()); self } + + pub fn children(mut self, children: impl AddChildren) -> Self { + children.add_children(&mut self.children); + self + } } +pub trait AddChildren { + fn add_children(self, children: &mut Vec); +} + +impl> AddChildren for T { + fn add_children(self, children: &mut Vec) { + children.push(self.into()); + } +} + +impl AddChildren for Vec { + fn add_children(self, children: &mut Vec) { + children.extend(self); + } +} + +impl AddChildren for [Content; L] { + fn add_children(self, children: &mut Vec) { + children.extend(self); + } +} + +macro_rules! add_children_tuple { + ( $( $t:ident ),* ) => { + impl <$( $t: AddChildren ),*> AddChildren for ($( $t ),*) { + fn add_children(self, children: &mut Vec) { + #[allow(non_snake_case)] + let ($( $t ),*) = self; + $( $t.add_children(children); )* + } + } + }; +} + +add_children_tuple!(C1, C2); +add_children_tuple!(C1, C2, C3); +add_children_tuple!(C1, C2, C3, C4); +add_children_tuple!(C1, C2, C3, C4, C5); +add_children_tuple!(C1, C2, C3, C4, C5, C6); +add_children_tuple!(C1, C2, C3, C4, C5, C6, C7); +add_children_tuple!(C1, C2, C3, C4, C5, C6, C7, C8); +add_children_tuple!(C1, C2, C3, C4, C5, C6, C7, C8, C9); + /// An HTML document. /// /// A `Document(el)` is basically the same as `[Content::doctype(), el.into()]`