1
- use std:: collections:: HashMap ;
2
1
use std:: path:: Path ;
3
2
3
+ use toml:: { Table , Value } ;
4
4
use toybox_vfs:: { Vfs , PathKind } ;
5
5
6
+ // /// Copy or replace values present in `other`
7
+ // pub fn merge_from(&mut self, _other: &Table) {
8
+ // todo!()
9
+ // }
6
10
7
- #[ derive( Debug , Clone , Default ) ]
8
- pub struct Table {
9
- data : HashMap < String , Value > ,
10
- }
11
-
12
-
13
- impl Table {
14
- pub fn new ( ) -> Table {
15
- Table :: default ( )
16
- }
17
-
18
- pub fn from_file ( vfs : & Vfs , kind : PathKind , path : impl AsRef < Path > ) -> anyhow:: Result < Table > {
19
- let data = vfs. load_string ( kind, path) ?;
20
- let raw: toml:: Table = toml:: from_str ( & data) ?;
21
- let _ = dbg ! ( raw) ;
11
+ // /// Recursively remove values from this table that are present in `other`
12
+ // pub fn remove_values_in(&mut self, _other: &Table) {
13
+ // todo!()
14
+ // }
22
15
23
- Ok ( Table :: default ( ) )
24
- }
25
16
26
- pub fn from_cli ( ) -> anyhow:: Result < Table > {
27
- let mut args = std:: env:: args ( ) ;
28
- let _ = args. next ( ) ; // skip first arg
29
17
30
- let _ = dbg ! ( args) ;
31
-
32
- Ok ( Table :: default ( ) )
33
- }
34
-
35
- pub fn save_to_file ( & self , vfs : & Vfs , kind : PathKind , path : impl AsRef < Path > ) -> anyhow:: Result < ( ) > {
36
- let toml = self . to_toml ( ) ;
37
- let string = toml:: to_string_pretty ( & toml) ?;
38
- vfs. save_data ( kind, path, & string)
39
- }
18
+ pub fn load_from_vfs ( vfs : & Vfs , kind : PathKind , path : impl AsRef < Path > ) -> anyhow:: Result < Table > {
19
+ let data = vfs. load_string ( kind, path) ?;
20
+ toml:: from_str ( & data) . map_err ( Into :: into)
21
+ }
40
22
41
- /// Copy or replace values present in `other`
42
- pub fn merge_from ( & mut self , _other : & Table ) {
43
- todo ! ( )
44
- }
23
+ pub fn load_from_cli ( ) -> anyhow:: Result < Table > {
24
+ let mut args = std:: env:: args ( ) ;
25
+ let _ = args. next ( ) ; // skip first arg
45
26
46
- /// Recursively remove values from this table that are present in `other`
47
- pub fn remove_values_in ( & mut self , _other : & Table ) {
48
- todo ! ( )
49
- }
27
+ let mut table = Table :: default ( ) ;
50
28
51
- pub fn get_value ( & self , key : & str ) -> Option < & Value > {
52
- if let Some ( ( key, tail) ) = key. split_once ( '.' ) {
53
- let subtable = self . data . get ( key) ?
54
- . as_table ( ) ?;
29
+ for arg in args {
30
+ let Some ( ( key, value_str) ) = arg. split_once ( '=' ) else {
31
+ log:: warn!( "Failed to parse CLI argument: '{arg}'" ) ;
32
+ continue
33
+ } ;
55
34
56
- subtable. get_value ( tail)
57
- } else {
58
- self . data . get ( key)
59
- }
35
+ set_value ( & mut table, key. trim ( ) , Value :: String ( value_str. trim ( ) . into ( ) ) ) ;
60
36
}
61
37
62
- pub fn set_value ( & mut self , key : & str , value : Value ) {
63
- if let Some ( ( key, tail) ) = key. split_once ( '.' ) {
64
- let subtable = self . data . entry ( key. into ( ) )
65
- . or_insert ( Value :: Table ( Default :: default ( ) ) )
66
- . as_table_mut ( )
67
- . expect ( "Trying to add value to non-table value" ) ;
68
-
69
- subtable. set_value ( tail, value) ;
70
-
71
- } else {
72
- self . data . insert ( key. into ( ) , value) ;
73
- }
74
- }
38
+ Ok ( table)
39
+ }
75
40
76
- fn to_toml ( & self ) -> toml:: Table {
77
- let mut tbl = toml:: Table :: new ( ) ;
41
+ pub fn save_to_vfs ( table : & Table , vfs : & Vfs , kind : PathKind , path : impl AsRef < Path > ) -> anyhow:: Result < ( ) > {
42
+ let string = toml:: to_string_pretty ( table) ?;
43
+ vfs. save_data ( kind, path, & string)
44
+ }
78
45
79
- for ( key, value) in self . data . iter ( ) {
80
- let value = match value {
81
- Value :: String ( string) => toml:: Value :: String ( string. clone ( ) ) ,
82
- Value :: Table ( table) => toml:: Value :: Table ( table. to_toml ( ) ) ,
83
- Value :: Bool ( b) => toml:: Value :: Boolean ( * b) ,
84
- } ;
46
+ pub fn get_value < ' t > ( table : & ' t Table , key : & str ) -> Option < & ' t Value > {
47
+ if let Some ( ( key, tail) ) = key. split_once ( '.' ) {
48
+ let subtable = table. get ( key) ?
49
+ . as_table ( ) ?;
85
50
86
- tbl. insert ( key. clone ( ) , value) ;
87
- }
51
+ get_value ( subtable, tail)
88
52
89
- tbl
53
+ } else {
54
+ table. get ( key)
90
55
}
91
56
}
92
57
58
+ pub fn set_value ( table : & mut Table , key : & str , value : Value ) {
59
+ if let Some ( ( key, tail) ) = key. split_once ( '.' ) {
60
+ let subtable = table. entry ( key)
61
+ . or_insert ( Value :: Table ( Default :: default ( ) ) )
62
+ . as_table_mut ( )
63
+ . expect ( "Trying to add value to non-table value" ) ;
93
64
65
+ set_value ( subtable, tail, value) ;
94
66
95
-
96
- #[ derive( Debug , Clone ) ]
97
- pub enum Value {
98
- String ( String ) ,
99
- Table ( Table ) ,
100
- Bool ( bool ) ,
101
- }
102
-
103
- impl Value {
104
- pub fn as_table ( & self ) -> Option < & Table > {
105
- match self {
106
- Value :: Table ( tbl) => Some ( tbl) ,
107
- _ => None
108
- }
109
- }
110
-
111
- pub fn as_table_mut ( & mut self ) -> Option < & mut Table > {
112
- match self {
113
- Value :: Table ( tbl) => Some ( tbl) ,
114
- _ => None
115
- }
67
+ } else {
68
+ table. insert ( key. into ( ) , value) ;
116
69
}
117
70
}
0 commit comments