From 79f1ea075d0d3ad3514da7120faa1c3c5fd17bfc Mon Sep 17 00:00:00 2001 From: Matt Bradbury Date: Sat, 4 Apr 2020 12:19:24 -0500 Subject: Hack to make issue #1 sort of work --- fondant_deps/src/lib.rs | 5 +++++ fondant_derive/src/lib.rs | 15 +++++++++++++-- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/fondant_deps/src/lib.rs b/fondant_deps/src/lib.rs index 6f62ce8..9559b11 100644 --- a/fondant_deps/src/lib.rs +++ b/fondant_deps/src/lib.rs @@ -21,6 +21,8 @@ pub mod fondant_exports { } use serde::{de::DeserializeOwned, Serialize}; +use std::path::PathBuf; + #[derive(Debug)] /// Errors that `load` and `store` can result in pub enum FondantError { @@ -42,6 +44,9 @@ pub enum FondantError { /// Derive this trait on a struct to mark it as a 'configuration' struct. pub trait Configure: Serialize + DeserializeOwned + Default { + fn load_file(config_file: &PathBuf) -> Result; fn load() -> Result; fn store(&self) -> Result<(), FondantError>; + fn store_file(&self, config_file: &PathBuf) -> Result<(), FondantError>; + } diff --git a/fondant_derive/src/lib.rs b/fondant_derive/src/lib.rs index 62106c4..a06df15 100644 --- a/fondant_derive/src/lib.rs +++ b/fondant_derive/src/lib.rs @@ -124,7 +124,12 @@ fn gen_impl(ast: &DeriveInput, cfg_path: ConfigPath) -> TokenStream { impl Configure for #struct_ident { fn load() -> Result<#struct_ident, FondantError> { #load_paths - match File::open(&config_file) { + Self::load_file(&config_file) + } + + fn load_file(conf_file: &PathBuf) -> Result<#struct_ident, FondantError> { + #load_paths + match File::open(&conf_file) { // Note: conf_file is different than config_file from #load_paths Ok(mut cfg) => { let mut cfg_data = String::new(); cfg.read_to_string(&mut cfg_data).unwrap(); @@ -145,12 +150,18 @@ fn gen_impl(ast: &DeriveInput, cfg_path: ConfigPath) -> TokenStream { }; } fn store(&self) -> Result<(), FondantError> { + #load_paths + &self.store_file(&config_file)?; + Ok(()) + } + + fn store_file(&self, conf_file: &PathBuf) -> Result<(), FondantError> { #load_paths let mut f = OpenOptions::new() .write(true) .create(true) .truncate(true) - .open(config_file) + .open(conf_file) // Note: conf_file is different than config_file from #load_paths .map_err(|_| FondantError::FileOpenError)?; let s = #ser::#ser_fn(self).map_err(|_| FondantError::ConfigParseError)?; -- cgit v1.2.3