1
1
// Copyright (c) 2021 Ghaith Hachem and Mathias Rieder
2
2
use std:: path:: Path ;
3
+ use encoding_rs:: Encoding ;
3
4
use structopt:: { clap:: ArgGroup , StructOpt } ;
4
5
5
6
#[ derive( PartialEq , Debug ) ]
@@ -68,6 +69,14 @@ pub struct CompileParameters {
68
69
) ]
69
70
pub target : Option < String > ,
70
71
72
+ #[ structopt(
73
+ long,
74
+ name = "encoding" ,
75
+ help = "The file encoding used to read the input-files, as defined by the Encoding Standard" ,
76
+ parse( try_from_str = parse_encoding) ,
77
+ ) ]
78
+ pub encoding : Option < & ' static Encoding > ,
79
+
71
80
#[ structopt(
72
81
name = "input-files" ,
73
82
help = "Read input from <input-files>, may be a glob expression like 'src/**/*' or a sequence of files" ,
@@ -78,7 +87,12 @@ pub struct CompileParameters {
78
87
pub input : Vec < String > ,
79
88
}
80
89
90
+ fn parse_encoding ( encoding : & str ) -> Result < & ' static Encoding , String > {
91
+ Encoding :: for_label ( encoding. as_bytes ( ) ) . ok_or ( format ! ( "Unknown encoding {}" , encoding) )
92
+ }
93
+
81
94
impl CompileParameters {
95
+
82
96
pub fn parse ( args : Vec < String > ) -> Result < CompileParameters , ParameterError > {
83
97
CompileParameters :: from_iter_safe ( args)
84
98
}
@@ -130,6 +144,7 @@ impl CompileParameters {
130
144
#[ cfg( test) ]
131
145
mod cli_tests {
132
146
use super :: { CompileParameters , FormatOption , ParameterError } ;
147
+ use pretty_assertions:: assert_eq;
133
148
use structopt:: clap:: ErrorKind ;
134
149
135
150
fn expect_argument_error ( args : Vec < String > , expected_error_kind : ErrorKind ) {
@@ -255,6 +270,14 @@ mod cli_tests {
255
270
assert_eq ! ( parameters. output_format_or_default( ) , super :: DEFAULT_FORMAT ) ;
256
271
}
257
272
273
+ #[ test]
274
+ fn encoding_resolution ( ) {
275
+ let parameters = CompileParameters :: parse ( vec_of_strings ! ( "input.st" , "--ir" , "--encoding" , "cp1252" ) ) . unwrap ( ) ;
276
+ assert_eq ! ( parameters. encoding, Some ( encoding_rs:: WINDOWS_1252 ) ) ;
277
+ let parameters = CompileParameters :: parse ( vec_of_strings ! ( "input.st" , "--ir" , "--encoding" , "windows-1252" ) ) . unwrap ( ) ;
278
+ assert_eq ! ( parameters. encoding, Some ( encoding_rs:: WINDOWS_1252 ) ) ;
279
+ }
280
+
258
281
#[ test]
259
282
fn valid_output_formats ( ) {
260
283
let parameters = CompileParameters :: parse ( vec_of_strings ! ( "input.st" , "--ir" ) ) . unwrap ( ) ;
0 commit comments