use crate::node::{Mark, Node};
use serde_json::{Map, Value};
impl Node {
#[inline]
pub fn element(node_type: impl Into<String>) -> Node {
Node {
node_type: Some(node_type.into()),
..Default::default()
}
}
#[inline]
pub fn text(text: impl Into<String>) -> Node {
Node {
node_type: Some("text".into()),
text: Some(text.into()),
..Default::default()
}
}
pub fn text_with_marks(text: impl Into<String>, marks: impl IntoIterator<Item = Mark>) -> Node {
Node {
node_type: Some("text".into()),
text: Some(text.into()),
marks: Some(marks.into_iter().collect()),
..Default::default()
}
}
pub fn with_attr(mut self, key: impl Into<String>, value: impl Into<Value>) -> Self {
self.attrs
.get_or_insert_with(Map::new)
.insert(key.into(), value.into());
self
}
pub fn with_child(mut self, node: Node) -> Self {
self.push_child(node);
self
}
pub fn with_children(mut self, nodes: impl IntoIterator<Item = Node>) -> Self {
self.children_mut().extend(nodes);
self
}
pub fn with_text(self, text: impl Into<String>) -> Self {
self.with_child(Node::text(text))
}
pub fn with_mark(mut self, mark: Mark) -> Self {
self.marks.get_or_insert_with(Vec::new).push(mark);
self
}
}
pub fn doc(children: impl IntoIterator<Item = Node>) -> Node {
Node {
node_type: Some("doc".into()),
content: Some(children.into_iter().collect()),
..Default::default()
}
}