1use lazy_static::lazy_static;
3use serde::{Deserialize, Serialize};
4use std::fs;
5use toml;
6use trustchain_core::TRUSTCHAIN_CONFIG;
7use trustchain_ion::Endpoint;
8
9lazy_static! {
10 pub static ref CLI_CONFIG: CLIConfig = parse_toml(
12 &fs::read_to_string(std::env::var(TRUSTCHAIN_CONFIG).unwrap().as_str())
13 .expect("Error reading trustchain_config.toml"));
14}
15
16fn parse_toml(toml_str: &str) -> CLIConfig {
18 toml::from_str::<Config>(toml_str)
19 .expect("Error parsing trustchain_config.toml")
20 .cli
21}
22
23pub fn cli_config() -> &'static CLI_CONFIG {
25 &CLI_CONFIG
26}
27
28#[derive(Serialize, Deserialize, PartialEq, Debug)]
30pub struct CLIConfig {
31 pub root_event_time: u32,
33 pub ion_endpoint: Endpoint,
34}
35
36#[derive(Serialize, Deserialize, PartialEq, Debug)]
38struct Config {
39 cli: CLIConfig,
41}
42
43#[cfg(test)]
44mod tests {
45 use super::*;
46
47 #[test]
48 fn test_deserialize() {
49 let config_string = r#"
50 [cli]
51 root_event_time = 1666971942
52 ion_endpoint.host = "http://127.0.0.1"
53 ion_endpoint.port = 3000
54
55 [non_core]
56 key = "value"
57 "#;
58
59 let config: CLIConfig = parse_toml(config_string);
60
61 assert_eq!(
62 config,
63 CLIConfig {
64 root_event_time: 1666971942,
65 ion_endpoint: Endpoint::new("http://127.0.0.1".to_string(), 3000)
66 }
67 );
68 }
69}