Function dynparser::ast::consume_val

source ·
pub fn consume_val(nodes: &[Node]) -> Result<(&str, &[Node]), Error>
Expand description

Given a slice of nodes, return the value (&str) of first node if it is a Node::Rule and return the rest of nodes

If it’s not possible, return an error

     use dynparser::ast;
     let nodes = vec![
                 ast::Node::Val("hello".to_string()),
                 ast::Node::Val("world".to_string()),
     ];
     
     let (val, nodes) = ast::consume_val(&nodes).unwrap();
     assert!(val == "hello");
     assert!(nodes.len() == 1);

     let (val, nodes) = ast::consume_val(&nodes).unwrap();
     assert!(val == "world");
     assert!(nodes.len() == 0);