1
+ use crate :: parser:: Root ;
1
2
use crate :: { parser, visitor:: Visit } ;
2
- use std:: io:: Result ;
3
- use std:: io:: Write ;
4
-
3
+ use std:: fmt:: Result ;
4
+ use std:: fmt:: Write ;
5
5
#[ derive( Default ) ]
6
6
pub struct AstPrinter < W : Write > {
7
7
level : usize ,
@@ -13,7 +13,7 @@ impl<W: Write> AstPrinter<W> {
13
13
Self { level, writer }
14
14
}
15
15
16
- pub fn print < ' a > ( & mut self , root : & ' a parser:: Root < ' a > ) -> Result < ( ) > {
16
+ pub fn print < ' a > ( & mut self , root : & ' a parser:: Root < ' a > ) -> Result {
17
17
self . visit_root ( root) ?;
18
18
Ok ( ( ) )
19
19
}
@@ -23,8 +23,8 @@ impl<W: Write> AstPrinter<W> {
23
23
}
24
24
}
25
25
26
- impl < ' a , W : Write > Visit < ' a , Result < ( ) > > for AstPrinter < W > {
27
- fn visit_root ( & mut self , root : & parser:: Root ) -> Result < ( ) > {
26
+ impl < ' a , W : Write > Visit < ' a , Result > for AstPrinter < W > {
27
+ fn visit_root ( & mut self , root : & parser:: Root ) -> Result {
28
28
writeln ! (
29
29
self . writer,
30
30
"{}Root@{:?}" ,
@@ -49,7 +49,7 @@ impl<'a, W: Write> Visit<'a, Result<()>> for AstPrinter<W> {
49
49
Ok ( ( ) )
50
50
}
51
51
52
- fn visit_rule ( & mut self , rule : & parser:: Rule ) -> Result < ( ) > {
52
+ fn visit_rule ( & mut self , rule : & parser:: Rule ) -> Result {
53
53
writeln ! (
54
54
self . writer,
55
55
"{}Rule@{:?}" ,
@@ -80,7 +80,7 @@ impl<'a, W: Write> Visit<'a, Result<()>> for AstPrinter<W> {
80
80
Ok ( ( ) )
81
81
}
82
82
83
- fn visit_at_rule ( & mut self , at_rule : & parser:: AtRule ) -> Result < ( ) > {
83
+ fn visit_at_rule ( & mut self , at_rule : & parser:: AtRule ) -> Result {
84
84
writeln ! (
85
85
self . writer,
86
86
"{}AtRule@{:?}" ,
@@ -117,7 +117,7 @@ impl<'a, W: Write> Visit<'a, Result<()>> for AstPrinter<W> {
117
117
Ok ( ( ) )
118
118
}
119
119
120
- fn visit_declaration ( & mut self , decl : & parser:: Declaration ) -> Result < ( ) > {
120
+ fn visit_declaration ( & mut self , decl : & parser:: Declaration ) -> Result {
121
121
writeln ! (
122
122
self . writer,
123
123
"{}Declaration@{:?}" ,
@@ -142,27 +142,8 @@ impl<'a, W: Write> Visit<'a, Result<()>> for AstPrinter<W> {
142
142
}
143
143
}
144
144
145
- #[ derive( Debug , Default ) ]
146
- pub struct WrapString ( pub String ) ;
147
- impl WrapString {
148
- pub fn inner_string ( self ) -> String {
149
- self . 0
150
- }
151
- }
152
-
153
- impl From < String > for WrapString {
154
- fn from ( string : String ) -> Self {
155
- Self ( string)
156
- }
157
- }
158
-
159
- impl Write for WrapString {
160
- fn write ( & mut self , buf : & [ u8 ] ) -> std:: io:: Result < usize > {
161
- self . 0 += std:: str:: from_utf8 ( buf) . unwrap ( ) ;
162
- Ok ( buf. len ( ) )
163
- }
164
-
165
- fn flush ( & mut self ) -> std:: io:: Result < ( ) > {
166
- Ok ( ( ) )
167
- }
145
+ pub fn pretty_print_ast ( root : & Root ) -> String {
146
+ let mut printer = AstPrinter :: new ( 0 , String :: default ( ) ) ;
147
+ printer. print ( root) . unwrap ( ) ;
148
+ printer. result ( )
168
149
}
0 commit comments