diff options
| author | necrashter | 2022-04-23 13:55:48 +0300 |
|---|---|---|
| committer | Yigit Sever | 2022-04-23 18:10:12 +0300 |
| commit | 0d2611a08449c8596faccf5f8e385980f571bf2d (patch) | |
| tree | 89a7ee9d66b16594b42b99d2ea405973312f3225 /src | |
| parent | 052d0a0fd2c6cf6b5cdc965a812ad22238d402e8 (diff) | |
| download | gradecoin-0d2611a08449c8596faccf5f8e385980f571bf2d.tar.gz gradecoin-0d2611a08449c8596faccf5f8e385980f571bf2d.tar.bz2 gradecoin-0d2611a08449c8596faccf5f8e385980f571bf2d.zip | |
Implement the ability to read config from yaml
Diffstat (limited to 'src')
| -rw-r--r-- | src/config.rs | 25 |
1 files changed, 25 insertions, 0 deletions
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 @@ | |||
| 2 | //! | 2 | //! |
| 3 | //! This module holds the data structures for network configuration. | 3 | //! This module holds the data structures for network configuration. |
| 4 | use serde::{Deserialize, Serialize}; | 4 | use serde::{Deserialize, Serialize}; |
| 5 | use log::{error, info}; | ||
| 5 | 6 | ||
| 6 | /// Configuration for a single network | 7 | /// Configuration for a single network |
| 7 | #[derive(Debug, Serialize, Deserialize, Clone, Default)] | 8 | #[derive(Debug, Serialize, Deserialize, Clone, Default)] |
| @@ -18,3 +19,27 @@ pub struct Config { | |||
| 18 | // Transaction traffic reward | 19 | // Transaction traffic reward |
| 19 | pub tx_traffic_reward: u16, | 20 | pub tx_traffic_reward: u16, |
| 20 | } | 21 | } |
| 22 | |||
| 23 | impl Config { | ||
| 24 | pub fn read(filename: &str) -> Option<Self> { | ||
| 25 | let file = match std::fs::File::open(filename) { | ||
| 26 | Ok(f) => f, | ||
| 27 | Err(e) => { | ||
| 28 | error!("Cannot read config file: {}", filename); | ||
| 29 | error!("Error: {:?}", e); | ||
| 30 | return None; | ||
| 31 | }, | ||
| 32 | }; | ||
| 33 | let config : Config = match serde_yaml::from_reader(file) { | ||
| 34 | Ok(c) => c, | ||
| 35 | Err(e) => { | ||
| 36 | error!("Cannot parse config file: {}", filename); | ||
| 37 | error!("Error: {:?}", e); | ||
| 38 | return None; | ||
| 39 | }, | ||
| 40 | }; | ||
| 41 | // File closes automatically when it goes out of scope. | ||
| 42 | info!("Config file read successfully: {}", filename); | ||
| 43 | Some(config) | ||
| 44 | } | ||
| 45 | } | ||
