1
1
use crate :: GitProvider ;
2
2
use figment:: {
3
- providers:: { Format , Toml } ,
3
+ providers:: { Format , Serialized , Toml } ,
4
4
Figment ,
5
5
} ;
6
6
use serde:: { Deserialize , Serialize } ;
@@ -15,15 +15,42 @@ pub struct Config {
15
15
sources : Option < Vec < Source > > ,
16
16
}
17
17
18
+ impl Default for Config {
19
+ /// The default configuration containing common sources.
20
+ fn default ( ) -> Self {
21
+ Config {
22
+ users : None ,
23
+ organizations : None ,
24
+ local : None ,
25
+ sources : Some ( vec ! [
26
+ Source {
27
+ name: "github" . to_string( ) ,
28
+ provider: GitProvider :: Github ,
29
+ url: "https://api.github.com" . to_string( ) ,
30
+ } ,
31
+ Source {
32
+ name: "gitlab" . to_string( ) ,
33
+ provider: GitProvider :: Gitlab ,
34
+ url: "https://gitlab.com" . to_string( ) ,
35
+ } ,
36
+ ] ) ,
37
+ }
38
+ }
39
+ }
40
+
18
41
impl Config {
19
42
/// Load the configuration from a TOML file at the given path.
20
43
pub fn load ( path : & Path ) -> figment:: Result < Self > {
21
- Figment :: from ( Toml :: file ( path) ) . extract ( )
44
+ Figment :: from ( Serialized :: defaults ( Config :: default ( ) ) )
45
+ . admerge ( Toml :: file ( path) )
46
+ . extract ( )
22
47
}
23
48
24
49
/// Create the configuration from a TOML string.
25
50
fn from_toml ( toml : & str ) -> figment:: Result < Self > {
26
- Figment :: from ( Toml :: string ( toml) ) . extract ( )
51
+ Figment :: from ( Serialized :: defaults ( Config :: default ( ) ) )
52
+ . admerge ( Toml :: string ( toml) )
53
+ . extract ( )
27
54
}
28
55
29
56
/// Save the configuration.
@@ -114,11 +141,21 @@ mod tests {
114
141
] ) ,
115
142
local : Some ( vec ! [ "jdoe@example.com ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIJHDGMF+tZQL3dcr1arPst+YP8v33Is0kAJVvyTKrxMw" . parse( ) . unwrap( ) ] ) ,
116
143
sources : Some ( vec ! [
144
+ Source {
145
+ name: "github" . to_string( ) ,
146
+ provider: GitProvider :: Github ,
147
+ url: "https://api.github.com" . to_string( ) ,
148
+ } ,
149
+ Source {
150
+ name: "gitlab" . to_string( ) ,
151
+ provider: GitProvider :: Gitlab ,
152
+ url: "https://gitlab.com" . to_string( ) ,
153
+ } ,
117
154
Source {
118
155
name: "acme-corp" . to_string( ) ,
119
156
provider: GitProvider :: Gitlab ,
120
157
url: "https://git.acme.corp" . to_string( ) ,
121
- }
158
+ } ,
122
159
] )
123
160
} ;
124
161
0 commit comments