Definition of NymConfig trait + default methods
This commit is contained in:
Generated
+10
-1
@@ -349,6 +349,15 @@ dependencies = [
|
||||
"winapi 0.3.8",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "config"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"handlebars",
|
||||
"serde",
|
||||
"toml",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "constant_time_eq"
|
||||
version = "0.1.5"
|
||||
@@ -1369,13 +1378,13 @@ dependencies = [
|
||||
"bs58",
|
||||
"built",
|
||||
"clap",
|
||||
"config",
|
||||
"crypto",
|
||||
"curve25519-dalek",
|
||||
"directory-client",
|
||||
"dirs",
|
||||
"dotenv",
|
||||
"futures 0.3.1",
|
||||
"handlebars",
|
||||
"healthcheck",
|
||||
"log",
|
||||
"mix-client",
|
||||
|
||||
@@ -9,6 +9,7 @@ members = [
|
||||
"common/clients/provider-client",
|
||||
"common/clients/validator-client",
|
||||
"common/addressing",
|
||||
"common/config",
|
||||
"common/crypto",
|
||||
"common/healthcheck",
|
||||
"common/pemstore",
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
[package]
|
||||
name = "config"
|
||||
version = "0.1.0"
|
||||
authors = ["Jedrzej Stuczynski <andrew@nymtech.net>"]
|
||||
edition = "2018"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
handlebars = "3.0.1"
|
||||
serde = { version = "1.0.104", features = ["derive"] }
|
||||
toml = "0.5.6"
|
||||
@@ -0,0 +1,46 @@
|
||||
use handlebars::Handlebars;
|
||||
use serde::de::DeserializeOwned;
|
||||
use serde::Serialize;
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::{fs, io};
|
||||
|
||||
pub trait NymConfig: Default + Serialize + DeserializeOwned {
|
||||
fn template() -> &'static str;
|
||||
|
||||
fn default_root_directory() -> PathBuf;
|
||||
|
||||
// default, most probable, implementations; can be easily overridden where required
|
||||
fn default_config_directory() -> PathBuf {
|
||||
Self::default_root_directory().join("config")
|
||||
}
|
||||
|
||||
fn default_data_directory() -> PathBuf {
|
||||
Self::default_root_directory().join("data")
|
||||
}
|
||||
|
||||
fn root_directory(&self) -> PathBuf;
|
||||
fn config_directory(&self) -> PathBuf;
|
||||
fn data_directory(&self) -> PathBuf;
|
||||
|
||||
fn save_to_file(&self, custom_location: Option<PathBuf>) -> io::Result<()> {
|
||||
let reg = Handlebars::new();
|
||||
// it's whoever is implementing the trait responsibility to make sure you can execute your own template on your data
|
||||
let templated_config = reg.render_template(Self::template(), self).unwrap();
|
||||
|
||||
fs::write(
|
||||
custom_location.unwrap_or(self.config_directory().join("config.toml")),
|
||||
templated_config,
|
||||
)
|
||||
}
|
||||
|
||||
fn load_from_file(custom_location: Option<PathBuf>) -> io::Result<Self> {
|
||||
let config_contents = fs::read_to_string(
|
||||
custom_location.unwrap_or(Self::default_config_directory().join("config.toml")),
|
||||
)?;
|
||||
|
||||
let parsing_result = toml::from_str(&config_contents)
|
||||
.map_err(|toml_err| io::Error::new(io::ErrorKind::Other, toml_err));
|
||||
|
||||
parsing_result
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user