@@ -2,81 +2,34 @@ use std::fmt;
22use std:: str:: FromStr ;
33
44use anyhow:: { Context , Result } ;
5+ use serde:: { Deserialize , Serialize } ;
56
67use super :: manifest:: Component ;
78use crate :: errors:: * ;
8- use crate :: utils:: toml_utils:: * ;
99
10- #[ derive( Clone , Debug , Default ) ]
10+ #[ derive( Clone , Debug , Default , Deserialize , Serialize ) ]
1111pub struct Config {
1212 pub config_version : ConfigVersion ,
1313 pub components : Vec < Component > ,
1414}
1515
1616impl 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-
4317 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" )
5019 }
5120
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 ) ?)
7123 }
7224
7325 pub ( crate ) fn new ( ) -> Self {
7426 Default :: default ( )
7527 }
7628}
7729
78- #[ derive( Clone , Copy , Debug , Default , Eq , PartialEq ) ]
30+ #[ derive( Clone , Copy , Debug , Default , Deserialize , Eq , PartialEq , Serialize ) ]
7931pub ( crate ) enum ConfigVersion {
32+ #[ serde( rename = "1" ) ]
8033 #[ default]
8134 V1 ,
8235}
0 commit comments