-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathconfig.rs
81 lines (62 loc) · 2.17 KB
/
config.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#![allow(unused_imports)]
use std::path::Path;
use serde::{Deserialize, Serialize};
pub use malachitebft_app_channel::app::config::{
ConsensusConfig, LogFormat, LogLevel, LoggingConfig, MetricsConfig, RuntimeConfig,
TimeoutConfig, ValueSyncConfig,
};
use malachitebft_app_channel::app::node::NodeConfig;
/// Malachite configuration options
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
pub struct Config {
/// A custom human-readable name for this node
pub moniker: String,
/// Log configuration options
pub logging: LoggingConfig,
/// Consensus configuration options
pub consensus: ConsensusConfig,
/// ValueSync configuration options
pub value_sync: ValueSyncConfig,
/// Metrics configuration options
pub metrics: MetricsConfig,
/// Runtime configuration options
pub runtime: RuntimeConfig,
}
impl NodeConfig for Config {
fn moniker(&self) -> &str {
&self.moniker
}
fn consensus(&self) -> &ConsensusConfig {
&self.consensus
}
fn value_sync(&self) -> &ValueSyncConfig {
&self.value_sync
}
}
/// load_config parses the environment variables and loads the provided config file path
/// to create a Config struct.
pub fn load_config(path: impl AsRef<Path>, prefix: Option<&str>) -> eyre::Result<Config> {
::config::Config::builder()
.add_source(::config::File::from(path.as_ref()))
.add_source(
::config::Environment::with_prefix(prefix.unwrap_or("MALACHITE")).separator("__"),
)
.build()?
.try_deserialize()
.map_err(Into::into)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn parse_default_config_file() {
let file = include_str!("../config.toml");
let config = toml::from_str::<Config>(file).unwrap();
assert_eq!(config.consensus.timeouts, TimeoutConfig::default());
let tmp_file = std::env::temp_dir().join("config-test.toml");
std::fs::write(&tmp_file, file).unwrap();
let config = load_config(&tmp_file, None).unwrap();
assert_eq!(config.consensus.timeouts, TimeoutConfig::default());
std::fs::remove_file(tmp_file).unwrap();
}
}