@@ -17,18 +17,15 @@ use ra_ap_paths::{AbsPath, AbsPathBuf, Utf8PathBuf};
1717use ra_ap_project_model:: {
1818 CargoConfig , CargoFeatures , CfgOverrides , RustLibSource , Sysroot , TargetDirectoryConfig ,
1919} ;
20- use rust_extractor_macros:: extractor_cli_config;
2120use serde:: { Deserialize , Serialize } ;
2221use std:: collections:: HashSet ;
23- use std:: fmt:: Debug ;
2422use std:: ops:: Not ;
2523use std:: path:: { Path , PathBuf } ;
2624
2725use crate :: toolchain:: select_toolchain;
2826
29- #[ derive( Debug , PartialEq , Eq , Default , Serialize , Deserialize , Clone , Copy , clap :: ValueEnum ) ]
27+ #[ derive( Debug , PartialEq , Eq , Default , Serialize , Deserialize , Clone , Copy ) ]
3028#[ serde( rename_all = "lowercase" ) ]
31- #[ clap( rename_all = "lowercase" ) ]
3229pub enum Compression {
3330 None ,
3431 Gzip ,
@@ -46,17 +43,38 @@ impl From<Compression> for trap::Compression {
4643 }
4744}
4845
49- #[ extractor_cli_config]
46+ #[ derive( Parser , Serialize ) ]
47+ #[ command( about, long_about = None ) ]
48+ struct CliConfig {
49+ #[ arg( long) ]
50+ #[ serde( skip_serializing_if = "Option::is_none" ) ]
51+ file_list : Option < PathBuf > ,
52+
53+ #[ arg( long) ]
54+ #[ serde( skip_serializing_if = "<&bool>::not" ) ]
55+ qltest : bool ,
56+
57+ #[ arg( long) ]
58+ #[ serde( skip_serializing_if = "Option::is_none" ) ]
59+ logging_verbosity : Option < String > ,
60+ }
61+
62+ #[ derive( Debug , Default , Deserialize ) ]
63+ #[ serde( default ) ]
5064pub struct Config {
5165 pub scratch_dir : PathBuf ,
5266 pub trap_dir : PathBuf ,
5367 pub source_archive_dir : PathBuf ,
5468 pub diagnostic_dir : PathBuf ,
5569 pub cargo_target_dir : Option < PathBuf > ,
5670 pub cargo_target : Option < String > ,
71+ #[ serde( deserialize_with = "deserialize::deserialize_newline_or_comma_separated_vec" ) ]
5772 pub cargo_features : Vec < String > ,
73+ #[ serde( deserialize_with = "deserialize::deserialize_newline_or_comma_separated_vec" ) ]
5874 pub cargo_cfg_overrides : Vec < String > ,
75+ #[ serde( deserialize_with = "deserialize::deserialize_newline_or_comma_separated_map" ) ]
5976 pub cargo_extra_env : FxHashMap < String , Option < String > > ,
77+ #[ serde( deserialize_with = "deserialize::deserialize_newline_or_comma_separated_vec" ) ]
6078 pub cargo_extra_args : Vec < String > ,
6179 pub cargo_all_targets : bool ,
6280 pub logging_flamegraph : Option < PathBuf > ,
@@ -65,13 +83,16 @@ pub struct Config {
6583 pub file_list : Option < PathBuf > ,
6684 pub qltest : bool ,
6785 pub qltest_cargo_check : bool ,
86+ #[ serde( deserialize_with = "deserialize::deserialize_newline_or_comma_separated_vec" ) ]
6887 pub qltest_dependencies : Vec < String > ,
6988 pub qltest_use_nightly : bool ,
7089 pub qltest_edition : Option < String > ,
7190 pub sysroot : Option < PathBuf > ,
7291 pub sysroot_src : Option < PathBuf > ,
7392 pub rustc_src : Option < PathBuf > ,
93+ #[ serde( deserialize_with = "deserialize::deserialize_newline_or_comma_separated_vec" ) ]
7494 pub build_script_command : Vec < String > ,
95+ #[ serde( deserialize_with = "deserialize::deserialize_newline_or_comma_separated_vec" ) ]
7596 pub extra_includes : Vec < PathBuf > ,
7697 pub proc_macro_server : Option < PathBuf > ,
7798 pub extract_dependencies_as_source : bool ,
@@ -286,3 +307,103 @@ fn join_path_buf(lhs: &AbsPath, rhs: &Path) -> AbsPathBuf {
286307 } ;
287308 lhs. join ( path)
288309}
310+
311+ #[ cfg( test) ]
312+ mod tests {
313+ use super :: * ;
314+
315+ #[ test]
316+ fn rejects_configuration_only_cli_options ( ) {
317+ for flag in [
318+ "--cargo-target" ,
319+ "--trap-compression" ,
320+ "--force-library-mode" ,
321+ ] {
322+ let err = CliConfig :: try_parse_from ( [ "extractor" , flag] )
323+ . err ( )
324+ . unwrap ( ) ;
325+ assert_eq ! ( err. kind( ) , clap:: error:: ErrorKind :: UnknownArgument ) ;
326+ }
327+ }
328+
329+ #[ test]
330+ fn absent_cli_options_preserve_configuration ( ) {
331+ let config: Config = Figment :: new ( )
332+ . merge ( Yaml :: string (
333+ r#"
334+ file_list: files.txt
335+ qltest: true
336+ logging_verbosity: debug
337+ "# ,
338+ ) )
339+ . merge ( Serialized :: defaults (
340+ CliConfig :: try_parse_from ( [ "extractor" ] ) . unwrap ( ) ,
341+ ) )
342+ . extract ( )
343+ . unwrap ( ) ;
344+
345+ assert_eq ! ( config. file_list, Some ( PathBuf :: from( "files.txt" ) ) ) ;
346+ assert ! ( config. qltest) ;
347+ assert_eq ! ( config. logging_verbosity. as_deref( ) , Some ( "debug" ) ) ;
348+ assert_eq ! ( config. trap_compression, Compression :: Zstd ) ;
349+ assert ! ( config. cargo_features. is_empty( ) ) ;
350+ assert ! ( config. cargo_extra_env. is_empty( ) ) ;
351+ }
352+
353+ #[ test]
354+ fn explicit_cli_options_override_configuration ( ) {
355+ let config: Config = Figment :: new ( )
356+ . merge ( Yaml :: string (
357+ r#"
358+ file_list: original.txt
359+ qltest: false
360+ logging_verbosity: off
361+ "# ,
362+ ) )
363+ . merge ( Serialized :: defaults (
364+ CliConfig :: try_parse_from ( [
365+ "extractor" ,
366+ "--file-list" ,
367+ "selected.txt" ,
368+ "--qltest" ,
369+ "--logging-verbosity=progress+" ,
370+ ] )
371+ . unwrap ( ) ,
372+ ) )
373+ . extract ( )
374+ . unwrap ( ) ;
375+
376+ assert_eq ! ( config. file_list, Some ( PathBuf :: from( "selected.txt" ) ) ) ;
377+ assert ! ( config. qltest) ;
378+ assert_eq ! ( config. logging_verbosity. as_deref( ) , Some ( "progress+" ) ) ;
379+ }
380+
381+ #[ test]
382+ fn preserves_custom_configuration_deserializers ( ) {
383+ let config: Config = Figment :: new ( )
384+ . merge ( Yaml :: string (
385+ r#"
386+ cargo_features: default,extra
387+ cargo_cfg_overrides: "foo\n-bar"
388+ cargo_extra_env: SET=value,UNSET
389+ cargo_extra_args: ["--offline"]
390+ qltest_dependencies: ["serde"]
391+ build_script_command: cargo,check
392+ extra_includes: ["generated"]
393+ trap_compression: none
394+ "# ,
395+ ) )
396+ . extract ( )
397+ . unwrap ( ) ;
398+
399+ assert_eq ! ( config. cargo_features, [ "default" , "extra" ] ) ;
400+ assert_eq ! ( config. cargo_cfg_overrides, [ "foo" , "-bar" ] ) ;
401+ assert_eq ! ( config. cargo_extra_env[ "SET" ] . as_deref( ) , Some ( "value" ) ) ;
402+ assert_eq ! ( config. cargo_extra_env[ "UNSET" ] , None ) ;
403+ assert_eq ! ( config. cargo_extra_args, [ "--offline" ] ) ;
404+ assert_eq ! ( config. qltest_dependencies, [ "serde" ] ) ;
405+ assert_eq ! ( config. build_script_command, [ "cargo" , "check" ] ) ;
406+ assert_eq ! ( config. extra_includes, [ PathBuf :: from( "generated" ) ] ) ;
407+ assert_eq ! ( config. trap_compression, Compression :: None ) ;
408+ }
409+ }
0 commit comments