use satteri_ast::mdast::MdastNodeType;
#[derive(Debug, Clone)]
pub enum Command {
Replace { node_id: u32, new_node: NewNode },
Remove { node_id: u32 },
InsertBefore { node_id: u32, new_node: NewNode },
InsertAfter { node_id: u32, new_node: NewNode },
Wrap { node_id: u32, parent_node: NewNode },
PrependChild { node_id: u32, child_node: NewNode },
AppendChild { node_id: u32, child_node: NewNode },
SetData {
node_id: u32,
key: String,
value: crate::data::DataValue,
},
}
#[derive(Debug, Clone)]
pub enum NewNode {
Raw(String),
Built(BuiltNode),
}
#[derive(Debug, Clone)]
pub struct BuiltNode {
pub node_type: MdastNodeType,
pub children: Vec<NewNode>,
pub data_bytes: Vec<u8>,
pub position: Option<crate::typed_nodes::NodePosition>,
}
pub struct NodeBuilder {
node_type: MdastNodeType,
children: Vec<NewNode>,
data_bytes: Vec<u8>,
}
impl NodeBuilder {
pub fn new(node_type: MdastNodeType) -> Self {
Self {
node_type,
children: Vec::new(),
data_bytes: Vec::new(),
}
}
pub fn child(mut self, child: NewNode) -> Self {
self.children.push(child);
self
}
pub fn children(mut self, children: impl IntoIterator<Item = NewNode>) -> Self {
self.children.extend(children);
self
}
pub fn data_bytes(mut self, bytes: Vec<u8>) -> Self {
self.data_bytes = bytes;
self
}
pub fn build(self) -> NewNode {
NewNode::Built(BuiltNode {
node_type: self.node_type,
children: self.children,
data_bytes: self.data_bytes,
position: None,
})
}
}
impl NodeBuilder {
pub fn heading(depth: u8) -> Self {
use satteri_ast::mdast::codec::encode_heading_data;
Self::new(MdastNodeType::Heading).data_bytes(encode_heading_data(depth))
}
pub fn paragraph() -> Self {
Self::new(MdastNodeType::Paragraph)
}
pub fn text(value_offset: u32, value_len: u32) -> Self {
use satteri_arena::{encode_string_ref_data, StringRef};
let string_ref = StringRef {
offset: value_offset,
len: value_len,
};
Self::new(MdastNodeType::Text).data_bytes(encode_string_ref_data(string_ref))
}
pub fn raw(markdown: impl Into<String>) -> NewNode {
NewNode::Raw(markdown.into())
}
}