@@ -8,10 +8,9 @@ use std::{
88use anyhow:: anyhow;
99use ignore:: { WalkBuilder , WalkState } ;
1010use oxvg_ast:: {
11- implementations:: {
12- roxmltree:: { parse, parse_file} ,
13- shared:: { Arena , Element , Ref } ,
14- } ,
11+ arena:: Allocator ,
12+ node:: Ref ,
13+ parse:: roxmltree:: parse,
1514 serialize:: { Indent , Node as _, Options } ,
1615 visitor:: Info ,
1716} ;
@@ -64,7 +63,7 @@ impl RunCommand for Optimise {
6463}
6564
6665impl Optimise {
67- fn handle_out < W : Write > ( dom : Ref < ' _ > , wr : W ) -> anyhow:: Result < W > {
66+ fn handle_out < W : Write > ( dom : Ref , wr : W ) -> anyhow:: Result < W > {
6867 Ok ( dom. serialize_into (
6968 wr,
7069 Options {
@@ -74,17 +73,22 @@ impl Optimise {
7473 ) ?)
7574 }
7675
77- fn handle_stdin < ' arena > ( & self , jobs : & Jobs , arena : Arena < ' arena > ) -> anyhow:: Result < ( ) > {
76+ fn handle_stdin ( & self , jobs : & Jobs ) -> anyhow:: Result < ( ) > {
7877 let mut source = String :: new ( ) ;
7978 std:: io:: stdin ( ) . read_to_string ( & mut source) ?;
80- let dom = parse ( & source, arena) ?;
79+ let xml = roxmltree:: Document :: parse ( & source) . unwrap ( ) ;
80+ let values = Allocator :: new_values ( ) ;
81+ let mut arena = Allocator :: new_arena ( ) ;
82+ let mut allocator = Allocator :: new ( & mut arena, & values) ;
83+ let dom = parse ( & xml, & mut allocator) ?;
8184
82- let info: Info < ' arena , Element < ' arena > > = Info {
85+ let info = Info {
8386 path : None ,
8487 multipass_count : 0 ,
85- arena ,
88+ allocator ,
8689 } ;
87- jobs. run ( & dom, & info) ?;
90+ jobs. run ( dom, & info)
91+ . map_err ( |e| anyhow:: Error :: msg ( e. to_string ( ) ) ) ?;
8892
8993 if let Some ( output) = & self . output . as_ref ( ) . and_then ( |o| {
9094 eprintln ! ( "Warning: Using empty `-o,--output` with stdin will print to stdout, you can instead omit `-o,--output`." ) ;
@@ -106,23 +110,22 @@ impl Optimise {
106110 Ok ( ( ) )
107111 }
108112
109- fn handle_file < ' arena > (
110- jobs : & Jobs ,
111- path : & PathBuf ,
112- output : Option < & PathBuf > ,
113- arena : Arena < ' arena > ,
114- ) -> anyhow:: Result < ( ) > {
115- let file = std:: fs:: File :: open ( path) ?;
116- let input_size = file. metadata ( ) ?. len ( ) as f64 / 1000.0 ;
117- let dom = parse_file ( path, arena) ?;
118- drop ( file) ;
119-
120- let info: Info < ' arena , Element < ' arena > > = Info {
113+ fn handle_file ( jobs : & Jobs , path : & PathBuf , output : Option < & PathBuf > ) -> anyhow:: Result < ( ) > {
114+ let file = std:: fs:: read_to_string ( path) ?;
115+ let input_size = file. len ( ) as f64 / 1000.0 ;
116+ let xml = roxmltree:: Document :: parse ( & file) . unwrap ( ) ;
117+ let values = Allocator :: new_values ( ) ;
118+ let mut arena = Allocator :: new_arena ( ) ;
119+ let mut allocator = Allocator :: new ( & mut arena, & values) ;
120+ let dom = parse ( & xml, & mut allocator) . unwrap ( ) ;
121+
122+ let info: Info = Info {
121123 path : Some ( path. clone ( ) ) ,
122124 multipass_count : 0 ,
123- arena ,
125+ allocator ,
124126 } ;
125- jobs. run ( & dom, & info) ?;
127+ jobs. run ( dom, & info)
128+ . map_err ( |e| anyhow:: Error :: msg ( e. to_string ( ) ) ) ?;
126129
127130 if let Some ( output_path) = output {
128131 if let Some ( parent) = output_path. parent ( ) {
@@ -165,7 +168,6 @@ impl Optimise {
165168 . build_parallel ( )
166169 . run ( || {
167170 Box :: new ( move |path| {
168- let arena = typed_arena:: Arena :: new ( ) ;
169171 let Ok ( path) = path else {
170172 return WalkState :: Continue ;
171173 } ;
@@ -179,12 +181,12 @@ impl Optimise {
179181 let Ok ( output_path) = output_path ( & path) else {
180182 return WalkState :: Continue ;
181183 } ;
182- if let Err ( err) = Self :: handle_file ( jobs, & path, output_path. as_ref ( ) , & arena ) {
184+ if let Err ( err) = Self :: handle_file ( jobs, & path, output_path. as_ref ( ) ) {
183185 eprintln ! (
184186 "{}: \x1b [31m{err}\x1b [0m" ,
185187 path. to_str( ) . unwrap_or_default( )
186188 ) ;
187- } ;
189+ }
188190 WalkState :: Continue
189191 } )
190192 } ) ;
@@ -198,8 +200,7 @@ impl Optimise {
198200 . first ( )
199201 . is_none_or ( |path| path == & PathBuf :: from_str ( "." ) . unwrap ( ) )
200202 {
201- let arena = typed_arena:: Arena :: new ( ) ;
202- return self . handle_stdin ( jobs, & arena) ;
203+ return self . handle_stdin ( jobs) ;
203204 }
204205 if self . paths . is_empty ( ) {
205206 return Err ( anyhow ! (
0 commit comments