1
- use anyhow:: { bail, Context , Result } ;
1
+ use std:: fmt;
2
+ use std:: str:: FromStr ;
3
+
4
+ use anyhow:: { Context , Result } ;
2
5
3
6
use super :: manifest:: Component ;
4
7
use crate :: errors:: * ;
5
8
use crate :: utils:: toml_utils:: * ;
6
9
7
- pub ( crate ) const SUPPORTED_CONFIG_VERSIONS : [ & str ; 1 ] = [ "1" ] ;
8
- pub ( crate ) const DEFAULT_CONFIG_VERSION : & str = "1" ;
9
-
10
- #[ derive( Clone , Debug ) ]
10
+ #[ derive( Clone , Debug , Default ) ]
11
11
pub struct Config {
12
- pub config_version : String ,
12
+ pub config_version : ConfigVersion ,
13
13
pub components : Vec < Component > ,
14
14
}
15
15
16
16
impl Config {
17
17
pub ( crate ) fn from_toml ( mut table : toml:: value:: Table , path : & str ) -> Result < Self > {
18
18
let config_version = get_string ( & mut table, "config_version" , path) ?;
19
- if !SUPPORTED_CONFIG_VERSIONS . contains ( & & * config_version) {
20
- bail ! ( RustupError :: UnsupportedVersion ( config_version) ) ;
21
- }
19
+ let config_version = ConfigVersion :: from_str ( & config_version) ?;
22
20
23
21
let components = get_array ( & mut table, "components" , path) ?;
24
22
let components =
@@ -33,7 +31,7 @@ impl Config {
33
31
let mut result = toml:: value:: Table :: new ( ) ;
34
32
result. insert (
35
33
"config_version" . to_owned ( ) ,
36
- toml:: Value :: String ( self . config_version ) ,
34
+ toml:: Value :: String ( self . config_version . as_str ( ) . to_owned ( ) ) ,
37
35
) ;
38
36
let components = Self :: components_to_toml ( self . components ) ;
39
37
if !components. is_empty ( ) {
@@ -77,11 +75,33 @@ impl Config {
77
75
}
78
76
}
79
77
80
- impl Default for Config {
81
- fn default ( ) -> Self {
82
- Self {
83
- config_version : DEFAULT_CONFIG_VERSION . to_owned ( ) ,
84
- components : Vec :: new ( ) ,
78
+ #[ derive( Clone , Copy , Debug , Default , Eq , PartialEq ) ]
79
+ pub ( crate ) enum ConfigVersion {
80
+ #[ default]
81
+ V1 ,
82
+ }
83
+
84
+ impl ConfigVersion {
85
+ pub fn as_str ( & self ) -> & ' static str {
86
+ match self {
87
+ Self :: V1 => "1" ,
85
88
}
86
89
}
87
90
}
91
+
92
+ impl FromStr for ConfigVersion {
93
+ type Err = RustupError ;
94
+
95
+ fn from_str ( s : & str ) -> Result < Self , Self :: Err > {
96
+ match s {
97
+ "1" => Ok ( Self :: V1 ) ,
98
+ _ => Err ( RustupError :: UnsupportedVersion ( s. to_owned ( ) ) ) ,
99
+ }
100
+ }
101
+ }
102
+
103
+ impl fmt:: Display for ConfigVersion {
104
+ fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
105
+ write ! ( f, "{}" , self . as_str( ) )
106
+ }
107
+ }
0 commit comments