From 00dc25a94fe20ed3f5bfa2223c998f35ca0381fe Mon Sep 17 00:00:00 2001 From: Jedrzej Stuczynski Date: Wed, 29 Jan 2020 16:04:27 +0000 Subject: [PATCH] Definition of NymConfig trait + default methods --- Cargo.lock | 11 +++++++++- Cargo.toml | 1 + common/config/Cargo.toml | 12 +++++++++++ common/config/src/lib.rs | 46 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 69 insertions(+), 1 deletion(-) create mode 100644 common/config/Cargo.toml create mode 100644 common/config/src/lib.rs diff --git a/Cargo.lock b/Cargo.lock index d97c0c9bfb..4b78ab0517 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -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", diff --git a/Cargo.toml b/Cargo.toml index 22e94b3a0a..a5ce1313ac 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,6 +9,7 @@ members = [ "common/clients/provider-client", "common/clients/validator-client", "common/addressing", + "common/config", "common/crypto", "common/healthcheck", "common/pemstore", diff --git a/common/config/Cargo.toml b/common/config/Cargo.toml new file mode 100644 index 0000000000..d955cd93d8 --- /dev/null +++ b/common/config/Cargo.toml @@ -0,0 +1,12 @@ +[package] +name = "config" +version = "0.1.0" +authors = ["Jedrzej Stuczynski "] +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" diff --git a/common/config/src/lib.rs b/common/config/src/lib.rs new file mode 100644 index 0000000000..18ab5b2297 --- /dev/null +++ b/common/config/src/lib.rs @@ -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) -> 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) -> io::Result { + 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 + } +}