use super::*;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ElementBuilder {
element: Element,
}
impl ElementBuilder {
pub fn new<S>(name: S) -> ElementBuilder
where
S: ToString,
{
ElementBuilder {
element: Element::new(name),
}
}
pub fn prefix<S>(&mut self, prefix: S) -> &mut ElementBuilder
where
S: ToString,
{
self.element.prefix = Some(prefix.to_string());
self
}
pub fn attr<K, V>(&mut self, key: K, value: V) -> &mut ElementBuilder
where
K: ToString,
V: ToString,
{
self.element
.attributes
.insert(key.to_string(), value.to_string());
self
}
pub fn text<S>(&mut self, text: S) -> &mut ElementBuilder
where
S: ToString,
{
self.element.text = Some(text.to_string());
self
}
pub fn cdata<S>(&mut self, cdata: S) -> &mut ElementBuilder
where
S: ToString,
{
self.element.cdata = Some(cdata.to_string());
self
}
pub fn children(&mut self, children: Vec<&mut ElementBuilder>) -> &mut ElementBuilder {
self.element
.children
.append(&mut children.into_iter().map(|i| i.element()).collect());
self
}
pub fn element(&self) -> Element {
self.element.clone()
}
}
impl From<ElementBuilder> for Element {
fn from(value: ElementBuilder) -> Element {
value.element()
}
}
impl From<Element> for ElementBuilder {
fn from(element: Element) -> ElementBuilder {
ElementBuilder { element }
}
}