aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authornecrashter2022-04-23 13:55:48 +0300
committerYigit Sever2022-04-23 18:10:12 +0300
commit0d2611a08449c8596faccf5f8e385980f571bf2d (patch)
tree89a7ee9d66b16594b42b99d2ea405973312f3225
parent052d0a0fd2c6cf6b5cdc965a812ad22238d402e8 (diff)
downloadgradecoin-0d2611a08449c8596faccf5f8e385980f571bf2d.tar.gz
gradecoin-0d2611a08449c8596faccf5f8e385980f571bf2d.tar.bz2
gradecoin-0d2611a08449c8596faccf5f8e385980f571bf2d.zip
Implement the ability to read config from yaml
-rw-r--r--Cargo.lock1
-rw-r--r--Cargo.toml1
-rw-r--r--src/config.rs25
3 files changed, 27 insertions, 0 deletions
diff --git a/Cargo.lock b/Cargo.lock
index 5a8a201..c77debd 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -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",
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"
16log4rs = "1.0.0" 16log4rs = "1.0.0"
17parking_lot = "0.10.0" 17parking_lot = "0.10.0"
18serde_json = "1.0.59" 18serde_json = "1.0.59"
19serde_yaml = "0.8"
19lazy_static = "1.4.0" 20lazy_static = "1.4.0"
20blake2 = "0.9.1" 21blake2 = "0.9.1"
21hex-literal = "0.3.1" 22hex-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.
4use serde::{Deserialize, Serialize}; 4use serde::{Deserialize, Serialize};
5use 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
23impl 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}