Skip to content

Commit

Permalink
Merge pull request #1303 from froydnj/froydnj-generic-node-conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
HParker authored Aug 23, 2023
2 parents 2b7e03a + 02eb4d4 commit 7b2f516
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
6 changes: 6 additions & 0 deletions rust/yarp/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,12 @@ fn write_node(file: &mut File, node: &Node) -> Result<(), Box<dyn std::error::Er
writeln!(file, "}}")?;
writeln!(file)?;
writeln!(file, "impl<'pr> {}<'pr> {{", node.name)?;
writeln!(file, " /// Converts this node to a generic node.")?;
writeln!(file, " #[must_use]")?;
writeln!(file, " pub fn as_node(&self) -> Node<'pr> {{")?;
writeln!(file, " Node::{} {{ pointer: self.pointer, marker: PhantomData }}", node.name)?;
writeln!(file, " }}")?;
writeln!(file)?;
writeln!(file, " /// Returns the location of this node.")?;
writeln!(file, " #[must_use]")?;
writeln!(file, " pub fn location(&self) -> Location<'pr> {{")?;
Expand Down
16 changes: 16 additions & 0 deletions rust/yarp/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -312,4 +312,20 @@ mod tests {

assert_eq!(visitor.count, 2);
}

#[test]
fn node_upcast_test() {
use super::Node;

let source = "module Foo; end";
let result = parse(source.as_ref());

let node = result.node();
let upcast_node = node.as_program_node().unwrap().as_node();
assert!(matches!(upcast_node, Node::ProgramNode { .. }));

let node = node.as_program_node().unwrap().statements().body().iter().next().unwrap();
let upcast_node = node.as_module_node().unwrap().as_node();
assert!(matches!(upcast_node, Node::ModuleNode { .. }));
}
}

0 comments on commit 7b2f516

Please sign in to comment.