Skip to content

Commit f9d44aa

Browse files
committed
refactor: some more improvements to make
1 parent c8289a0 commit f9d44aa

File tree

1 file changed

+35
-28
lines changed

1 file changed

+35
-28
lines changed

lib/src/make.rs

Lines changed: 35 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
1-
use std::{fmt::Write, iter::IntoIterator};
2-
31
use rnix::{
42
ast::{self, AstNode},
53
SyntaxNode,
64
};
75

8-
fn ast_from_text<N: AstNode>(text: &str) -> N {
9-
let parsed = crate::parse::ParseResult::parse(text)
6+
fn ast_from_text<N: AstNode>(text: impl AsRef<str>) -> N {
7+
let parsed = crate::parse::ParseResult::parse(text.as_ref())
108
.to_result()
119
.unwrap_or_else(|errors| {
1210
panic!(
1311
"Failed to make ast node `{}` from text `{}`\n{errors}",
1412
std::any::type_name::<N>(),
15-
text,
13+
text.as_ref(),
1614
errors = errors
1715
.into_iter()
1816
.map(|error| error.to_string())
@@ -21,28 +19,31 @@ fn ast_from_text<N: AstNode>(text: &str) -> N {
2119
);
2220
});
2321

24-
parsed
25-
.children()
22+
// for inherit it goes ROOT ( ATTRSET (INHERIT)), hence we have to traverse 3 levels at worst
23+
// FIXME: just specialize for the structs
24+
std::iter::once(parsed.clone())
25+
.chain(parsed.clone().children())
26+
.chain(parsed.children().flat_map(|child| child.children()))
2627
.find_map(|child| N::cast(child))
2728
.unwrap_or_else(|| {
2829
panic!(
2930
"Failed to make ast node `{}` from text `{}`",
3031
std::any::type_name::<N>(),
31-
text
32+
text.as_ref()
3233
)
3334
})
3435
}
3536

3637
pub fn parenthesize(node: &SyntaxNode) -> ast::Paren {
37-
ast_from_text(&format!("({})", node))
38+
ast_from_text(format!("({node})"))
3839
}
3940

4041
pub fn quote(node: &SyntaxNode) -> ast::Str {
41-
ast_from_text(&format!("\"{}\"", node))
42+
ast_from_text(format!("\"{node}\""))
4243
}
4344

4445
pub fn unary_not(node: &SyntaxNode) -> ast::UnaryOp {
45-
ast_from_text(&format!("!{}", node))
46+
ast_from_text(format!("!{node}"))
4647
}
4748

4849
pub fn inherit_stmt<'a>(nodes: impl IntoIterator<Item = &'a ast::Ident>) -> ast::Inherit {
@@ -51,7 +52,7 @@ pub fn inherit_stmt<'a>(nodes: impl IntoIterator<Item = &'a ast::Ident>) -> ast:
5152
.map(|i| i.to_string())
5253
.collect::<Vec<_>>()
5354
.join(" ");
54-
ast_from_text(&format!("{{ inherit {}; }}", inherited_idents))
55+
ast_from_text(format!("{{ inherit {inherited_idents}; }}"))
5556
}
5657

5758
pub fn inherit_from_stmt<'a>(
@@ -63,30 +64,36 @@ pub fn inherit_from_stmt<'a>(
6364
.map(|i| i.to_string())
6465
.collect::<Vec<_>>()
6566
.join(" ");
66-
ast_from_text(&format!("{{ inherit ({}) {}; }}", from, inherited_idents))
67+
ast_from_text(format!("{{ inherit ({from}) {inherited_idents}; }}"))
6768
}
6869

6970
pub fn attrset(
7071
inherits: impl IntoIterator<Item = ast::Inherit>,
7172
entries: impl IntoIterator<Item = ast::Entry>,
7273
recursive: bool,
7374
) -> ast::AttrSet {
74-
let mut buffer = String::new();
75-
76-
writeln!(buffer, "{}{{", if recursive { "rec " } else { "" }).unwrap();
77-
for inherit in inherits.into_iter() {
78-
writeln!(buffer, " {inherit}").unwrap();
79-
}
80-
for entry in entries.into_iter() {
81-
writeln!(buffer, " {entry}").unwrap();
82-
}
83-
write!(buffer, "}}").unwrap();
84-
85-
ast_from_text(&buffer)
75+
let rec = recursive.then_some("rec ").unwrap_or_default();
76+
let inherits = inherits
77+
.into_iter()
78+
.map(|inherit| format!(" {inherit}"))
79+
.collect::<Vec<_>>()
80+
.join("\n");
81+
let entries = entries
82+
.into_iter()
83+
.map(|inherit| format!(" {inherit}"))
84+
.collect::<Vec<_>>()
85+
.join("\n");
86+
87+
ast_from_text(format!(
88+
"{rec}{{
89+
{inherits}
90+
{entries}
91+
}}"
92+
))
8693
}
8794

8895
pub fn select(set: &SyntaxNode, index: &SyntaxNode) -> ast::Select {
89-
ast_from_text(&format!("{}.{}", set, index))
96+
ast_from_text(format!("{set}.{index}"))
9097
}
9198

9299
pub fn ident(text: &str) -> ast::Ident {
@@ -99,9 +106,9 @@ pub fn empty() -> ast::Root {
99106

100107
// TODO: make `op` strongly typed here
101108
pub fn binary(lhs: &SyntaxNode, op: &str, rhs: &SyntaxNode) -> ast::BinOp {
102-
ast_from_text(&format!("{} {} {}", lhs, op, rhs))
109+
ast_from_text(format!("{lhs} {op} {rhs}"))
103110
}
104111

105112
pub fn or_default(set: &SyntaxNode, index: &SyntaxNode, default: &SyntaxNode) -> ast::Select {
106-
ast_from_text(&format!("{}.{} or {}", set, index, default))
113+
ast_from_text(format!("{set}.{index} or {default}"))
107114
}

0 commit comments

Comments
 (0)