diff --git a/src/nodes.rs b/src/nodes.rs index f429fb66..eb23dc4d 100644 --- a/src/nodes.rs +++ b/src/nodes.rs @@ -380,8 +380,22 @@ impl Ast { /// It is bound by the lifetime `'a`, which corresponds to the `Arena` nodes are allocated in. /// `AstNode`s are almost handled as a reference itself bound by `'a`. Child `Ast`s are wrapped in /// `RefCell` for interior mutability. +/// +/// You can construct a new `AstNode` from a `NodeValue` using the `From` trait: +/// +/// ```no_run +/// # use comrak::nodes::{AstNode, NodeValue}; +/// let root = AstNode::from(NodeValue::Document); +/// ``` pub type AstNode<'a> = Node<'a, RefCell>; +impl<'a> From for AstNode<'a> { + /// Create a new AST node with the given value. + fn from(value: NodeValue) -> Self { + Node::new(RefCell::new(Ast::new(value))) + } +} + #[doc(hidden)] pub fn last_child_is_open<'a>(node: &'a AstNode<'a>) -> bool { node.last_child().map_or(false, |n| n.data.borrow().open)