1
- use std:: { fmt:: Write , iter:: IntoIterator } ;
2
-
3
1
use rnix:: {
4
2
ast:: { self , AstNode } ,
5
3
SyntaxNode ,
6
4
} ;
7
5
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 ( ) )
10
8
. to_result ( )
11
9
. unwrap_or_else ( |errors| {
12
10
panic ! (
13
11
"Failed to make ast node `{}` from text `{}`\n {errors}" ,
14
12
std:: any:: type_name:: <N >( ) ,
15
- text,
13
+ text. as_ref ( ) ,
16
14
errors = errors
17
15
. into_iter( )
18
16
. map( |error| error. to_string( ) )
@@ -21,28 +19,31 @@ fn ast_from_text<N: AstNode>(text: &str) -> N {
21
19
) ;
22
20
} ) ;
23
21
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 ( ) ) )
26
27
. find_map ( |child| N :: cast ( child) )
27
28
. unwrap_or_else ( || {
28
29
panic ! (
29
30
"Failed to make ast node `{}` from text `{}`" ,
30
31
std:: any:: type_name:: <N >( ) ,
31
- text
32
+ text. as_ref ( )
32
33
)
33
34
} )
34
35
}
35
36
36
37
pub fn parenthesize ( node : & SyntaxNode ) -> ast:: Paren {
37
- ast_from_text ( & format ! ( "({})" , node ) )
38
+ ast_from_text ( format ! ( "({node })" ) )
38
39
}
39
40
40
41
pub fn quote ( node : & SyntaxNode ) -> ast:: Str {
41
- ast_from_text ( & format ! ( "\" {}\" " , node ) )
42
+ ast_from_text ( format ! ( "\" {node }\" " ) )
42
43
}
43
44
44
45
pub fn unary_not ( node : & SyntaxNode ) -> ast:: UnaryOp {
45
- ast_from_text ( & format ! ( "!{}" , node ) )
46
+ ast_from_text ( format ! ( "!{node}" ) )
46
47
}
47
48
48
49
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:
51
52
. map ( |i| i. to_string ( ) )
52
53
. collect :: < Vec < _ > > ( )
53
54
. join ( " " ) ;
54
- ast_from_text ( & format ! ( "{{ inherit {}; }}" , inherited_idents ) )
55
+ ast_from_text ( format ! ( "{{ inherit {inherited_idents }; }}" ) )
55
56
}
56
57
57
58
pub fn inherit_from_stmt < ' a > (
@@ -63,30 +64,36 @@ pub fn inherit_from_stmt<'a>(
63
64
. map ( |i| i. to_string ( ) )
64
65
. collect :: < Vec < _ > > ( )
65
66
. join ( " " ) ;
66
- ast_from_text ( & format ! ( "{{ inherit ({}) {}; }}" , from , inherited_idents ) )
67
+ ast_from_text ( format ! ( "{{ inherit ({from }) {inherited_idents }; }}" ) )
67
68
}
68
69
69
70
pub fn attrset (
70
71
inherits : impl IntoIterator < Item = ast:: Inherit > ,
71
72
entries : impl IntoIterator < Item = ast:: Entry > ,
72
73
recursive : bool ,
73
74
) -> 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
+ ) )
86
93
}
87
94
88
95
pub fn select ( set : & SyntaxNode , index : & SyntaxNode ) -> ast:: Select {
89
- ast_from_text ( & format ! ( "{}.{}" , set , index ) )
96
+ ast_from_text ( format ! ( "{set }.{index}" ) )
90
97
}
91
98
92
99
pub fn ident ( text : & str ) -> ast:: Ident {
@@ -99,9 +106,9 @@ pub fn empty() -> ast::Root {
99
106
100
107
// TODO: make `op` strongly typed here
101
108
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}" ) )
103
110
}
104
111
105
112
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}" ) )
107
114
}
0 commit comments