Skip to content

Commit

Permalink
a few polishes
Browse files Browse the repository at this point in the history
  • Loading branch information
wilyle committed Sep 28, 2023
1 parent c026fc1 commit 45636cd
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 15 deletions.
7 changes: 4 additions & 3 deletions build_common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<P: AsRef<Path>>(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);

Expand Down
24 changes: 12 additions & 12 deletions common/src/config_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<TConfig, TError, TPath, TIoErrorHandler, TDeserializeErrorHandler>(
/// - `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<TConfig, TError, TPath, TIoErrorHandler, TConfigErrorHandler>(
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<TConfig, TError>
where
TConfig: for<'a> Deserialize<'a>,
TPath: AsRef<Path>,
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 <current_dir>/{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,
Expand All @@ -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)
}

0 comments on commit 45636cd

Please sign in to comment.