26 lines
716 B
Rust
26 lines
716 B
Rust
use std::{
|
|
fmt::Debug,
|
|
fs::File,
|
|
io::Read,
|
|
path::Path,
|
|
};
|
|
|
|
use serde::de::DeserializeOwned;
|
|
|
|
pub fn load <
|
|
T: DeserializeOwned,
|
|
P: AsRef <Path> + Debug
|
|
> (
|
|
config_file_path: P
|
|
) -> T {
|
|
let mut f = File::open (&config_file_path).unwrap_or_else (|_| panic! ("Can't open {:?}", config_file_path));
|
|
let mut buffer = vec! [0u8; 4096];
|
|
let bytes_read = f.read (&mut buffer).unwrap_or_else (|_| panic! ("Can't read {:?}", config_file_path));
|
|
buffer.truncate (bytes_read);
|
|
|
|
{
|
|
let config_s = String::from_utf8 (buffer).unwrap_or_else (|_| panic! ("Can't parse {:?} as UTF-8", config_file_path));
|
|
toml::from_str (&config_s).unwrap_or_else (|e| panic! ("Can't parse {:?} as TOML: {}", config_file_path, e))
|
|
}
|
|
}
|