diff --git a/build_common/src/lib.rs b/build_common/src/lib.rs index 5cb960a7..18c57038 100644 --- a/build_common/src/lib.rs +++ b/build_common/src/lib.rs @@ -4,15 +4,16 @@ use std::{env, fs, path::Path}; +const OUT_DIR: &str = "OUT_DIR"; + /// Copies a file to the build output in `OUT_DIR`. -/// Includes a `cargo:rerun-if-changed` instruction for use in build.rs scripts. +/// Includes a `cargo:rerun-if-changed` instruction for use in `build.rs` scripts. +/// This will likely panic outside of a build script and is not recommended for use in services. /// /// # Arguments: /// - `source_path`: The source file to copy /// - `dest_filename`: The filename for the destination pub fn copy_to_build_out_dir>(source_path: P, dest_filename: &str) { - const OUT_DIR: &str = "OUT_DIR"; - let target_dir = env::var(OUT_DIR).unwrap(); let destination = Path::new(&target_dir).join(dest_filename); diff --git a/common/src/config_utils.rs b/common/src/config_utils.rs index 4d2bd3a6..e5cc26fd 100644 --- a/common/src/config_utils.rs +++ b/common/src/config_utils.rs @@ -21,40 +21,40 @@ const FREYJA_HOME: &str = "FREYJA_HOME"; /// - `config_file_name`: The config file name without an extension. This is used to construct the file names to search for /// - `config_file_ext`: The config file extension. This is used to construct the file names to search for /// - `default_config_path`: The path to the directory containing the default configuration -/// - `io_error_handler`: The error handler for IO errors -/// - `deserialize_error_handler`: The error handler for deserialize errors -pub fn read_from_files( +/// - `io_error_handler`: The error handler for `std::io::Error` errors +/// - `config_error_handler`: The error handler for errors from the config library +pub fn read_from_files( config_file_name: &str, config_file_ext: &str, default_config_path: TPath, io_error_handler: TIoErrorHandler, - deserialize_error_handler: TDeserializeErrorHandler, + config_error_handler: TConfigErrorHandler, ) -> Result where TConfig: for<'a> Deserialize<'a>, TPath: AsRef, TIoErrorHandler: Fn(std::io::Error) -> TError, - TDeserializeErrorHandler: Fn(ConfigError) -> TError, + TConfigErrorHandler: Fn(ConfigError) -> TError, { - let default_config_filename = format!("{}.default.{}", config_file_name, config_file_ext); + let default_config_filename = format!("{config_file_name}.default.{config_file_ext}"); let default_config_file = default_config_path.as_ref().join(default_config_filename); - let overrides_filename = format!("{}.{}", config_file_name, config_file_ext); + let overrides_filename = format!("{config_file_name}.{config_file_ext}"); - // The path below resolves to /{config}.json + // The path below resolves to {current_dir}/{overrides_filename} let current_dir_config_path = env::current_dir() .map_err(&io_error_handler)? .join(overrides_filename.clone()); let freyja_dir_config_path = match env::var(FREYJA_HOME) { Ok(freyja_home) => { - // The path below resolves to $FREYJA_HOME/config/{config}.json + // The path below resolves to $FREYJA_HOME/config/{overrides_filename} Path::new(&freyja_home) .join(CONFIG_DIR) .join(overrides_filename) } Err(_) => { - // The path below resolves to $HOME/.freyja/config/{config}.json + // The path below resolves to $HOME/.freyja/config/{overrides_filename} home_dir() .ok_or(io_error_handler(std::io::Error::new( std::io::ErrorKind::Other, @@ -71,9 +71,9 @@ where .add_source(File::from(current_dir_config_path).required(false)) .add_source(File::from(freyja_dir_config_path).required(false)) .build() - .map_err(&deserialize_error_handler)?; + .map_err(&config_error_handler)?; config_store .try_deserialize() - .map_err(deserialize_error_handler) + .map_err(config_error_handler) }