1
1
use std:: cell:: RefCell ;
2
2
use std:: collections:: BTreeMap ;
3
+ use std:: fmt;
3
4
use std:: path:: { Path , PathBuf } ;
4
5
use std:: str:: FromStr ;
5
6
@@ -12,9 +13,6 @@ use crate::notifications::*;
12
13
use crate :: utils:: toml_utils:: * ;
13
14
use crate :: utils:: utils;
14
15
15
- pub ( crate ) const SUPPORTED_METADATA_VERSIONS : [ & str ; 2 ] = [ "2" , "12" ] ;
16
- pub ( crate ) const DEFAULT_METADATA_VERSION : & str = "12" ;
17
-
18
16
#[ derive( Clone , Debug , Eq , PartialEq ) ]
19
17
pub struct SettingsFile {
20
18
path : PathBuf ,
@@ -78,7 +76,7 @@ impl SettingsFile {
78
76
79
77
#[ derive( Clone , Debug , Eq , PartialEq ) ]
80
78
pub struct Settings {
81
- pub version : String ,
79
+ pub version : MetadataVersion ,
82
80
pub default_host_triple : Option < String > ,
83
81
pub default_toolchain : Option < String > ,
84
82
pub profile : Option < Profile > ,
@@ -90,7 +88,7 @@ pub struct Settings {
90
88
impl Default for Settings {
91
89
fn default ( ) -> Self {
92
90
Self {
93
- version : DEFAULT_METADATA_VERSION . to_owned ( ) ,
91
+ version : MetadataVersion :: default ( ) ,
94
92
default_host_triple : None ,
95
93
default_toolchain : None ,
96
94
profile : Some ( Profile :: default ( ) ) ,
@@ -152,9 +150,8 @@ impl Settings {
152
150
153
151
pub ( crate ) fn from_toml ( mut table : toml:: value:: Table , path : & str ) -> Result < Self > {
154
152
let version = get_string ( & mut table, "version" , path) ?;
155
- if !SUPPORTED_METADATA_VERSIONS . contains ( & & * version) {
156
- return Err ( RustupError :: UnknownMetadataVersion ( version) . into ( ) ) ;
157
- }
153
+ let version = MetadataVersion :: from_str ( & version) ?;
154
+
158
155
let auto_self_update = get_opt_string ( & mut table, "auto_self_update" , path) ?
159
156
. and_then ( |mode| SelfUpdateMode :: from_str ( mode. as_str ( ) ) . ok ( ) ) ;
160
157
let profile = get_opt_string ( & mut table, "profile" , path) ?
@@ -172,7 +169,10 @@ impl Settings {
172
169
pub ( crate ) fn into_toml ( self ) -> toml:: value:: Table {
173
170
let mut result = toml:: value:: Table :: new ( ) ;
174
171
175
- result. insert ( "version" . to_owned ( ) , toml:: Value :: String ( self . version ) ) ;
172
+ result. insert (
173
+ "version" . to_owned ( ) ,
174
+ toml:: Value :: String ( self . version . as_str ( ) . to_owned ( ) ) ,
175
+ ) ;
176
176
177
177
if let Some ( v) = self . default_host_triple {
178
178
result. insert ( "default_host_triple" . to_owned ( ) , toml:: Value :: String ( v) ) ;
@@ -227,3 +227,37 @@ impl Settings {
227
227
result
228
228
}
229
229
}
230
+
231
+ #[ derive( Clone , Copy , Debug , Default , Eq , PartialEq ) ]
232
+ pub ( crate ) enum MetadataVersion {
233
+ V2 ,
234
+ #[ default]
235
+ V12 ,
236
+ }
237
+
238
+ impl MetadataVersion {
239
+ fn as_str ( & self ) -> & ' static str {
240
+ match self {
241
+ Self :: V2 => "2" ,
242
+ Self :: V12 => "12" ,
243
+ }
244
+ }
245
+ }
246
+
247
+ impl FromStr for MetadataVersion {
248
+ type Err = RustupError ;
249
+
250
+ fn from_str ( s : & str ) -> Result < Self , Self :: Err > {
251
+ match s {
252
+ "2" => Ok ( Self :: V2 ) ,
253
+ "12" => Ok ( Self :: V12 ) ,
254
+ _ => Err ( RustupError :: UnknownMetadataVersion ( s. to_owned ( ) ) ) ,
255
+ }
256
+ }
257
+ }
258
+
259
+ impl fmt:: Display for MetadataVersion {
260
+ fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
261
+ write ! ( f, "{}" , self . as_str( ) )
262
+ }
263
+ }
0 commit comments