Skip to content

Commit b637229

Browse files
author
Ashe Connor
authored
Merge pull request #105 from sunjay/new-node
Added a constructor for AstNode
2 parents 01eaf51 + 1998c04 commit b637229

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

src/nodes.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,8 +380,22 @@ impl Ast {
380380
/// It is bound by the lifetime `'a`, which corresponds to the `Arena` nodes are allocated in.
381381
/// `AstNode`s are almost handled as a reference itself bound by `'a`. Child `Ast`s are wrapped in
382382
/// `RefCell` for interior mutability.
383+
///
384+
/// You can construct a new `AstNode` from a `NodeValue` using the `From` trait:
385+
///
386+
/// ```no_run
387+
/// # use comrak::nodes::{AstNode, NodeValue};
388+
/// let root = AstNode::from(NodeValue::Document);
389+
/// ```
383390
pub type AstNode<'a> = Node<'a, RefCell<Ast>>;
384391

392+
impl<'a> From<NodeValue> for AstNode<'a> {
393+
/// Create a new AST node with the given value.
394+
fn from(value: NodeValue) -> Self {
395+
Node::new(RefCell::new(Ast::new(value)))
396+
}
397+
}
398+
385399
#[doc(hidden)]
386400
pub fn last_child_is_open<'a>(node: &'a AstNode<'a>) -> bool {
387401
node.last_child().map_or(false, |n| n.data.borrow().open)

0 commit comments

Comments
 (0)