get rid of the file module and do the file stuff from config module

master
rasul 5 years ago
parent ed2a52da4b
commit 9d15eb0042

@ -1,11 +1,13 @@
use std::fs::File;
use std::io::{BufReader, Read};
use std::path::PathBuf;
use serde_derive::Deserialize;
use crate::config::*;
use crate::file;
use crate::id::Id;
use crate::result::RudeResult;
use crate::try_print;
/// Game configuration
#[derive(Clone, Debug, Deserialize)]
@ -26,6 +28,31 @@ pub struct Config {
impl Config {
/// Load and deserialize the toml configuration from the given file.
pub fn load<P: Into<PathBuf>>(path: P) -> RudeResult<Self> {
Ok(file::read_print(&path.into())?)
let path = path.into();
log::debug!("Reading file {}", path.display());
let file: File = try_print!(
File::open(path.clone()),
"Unable to open file: {}",
path.display(),
);
let mut buffer = BufReader::new(file);
let mut file_contents = String::new();
try_print!(
buffer.read_to_string(&mut file_contents),
"Unable to read file: {}",
path.display(),
);
// this Box::leak() may not be great? something about leaking memory?
// i don't know what i'm doing here?
Ok(try_print!(
toml::from_str(Box::leak(file_contents.into_boxed_str())),
"Unable to parse toml: {}",
path.display(),
))
}
}

@ -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,
)
}

@ -5,7 +5,6 @@ pub mod client;
pub mod command;
pub mod config;
pub mod database;
pub mod file;
pub mod game;
pub mod id;
pub mod logger;

@ -3,7 +3,6 @@ mod client;
mod command;
mod config;
mod database;
mod file;
mod game;
mod id;
mod logger;

Loading…
Cancel
Save