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.rs45
1 files changed, 41 insertions, 4 deletions
diff --git a/lib/src/make.rs b/lib/src/make.rs
index cf371a2..2a6450c 100644
--- a/lib/src/make.rs
+++ b/lib/src/make.rs
@@ -1,13 +1,20 @@
1use std::iter::IntoIterator; 1use std::{fmt::Write, iter::IntoIterator};
2 2
3use rnix::{SyntaxNode, types::{TypedNode, self, TokenWrapper}}; 3use rnix::{
4 types::{self, TokenWrapper, TypedNode},
5 SyntaxNode,
6};
4 7
5fn ast_from_text<N: TypedNode>(text: &str) -> N { 8fn ast_from_text<N: TypedNode>(text: &str) -> N {
6 let parse = rnix::parse(text); 9 let parse = rnix::parse(text);
7 let node = match parse.node().descendants().find_map(N::cast) { 10 let node = match parse.node().descendants().find_map(N::cast) {
8 Some(it) => it, 11 Some(it) => it,
9 None => { 12 None => {
10 panic!("Failed to make ast node `{}` from text `{}`", std::any::type_name::<N>(), text) 13 panic!(
14 "Failed to make ast node `{}` from text `{}`",
15 std::any::type_name::<N>(),
16 text
17 )
11 } 18 }
12 }; 19 };
13 node 20 node
@@ -30,7 +37,10 @@ pub fn inherit_stmt<'a>(nodes: impl IntoIterator<Item = &'a types::Ident>) -> ty
30 ast_from_text(&format!("{{ inherit {}; }}", inherited_idents)) 37 ast_from_text(&format!("{{ inherit {}; }}", inherited_idents))
31} 38}
32 39
33pub fn inherit_from_stmt<'a>(from: SyntaxNode, nodes: impl IntoIterator<Item = &'a types::Ident>) -> types::Inherit { 40pub fn inherit_from_stmt<'a>(
41 from: SyntaxNode,
42 nodes: impl IntoIterator<Item = &'a types::Ident>,
43) -> types::Inherit {
34 let inherited_idents = nodes 44 let inherited_idents = nodes
35 .into_iter() 45 .into_iter()
36 .map(|i| i.as_str().to_owned()) 46 .map(|i| i.as_str().to_owned())
@@ -38,3 +48,30 @@ pub fn inherit_from_stmt<'a>(from: SyntaxNode, nodes: impl IntoIterator<Item = &
38 .join(" "); 48 .join(" ");
39 ast_from_text(&format!("{{ inherit ({}) {}; }}", from, inherited_idents)) 49 ast_from_text(&format!("{{ inherit ({}) {}; }}", from, inherited_idents))
40} 50}
51
52pub fn attrset(
53 inherits: impl IntoIterator<Item = types::Inherit>,
54 entries: impl IntoIterator<Item = types::KeyValue>,
55 recursive: bool,
56) -> types::AttrSet {
57 let mut buffer = String::new();
58
59 writeln!(buffer, "{}{{", if recursive { "rec " } else { "" }).unwrap();
60 for inherit in inherits.into_iter() {
61 writeln!(buffer, " {}", inherit.node().text()).unwrap();
62 }
63 for entry in entries.into_iter() {
64 writeln!(buffer, " {}", entry.node().text()).unwrap();
65 }
66 write!(buffer, "}}").unwrap();
67
68 ast_from_text(&buffer)
69}
70
71pub fn select(set: &SyntaxNode, index: &SyntaxNode) -> types::Select {
72 ast_from_text(&format!("{}.{}", set, index))
73}
74
75pub fn ident(text: &str) -> types::Ident {
76 ast_from_text(text)
77}