1
0
mirror of https://github.com/jht5945/rust_util.git synced 2025-12-28 16:10:05 +08:00

feat: add logger level, read json config

This commit is contained in:
2020-09-09 01:35:44 +08:00
parent 79e2618e98
commit 0576c23c5a
3 changed files with 69 additions and 8 deletions

View File

@@ -118,6 +118,26 @@ pub fn find_parents_exists_dir(dir: &str) -> Option<PathBuf> {
}
}
#[cfg(feature = "use_serde")]
pub fn read_json_config<T>(config: Option<String>, files: &[String]) -> XResult<Option<(PathBuf, T)>> {
let config_path_buf_opt = read_config(config, files);
match config_path_buf_opt {
None => Ok(None),
Some(config_path_buf) => {
information!("Read config: {}", config_path_buf);
let config_content = fs::read_to_string(config_path_buf)?;
Ok(Some((config_path_buf, serde_json::from_str(&config_content)?)))
}
}
}
pub fn read_config(config: Option<String>, files: &[String]) -> Option<PathBuf> {
match config {
Some(config_str) => Some(PathBuf::from(config_str)),
None => locate_file(files),
}
}
pub fn locate_file(files: &[String]) -> Option<PathBuf> {
for f in files {
match PathBuf::from(&resolve_file_path(f)) {