1- use std:: { fs:: { self , File } , io:: { Read , Write } } ;
2-
3- use clap:: { Parser , Args } ;
4- use parcel_css:: stylesheet:: { StyleSheet , MinifyOptions , PrinterOptions , ParserOptions } ;
1+ use std:: fs;
2+ use clap:: { Args , Parser } ;
3+ use parcel_css:: stylesheet:: { MinifyOptions , ParserOptions , PrinterOptions , StyleSheet } ;
54
65#[ derive( Parser , Debug ) ]
76#[ clap( author, version, about, long_about = None ) ]
87enum Cli {
9- ///Minify a given CSS file
10- Minify ( MinifyArgs )
8+ /// Minify a given CSS file
9+ Minify ( MinifyArgs ) ,
1110}
1211
1312#[ derive( Args , Debug ) ]
1413struct MinifyArgs {
15- ///The CSS file to minify
16- input_file : String ,
17- ///The name of the output file
18- #[ clap( short, long) ]
19- output_file : Option < String >
14+ /// The CSS file to minify
15+ input_file : String ,
16+ /// The name of the output file
17+ #[ clap( short, long) ]
18+ output_file : Option < String > ,
2019}
2120
22- pub fn main ( ) -> Result < ( ) , Box < dyn std:: error:: Error > > {
23- match Cli :: parse ( ) {
24- Cli :: Minify ( cli_args) => {
25- minify_css ( cli_args) ?;
26- }
21+ pub fn main ( ) -> Result < ( ) , std:: io:: Error > {
22+ match Cli :: parse ( ) {
23+ Cli :: Minify ( cli_args) => {
24+ minify_css ( cli_args)
2725 }
28-
29- Ok ( ( ) )
26+ }
3027}
3128
32- fn minify_css ( cli_args : MinifyArgs ) -> Result < ( ) , Box < dyn std:: error:: Error > > {
33- let source = read_file ( & cli_args. input_file ) ?;
34- let mut stylesheet = StyleSheet :: parse ( cli_args. input_file . to_string ( ) , & source, ParserOptions :: default ( ) ) . unwrap ( ) ;
35- stylesheet. minify ( MinifyOptions :: default ( ) ) ;
36- let res = stylesheet. to_css ( PrinterOptions { minify : true , ..PrinterOptions :: default ( ) } ) . unwrap ( ) ;
37- Ok ( if let Some ( output_file) = cli_args. output_file {
38- write_file ( & output_file, & res. code ) ?;
39- }
40- else {
41- println ! ( "{}" , res. code) ;
42- } )
43- }
29+ fn minify_css ( cli_args : MinifyArgs ) -> Result < ( ) , std:: io:: Error > {
30+ let source = fs:: read_to_string ( & cli_args. input_file ) ?;
31+ let mut stylesheet = StyleSheet :: parse ( cli_args. input_file . to_string ( ) , & source, ParserOptions :: default ( ) ) . unwrap ( ) ;
32+ stylesheet. minify ( MinifyOptions :: default ( ) ) ;
33+ let res = stylesheet. to_css ( PrinterOptions { minify : true , ..PrinterOptions :: default ( ) } ) . unwrap ( ) ;
4434
45- fn write_file ( output_filename : & str , file_content : & str ) -> Result < ( ) , std:: io:: Error > {
46- let mut file = File :: create ( output_filename) ?;
47- file. write_all ( file_content. as_bytes ( ) ) ?;
48- Ok ( ( ) )
49- }
50-
51- fn read_file ( filename : & str ) -> Result < String , std:: io:: Error > {
52- let mut file = fs:: File :: open ( filename) ?;
35+ if let Some ( output_file) = cli_args. output_file {
36+ fs:: write ( output_file, res. code . as_bytes ( ) ) ?;
37+ } else {
38+ println ! ( "{}" , res. code) ;
39+ }
5340
54- let mut buffer = String :: new ( ) ;
55- file. read_to_string ( & mut buffer) ?;
56- Ok ( buffer)
57- }
41+ Ok ( ( ) )
42+ }
0 commit comments