Skip to content

Commit 1fb3300

Browse files
committed
Add default sources in config
1 parent b8c119a commit 1fb3300

File tree

1 file changed

+41
-4
lines changed

1 file changed

+41
-4
lines changed

src/config.rs

+41-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::GitProvider;
22
use figment::{
3-
providers::{Format, Toml},
3+
providers::{Format, Serialized, Toml},
44
Figment,
55
};
66
use serde::{Deserialize, Serialize};
@@ -15,15 +15,42 @@ pub struct Config {
1515
sources: Option<Vec<Source>>,
1616
}
1717

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+
1841
impl Config {
1942
/// Load the configuration from a TOML file at the given path.
2043
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()
2247
}
2348

2449
/// Create the configuration from a TOML string.
2550
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()
2754
}
2855

2956
/// Save the configuration.
@@ -114,11 +141,21 @@ mod tests {
114141
]),
115142
local: Some(vec!["jdoe@example.com ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIJHDGMF+tZQL3dcr1arPst+YP8v33Is0kAJVvyTKrxMw".parse().unwrap()]),
116143
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+
},
117154
Source {
118155
name: "acme-corp".to_string(),
119156
provider: GitProvider::Gitlab,
120157
url: "https://git.acme.corp".to_string(),
121-
}
158+
},
122159
])
123160
};
124161

0 commit comments

Comments
 (0)