@@ -5,12 +5,12 @@ use std::path::{Path, PathBuf};
5
5
use std:: str:: FromStr ;
6
6
7
7
use anyhow:: { Context , Result } ;
8
+ use serde:: { Deserialize , Serialize } ;
8
9
9
10
use crate :: cli:: self_update:: SelfUpdateMode ;
10
11
use crate :: dist:: dist:: Profile ;
11
12
use crate :: errors:: * ;
12
13
use crate :: notifications:: * ;
13
- use crate :: utils:: toml_utils:: * ;
14
14
use crate :: utils:: utils;
15
15
16
16
#[ derive( Clone , Debug , Eq , PartialEq ) ]
@@ -28,8 +28,12 @@ impl SettingsFile {
28
28
}
29
29
30
30
fn write_settings ( & self ) -> Result < ( ) > {
31
- let s = self . cache . borrow ( ) . as_ref ( ) . unwrap ( ) . clone ( ) ;
32
- utils:: write_file ( "settings" , & self . path , & s. stringify ( ) ) ?;
31
+ let settings = self . cache . borrow ( ) ;
32
+ utils:: write_file (
33
+ "settings" ,
34
+ & self . path ,
35
+ & settings. as_ref ( ) . unwrap ( ) . stringify ( ) ?,
36
+ ) ?;
33
37
Ok ( ( ) )
34
38
}
35
39
@@ -74,14 +78,20 @@ impl SettingsFile {
74
78
}
75
79
}
76
80
77
- #[ derive( Clone , Debug , Default , Eq , PartialEq ) ]
81
+ #[ derive( Clone , Debug , Default , Deserialize , Eq , PartialEq , Serialize ) ]
78
82
pub struct Settings {
79
83
pub version : MetadataVersion ,
84
+ #[ serde( skip_serializing_if = "Option::is_none" ) ]
80
85
pub default_host_triple : Option < String > ,
86
+ #[ serde( skip_serializing_if = "Option::is_none" ) ]
81
87
pub default_toolchain : Option < String > ,
88
+ #[ serde( skip_serializing_if = "Option::is_none" ) ]
82
89
pub profile : Option < Profile > ,
90
+ #[ serde( default ) ]
83
91
pub overrides : BTreeMap < String , String > ,
92
+ #[ serde( skip_serializing_if = "Option::is_none" ) ]
84
93
pub pgp_keys : Option < String > ,
94
+ #[ serde( skip_serializing_if = "Option::is_none" ) ]
85
95
pub auto_self_update : Option < SelfUpdateMode > ,
86
96
}
87
97
@@ -126,97 +136,19 @@ impl Settings {
126
136
}
127
137
128
138
pub ( crate ) fn parse ( data : & str ) -> Result < Self > {
129
- let value = toml:: from_str ( data) . context ( "error parsing settings" ) ?;
130
- Self :: from_toml ( value, "" )
139
+ toml:: from_str ( data) . context ( "error parsing settings" )
131
140
}
132
141
133
- pub ( crate ) fn stringify ( self ) -> String {
134
- self . into_toml ( ) . to_string ( )
135
- }
136
-
137
- pub ( crate ) fn from_toml ( mut table : toml:: value:: Table , path : & str ) -> Result < Self > {
138
- let version = get_string ( & mut table, "version" , path) ?;
139
- let version = MetadataVersion :: from_str ( & version) ?;
140
-
141
- let auto_self_update = get_opt_string ( & mut table, "auto_self_update" , path) ?
142
- . and_then ( |mode| SelfUpdateMode :: from_str ( mode. as_str ( ) ) . ok ( ) ) ;
143
- let profile = get_opt_string ( & mut table, "profile" , path) ?
144
- . and_then ( |p| Profile :: from_str ( p. as_str ( ) ) . ok ( ) ) ;
145
- Ok ( Self {
146
- version,
147
- default_host_triple : get_opt_string ( & mut table, "default_host_triple" , path) ?,
148
- default_toolchain : get_opt_string ( & mut table, "default_toolchain" , path) ?,
149
- profile,
150
- overrides : Self :: table_to_overrides ( & mut table, path) ?,
151
- pgp_keys : get_opt_string ( & mut table, "pgp_keys" , path) ?,
152
- auto_self_update,
153
- } )
154
- }
155
- pub ( crate ) fn into_toml ( self ) -> toml:: value:: Table {
156
- let mut result = toml:: value:: Table :: new ( ) ;
157
-
158
- result. insert (
159
- "version" . to_owned ( ) ,
160
- toml:: Value :: String ( self . version . as_str ( ) . to_owned ( ) ) ,
161
- ) ;
162
-
163
- if let Some ( v) = self . default_host_triple {
164
- result. insert ( "default_host_triple" . to_owned ( ) , toml:: Value :: String ( v) ) ;
165
- }
166
-
167
- if let Some ( v) = self . default_toolchain {
168
- result. insert ( "default_toolchain" . to_owned ( ) , toml:: Value :: String ( v) ) ;
169
- }
170
-
171
- if let Some ( v) = self . profile {
172
- result. insert ( "profile" . to_owned ( ) , toml:: Value :: String ( v. to_string ( ) ) ) ;
173
- }
174
-
175
- if let Some ( v) = self . pgp_keys {
176
- result. insert ( "pgp_keys" . to_owned ( ) , toml:: Value :: String ( v) ) ;
177
- }
178
-
179
- if let Some ( v) = self . auto_self_update {
180
- result. insert (
181
- "auto_self_update" . to_owned ( ) ,
182
- toml:: Value :: String ( v. to_string ( ) ) ,
183
- ) ;
184
- }
185
-
186
- let overrides = Self :: overrides_to_table ( self . overrides ) ;
187
- result. insert ( "overrides" . to_owned ( ) , toml:: Value :: Table ( overrides) ) ;
188
-
189
- result
190
- }
191
-
192
- fn table_to_overrides (
193
- table : & mut toml:: value:: Table ,
194
- path : & str ,
195
- ) -> Result < BTreeMap < String , String > > {
196
- let mut result = BTreeMap :: new ( ) ;
197
- let pkg_table = get_table ( table, "overrides" , path) ?;
198
-
199
- for ( k, v) in pkg_table {
200
- if let toml:: Value :: String ( t) = v {
201
- result. insert ( k, t) ;
202
- }
203
- }
204
-
205
- Ok ( result)
206
- }
207
-
208
- fn overrides_to_table ( overrides : BTreeMap < String , String > ) -> toml:: value:: Table {
209
- let mut result = toml:: value:: Table :: new ( ) ;
210
- for ( k, v) in overrides {
211
- result. insert ( k, toml:: Value :: String ( v) ) ;
212
- }
213
- result
142
+ fn stringify ( & self ) -> Result < String > {
143
+ Ok ( toml:: to_string ( self ) ?)
214
144
}
215
145
}
216
146
217
- #[ derive( Clone , Copy , Debug , Default , Eq , PartialEq ) ]
147
+ #[ derive( Clone , Copy , Debug , Default , Deserialize , Eq , PartialEq , Serialize ) ]
218
148
pub ( crate ) enum MetadataVersion {
149
+ #[ serde( rename = "2" ) ]
219
150
V2 ,
151
+ #[ serde( rename = "12" ) ]
220
152
#[ default]
221
153
V12 ,
222
154
}
@@ -255,7 +187,7 @@ mod tests {
255
187
#[ test]
256
188
fn serialize_default ( ) {
257
189
let settings = Settings :: default ( ) ;
258
- let toml = settings. stringify ( ) ;
190
+ let toml = settings. stringify ( ) . unwrap ( ) ;
259
191
assert_eq ! (
260
192
toml,
261
193
r#"version = "12"
@@ -281,8 +213,8 @@ mod tests {
281
213
..Default :: default ( )
282
214
} ;
283
215
284
- let toml = settings. stringify ( ) ;
285
- assert_eq ! ( toml, BASIC , ) ;
216
+ let toml = settings. stringify ( ) . unwrap ( ) ;
217
+ assert_eq ! ( toml, BASIC ) ;
286
218
}
287
219
288
220
#[ test]
@@ -296,9 +228,9 @@ mod tests {
296
228
assert_eq ! ( settings. profile, Some ( Profile :: Default ) ) ;
297
229
}
298
230
299
- const BASIC : & str = r#"default_toolchain = "stable-aarch64-apple-darwin"
231
+ const BASIC : & str = r#"version = "12"
232
+ default_toolchain = "stable-aarch64-apple-darwin"
300
233
profile = "default"
301
- version = "12"
302
234
303
235
[overrides]
304
236
"# ;
0 commit comments