parent
ed2a52da4b
commit
9d15eb0042
@ -1,43 +0,0 @@
|
||||
use std::fs::File;
|
||||
use std::io::{BufReader, Read};
|
||||
use std::path::PathBuf;
|
||||
|
||||
use log::debug;
|
||||
use serde::de::Deserialize;
|
||||
|
||||
use crate::result::*;
|
||||
|
||||
/// Open a file from the filesystem, read the contents, parse toml and return
|
||||
/// the corresponding toml object. Errors will be printed as well as returned.
|
||||
pub fn read_print<'de, P: Into<PathBuf>, T: Deserialize<'de>>(path: P) -> RudeResult<T> {
|
||||
read_file(path, false)
|
||||
}
|
||||
|
||||
/// The actual function to read a file.
|
||||
fn read_file<'de, P: Into<PathBuf>, T: Deserialize<'de>>(path: P, log: bool) -> RudeResult<T> {
|
||||
let path = path.into();
|
||||
debug!("Reading file {}", path.display());
|
||||
|
||||
let file: File = try_error(
|
||||
File::open(path.clone()),
|
||||
format!("Unable to open file: {}", path.display()),
|
||||
log,
|
||||
)?;
|
||||
|
||||
let mut buffer = BufReader::new(file);
|
||||
let mut file_contents = String::new();
|
||||
|
||||
try_error(
|
||||
buffer.read_to_string(&mut file_contents),
|
||||
format!("Unable to read file: {}", path.display()),
|
||||
log,
|
||||
)?;
|
||||
|
||||
// this Box::leak() may not be great? something about leaking memory?
|
||||
// i don't know what i'm doing here?
|
||||
try_error(
|
||||
toml::from_str(Box::leak(file_contents.into_boxed_str())),
|
||||
format!("Unable to parse toml: {}", path.display()),
|
||||
log,
|
||||
)
|
||||
}
|
Loading…
Reference in new issue