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 --- src/config.rs | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) (limited to 'src/config.rs') 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