diff options
-rw-r--r-- | fondant_deps/src/lib.rs | 5 | ||||
-rw-r--r-- | 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 { | |||
21 | } | 21 | } |
22 | 22 | ||
23 | use serde::{de::DeserializeOwned, Serialize}; | 23 | use serde::{de::DeserializeOwned, Serialize}; |
24 | use std::path::PathBuf; | ||
25 | |||
24 | #[derive(Debug)] | 26 | #[derive(Debug)] |
25 | /// Errors that `load` and `store` can result in | 27 | /// Errors that `load` and `store` can result in |
26 | pub enum FondantError { | 28 | pub enum FondantError { |
@@ -42,6 +44,9 @@ pub enum FondantError { | |||
42 | 44 | ||
43 | /// Derive this trait on a struct to mark it as a 'configuration' struct. | 45 | /// Derive this trait on a struct to mark it as a 'configuration' struct. |
44 | pub trait Configure: Serialize + DeserializeOwned + Default { | 46 | pub trait Configure: Serialize + DeserializeOwned + Default { |
47 | fn load_file(config_file: &PathBuf) -> Result<Self, FondantError>; | ||
45 | fn load() -> Result<Self, FondantError>; | 48 | fn load() -> Result<Self, FondantError>; |
46 | fn store(&self) -> Result<(), FondantError>; | 49 | fn store(&self) -> Result<(), FondantError>; |
50 | fn store_file(&self, config_file: &PathBuf) -> Result<(), FondantError>; | ||
51 | |||
47 | } | 52 | } |
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 { | |||
124 | impl Configure for #struct_ident { | 124 | impl Configure for #struct_ident { |
125 | fn load() -> Result<#struct_ident, FondantError> { | 125 | fn load() -> Result<#struct_ident, FondantError> { |
126 | #load_paths | 126 | #load_paths |
127 | match File::open(&config_file) { | 127 | Self::load_file(&config_file) |
128 | } | ||
129 | |||
130 | fn load_file(conf_file: &PathBuf) -> Result<#struct_ident, FondantError> { | ||
131 | #load_paths | ||
132 | match File::open(&conf_file) { // Note: conf_file is different than config_file from #load_paths | ||
128 | Ok(mut cfg) => { | 133 | Ok(mut cfg) => { |
129 | let mut cfg_data = String::new(); | 134 | let mut cfg_data = String::new(); |
130 | cfg.read_to_string(&mut cfg_data).unwrap(); | 135 | cfg.read_to_string(&mut cfg_data).unwrap(); |
@@ -146,11 +151,17 @@ fn gen_impl(ast: &DeriveInput, cfg_path: ConfigPath) -> TokenStream { | |||
146 | } | 151 | } |
147 | fn store(&self) -> Result<(), FondantError> { | 152 | fn store(&self) -> Result<(), FondantError> { |
148 | #load_paths | 153 | #load_paths |
154 | &self.store_file(&config_file)?; | ||
155 | Ok(()) | ||
156 | } | ||
157 | |||
158 | fn store_file(&self, conf_file: &PathBuf) -> Result<(), FondantError> { | ||
159 | #load_paths | ||
149 | let mut f = OpenOptions::new() | 160 | let mut f = OpenOptions::new() |
150 | .write(true) | 161 | .write(true) |
151 | .create(true) | 162 | .create(true) |
152 | .truncate(true) | 163 | .truncate(true) |
153 | .open(config_file) | 164 | .open(conf_file) // Note: conf_file is different than config_file from #load_paths |
154 | .map_err(|_| FondantError::FileOpenError)?; | 165 | .map_err(|_| FondantError::FileOpenError)?; |
155 | 166 | ||
156 | let s = #ser::#ser_fn(self).map_err(|_| FondantError::ConfigParseError)?; | 167 | let s = #ser::#ser_fn(self).map_err(|_| FondantError::ConfigParseError)?; |