From 0d2611a08449c8596faccf5f8e385980f571bf2d Mon Sep 17 00:00:00 2001 From: necrashter Date: Sat, 23 Apr 2022 13:55:48 +0300 Subject: Implement the ability to read config from yaml --- Cargo.lock | 1 + Cargo.toml | 1 + src/config.rs | 25 +++++++++++++++++++++++++ 3 files changed, 27 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index 5a8a201..c77debd 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -524,6 +524,7 @@ dependencies = [ "serde", "serde_json", "serde_test", + "serde_yaml", "sha2", "tokio", "warp", diff --git a/Cargo.toml b/Cargo.toml index 065cd84..45703ff 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,6 +16,7 @@ log = "0.4.8" log4rs = "1.0.0" parking_lot = "0.10.0" serde_json = "1.0.59" +serde_yaml = "0.8" lazy_static = "1.4.0" blake2 = "0.9.1" hex-literal = "0.3.1" diff --git a/src/config.rs b/src/config.rs index 80d2def..10c054c 100644 --- a/src/config.rs +++ b/src/config.rs @@ -2,6 +2,7 @@ //! //! This module holds the data structures for network configuration. use serde::{Deserialize, Serialize}; +use log::{error, info}; /// Configuration for a single network #[derive(Debug, Serialize, Deserialize, Clone, Default)] @@ -18,3 +19,27 @@ pub struct Config { // Transaction traffic reward pub tx_traffic_reward: u16, } + +impl Config { + pub fn read(filename: &str) -> Option { + let file = match std::fs::File::open(filename) { + Ok(f) => f, + Err(e) => { + error!("Cannot read config file: {}", filename); + error!("Error: {:?}", e); + return None; + }, + }; + let config : Config = match serde_yaml::from_reader(file) { + Ok(c) => c, + Err(e) => { + error!("Cannot parse config file: {}", filename); + error!("Error: {:?}", e); + return None; + }, + }; + // File closes automatically when it goes out of scope. + info!("Config file read successfully: {}", filename); + Some(config) + } +} -- cgit v1.2.3-70-g09d2