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

fix: use serde

This commit is contained in:
2020-09-10 00:32:42 +08:00
parent 3db1fb8130
commit 1917b23c27
2 changed files with 4 additions and 5 deletions

View File

@@ -1,6 +1,6 @@
[package] [package]
name = "rust_util" name = "rust_util"
version = "0.6.9" version = "0.6.10"
authors = ["Hatter Jiang <jht5945@gmail.com>"] authors = ["Hatter Jiang <jht5945@gmail.com>"]
edition = "2018" edition = "2018"
description = "Hatter's Rust Util" description = "Hatter's Rust Util"

View File

@@ -119,14 +119,13 @@ pub fn find_parents_exists_dir(dir: &str) -> Option<PathBuf> {
} }
#[cfg(feature = "use_serde")] #[cfg(feature = "use_serde")]
pub fn read_json_config<T>(config: Option<String>, files: &[String]) -> XResult<Option<(PathBuf, T)>> { pub fn read_json_config<T>(config: Option<String>, files: &[String]) -> XResult<Option<(PathBuf, T)>> where T: serde::de::DeserializeOwned {
let config_path_buf_opt = read_config(config, files); let config_path_buf_opt = read_config(config, files);
match config_path_buf_opt { match config_path_buf_opt {
None => Ok(None), None => Ok(None),
Some(config_path_buf) => { Some(config_path_buf) => {
// information!("Read config: {}", config_path_buf); let json_config: T = serde_json::from_reader(std::fs::File::open(&config_path_buf)?)?;
let config_content = fs::read_to_string(config_path_buf)?; Ok(Some((config_path_buf, json_config)))
Ok(Some((config_path_buf, serde_json::from_str(&config_content)?)))
} }
} }
} }