1
1
use rustfmt_nightly:: { Config , NewlineStyle , EmitMode , Edition } ;
2
2
3
- use std:: collections:: HashMap ;
4
3
use serde:: { Serialize , Deserialize } ;
5
- use dprint_core:: configuration:: { GlobalConfiguration , ResolveConfigurationResult , NewLineKind , ConfigurationDiagnostic } ;
4
+ use dprint_core:: configuration:: { GlobalConfiguration , ResolveConfigurationResult , NewLineKind , ConfigurationDiagnostic , ConfigKeyMap , ConfigKeyValue } ;
6
5
7
6
#[ derive( Clone , Serialize , Deserialize ) ]
8
7
pub struct Configuration {
9
8
// Unfortunately no resolved configuration at the moment because serializing
10
9
// rustfmt's PartialConfig configuration kept causing a panic
11
10
#[ serde( flatten) ]
12
- pub ( crate ) config : HashMap < String , String > ,
11
+ pub ( crate ) config : ConfigKeyMap ,
13
12
#[ serde( skip_serializing, skip_deserializing) ]
14
13
pub ( crate ) rustfmt_config : Config ,
15
14
}
16
15
17
16
pub fn resolve_config (
18
- config : HashMap < String , String > ,
17
+ config : ConfigKeyMap ,
19
18
global_config : & GlobalConfiguration ,
20
19
) -> ResolveConfigurationResult < Configuration > {
21
20
let mut rustfmt_config = Config :: default ( ) ;
@@ -44,15 +43,23 @@ pub fn resolve_config(
44
43
45
44
for ( key, value) in config. iter ( ) {
46
45
if key == "newLineKind" {
47
- match value. as_str ( ) {
48
- "auto" => rustfmt_config. set ( ) . newline_style ( NewlineStyle :: Auto ) ,
49
- "lf" => rustfmt_config. set ( ) . newline_style ( NewlineStyle :: Unix ) ,
50
- "crlf" => rustfmt_config. set ( ) . newline_style ( NewlineStyle :: Windows ) ,
51
- "system" => rustfmt_config. set ( ) . newline_style ( NewlineStyle :: Native ) ,
46
+ match value {
47
+ ConfigKeyValue :: String ( value) => match value. as_str ( ) {
48
+ "auto" => rustfmt_config. set ( ) . newline_style ( NewlineStyle :: Auto ) ,
49
+ "lf" => rustfmt_config. set ( ) . newline_style ( NewlineStyle :: Unix ) ,
50
+ "crlf" => rustfmt_config. set ( ) . newline_style ( NewlineStyle :: Windows ) ,
51
+ "system" => rustfmt_config. set ( ) . newline_style ( NewlineStyle :: Native ) ,
52
+ _ => {
53
+ diagnostics. push ( ConfigurationDiagnostic {
54
+ property_name : String :: from ( key) ,
55
+ message : format ! ( "Invalid newline kind: {}" , value) ,
56
+ } ) ;
57
+ }
58
+ } ,
52
59
_ => {
53
60
diagnostics. push ( ConfigurationDiagnostic {
54
61
property_name : String :: from ( key) ,
55
- message : format ! ( "Invalid newline kind: {}" , value ) ,
62
+ message : String :: from ( "Newline kind must be a string." ) ,
56
63
} ) ;
57
64
}
58
65
}
@@ -65,8 +72,9 @@ pub fn resolve_config(
65
72
"indentWidth" => "tab_spaces" ,
66
73
_ => key,
67
74
} ;
68
- if Config :: is_valid_key_val ( key, value) {
69
- rustfmt_config. override_value ( key, value) ;
75
+ let value = key_value_to_string ( value) ;
76
+ if Config :: is_valid_key_val ( key, & value) {
77
+ rustfmt_config. override_value ( key, & value) ;
70
78
} else {
71
79
let message = format ! ( "Invalid key or value in configuration. Key: {}, Value: {}" , key, value) ;
72
80
diagnostics. push ( ConfigurationDiagnostic {
@@ -83,3 +91,11 @@ pub fn resolve_config(
83
91
config : Configuration { config, rustfmt_config } ,
84
92
}
85
93
}
94
+
95
+ fn key_value_to_string ( value : & ConfigKeyValue ) -> String {
96
+ match value {
97
+ ConfigKeyValue :: String ( value) => value. clone ( ) ,
98
+ ConfigKeyValue :: Number ( value) => value. to_string ( ) ,
99
+ ConfigKeyValue :: Bool ( value) => value. to_string ( ) ,
100
+ }
101
+ }
0 commit comments