Skip to content

Commit

Permalink
add support for json config
Browse files Browse the repository at this point in the history
  • Loading branch information
cristicbz committed Jan 3, 2020
1 parent 093b4b2 commit 2014b3a
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 4 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ lazy_static = "1.0"
time = "0.1"
serde = "1.0"
serde_derive = "1.0"
serde_json = "1.0.44"
toml = "0.5"
base64 = "0.11"
regex = "1.0"
Expand Down
14 changes: 10 additions & 4 deletions src/config/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
// Copyright: 2018, Valerian Saliou <valerian@valeriansaliou.name>
// License: Mozilla Public License v2.0 (MPL v2.0)

use serde_json;
use std::fs::File;
use std::io::Read;
use toml;
Expand All @@ -15,16 +16,21 @@ pub struct ConfigReader;

impl ConfigReader {
pub fn make() -> Config {
debug!("reading config file: {}", &APP_ARGS.config);
let config_file = &APP_ARGS.config;
debug!("reading config file: {}", config_file);

let mut file = File::open(&APP_ARGS.config).expect("cannot find config file");
let mut file = File::open(config_file).expect("cannot find config file");
let mut conf = String::new();

file.read_to_string(&mut conf)
.expect("cannot read config file");

debug!("read config file: {}", &APP_ARGS.config);
debug!("read config file: {}", config_file);

toml::from_str(&conf).expect("syntax error in config file")
if config_file.ends_with(".json") {
serde_json::from_str(&conf).expect("syntax error in JSON config file")
} else {
toml::from_str(&conf).expect("syntax error in TOML config file")
}
}
}

0 comments on commit 2014b3a

Please sign in to comment.