diff options
| -rw-r--r-- | Cargo.lock | 1 | ||||
| -rw-r--r-- | Cargo.toml | 1 | ||||
| -rw-r--r-- | src/config.rs | 25 |
3 files changed, 27 insertions, 0 deletions
| @@ -524,6 +524,7 @@ dependencies = [ | |||
| 524 | "serde", | 524 | "serde", |
| 525 | "serde_json", | 525 | "serde_json", |
| 526 | "serde_test", | 526 | "serde_test", |
| 527 | "serde_yaml", | ||
| 527 | "sha2", | 528 | "sha2", |
| 528 | "tokio", | 529 | "tokio", |
| 529 | "warp", | 530 | "warp", |
| @@ -16,6 +16,7 @@ log = "0.4.8" | |||
| 16 | log4rs = "1.0.0" | 16 | log4rs = "1.0.0" |
| 17 | parking_lot = "0.10.0" | 17 | parking_lot = "0.10.0" |
| 18 | serde_json = "1.0.59" | 18 | serde_json = "1.0.59" |
| 19 | serde_yaml = "0.8" | ||
| 19 | lazy_static = "1.4.0" | 20 | lazy_static = "1.4.0" |
| 20 | blake2 = "0.9.1" | 21 | blake2 = "0.9.1" |
| 21 | hex-literal = "0.3.1" | 22 | 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 @@ | |||
| 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 | } | ||
