@@ -2,81 +2,34 @@ use std::fmt;
2
2
use std:: str:: FromStr ;
3
3
4
4
use anyhow:: { Context , Result } ;
5
+ use serde:: { Deserialize , Serialize } ;
5
6
6
7
use super :: manifest:: Component ;
7
8
use crate :: errors:: * ;
8
- use crate :: utils:: toml_utils:: * ;
9
9
10
- #[ derive( Clone , Debug , Default ) ]
10
+ #[ derive( Clone , Debug , Default , Deserialize , Serialize ) ]
11
11
pub struct Config {
12
12
pub config_version : ConfigVersion ,
13
13
pub components : Vec < Component > ,
14
14
}
15
15
16
16
impl Config {
17
- pub ( crate ) fn from_toml ( mut table : toml:: value:: Table , path : & str ) -> Result < Self > {
18
- let config_version = get_string ( & mut table, "config_version" , path) ?;
19
- let config_version = ConfigVersion :: from_str ( & config_version) ?;
20
-
21
- let components = get_array ( & mut table, "components" , path) ?;
22
- let components =
23
- Self :: toml_to_components ( components, & format ! ( "{}{}." , path, "components" ) ) ?;
24
-
25
- Ok ( Self {
26
- config_version,
27
- components,
28
- } )
29
- }
30
- pub ( crate ) fn into_toml ( self ) -> toml:: value:: Table {
31
- let mut result = toml:: value:: Table :: new ( ) ;
32
- result. insert (
33
- "config_version" . to_owned ( ) ,
34
- toml:: Value :: String ( self . config_version . as_str ( ) . to_owned ( ) ) ,
35
- ) ;
36
- let components = Self :: components_to_toml ( self . components ) ;
37
- if !components. is_empty ( ) {
38
- result. insert ( "components" . to_owned ( ) , toml:: Value :: Array ( components) ) ;
39
- }
40
- result
41
- }
42
-
43
17
pub ( crate ) fn parse ( data : & str ) -> Result < Self > {
44
- let value = toml:: from_str ( data) . context ( "error parsing config" ) ?;
45
- Self :: from_toml ( value, "" )
46
- }
47
-
48
- pub ( crate ) fn stringify ( self ) -> String {
49
- self . into_toml ( ) . to_string ( )
18
+ toml:: from_str ( data) . context ( "error parsing config" )
50
19
}
51
20
52
- fn toml_to_components ( arr : toml:: value:: Array , path : & str ) -> Result < Vec < Component > > {
53
- let mut result = Vec :: new ( ) ;
54
-
55
- for ( i, v) in arr. into_iter ( ) . enumerate ( ) {
56
- if let toml:: Value :: Table ( t) = v {
57
- let path = format ! ( "{path}[{i}]" ) ;
58
- result. push ( Component :: from_toml ( t, & path, false ) ?) ;
59
- }
60
- }
61
-
62
- Ok ( result)
63
- }
64
-
65
- fn components_to_toml ( components : Vec < Component > ) -> toml:: value:: Array {
66
- let mut result = toml:: value:: Array :: new ( ) ;
67
- for v in components {
68
- result. push ( toml:: Value :: Table ( v. into_toml ( ) ) ) ;
69
- }
70
- result
21
+ pub ( crate ) fn stringify ( & self ) -> Result < String > {
22
+ Ok ( toml:: to_string ( & self ) ?)
71
23
}
72
24
73
25
pub ( crate ) fn new ( ) -> Self {
74
26
Default :: default ( )
75
27
}
76
28
}
77
29
78
- #[ derive( Clone , Copy , Debug , Default , Eq , PartialEq ) ]
30
+ #[ derive( Clone , Copy , Debug , Default , Deserialize , Eq , PartialEq , Serialize ) ]
79
31
pub ( crate ) enum ConfigVersion {
32
+ #[ serde( rename = "1" ) ]
80
33
#[ default]
81
34
V1 ,
82
35
}
0 commit comments