aboutsummaryrefslogtreecommitdiff
path: root/lib/src/make.rs
diff options
context:
space:
mode:
Diffstat (limited to 'lib/src/make.rs')
-rw-r--r--lib/src/make.rs20
1 files changed, 20 insertions, 0 deletions
diff --git a/lib/src/make.rs b/lib/src/make.rs
new file mode 100644
index 0000000..0b21105
--- /dev/null
+++ b/lib/src/make.rs
@@ -0,0 +1,20 @@
1use rnix::{SyntaxNode, types::{TypedNode, self}};
2
3fn ast_from_text<N: TypedNode>(text: &str) -> N {
4 let parse = rnix::parse(text);
5 let node = match parse.node().descendants().find_map(N::cast) {
6 Some(it) => it,
7 None => {
8 panic!("Failed to make ast node `{}` from text {}", std::any::type_name::<N>(), text)
9 }
10 };
11 node
12}
13
14pub fn parenthesize(node: &SyntaxNode) -> types::Paren {
15 ast_from_text(&format!("({})", node))
16}
17
18pub fn unary_not(node: &SyntaxNode) -> types::UnaryOp {
19 ast_from_text(&format!("!{}", node))
20}