From 9d15eb00427cc82823e8ae309aec7c32680ed023 Mon Sep 17 00:00:00 2001 From: rasul Date: Fri, 3 Apr 2020 18:40:45 -0500 Subject: [PATCH] get rid of the file module and do the file stuff from config module --- src/config/config.rs | 31 +++++++++++++++++++++++++++++-- src/file.rs | 43 ------------------------------------------- src/lib.rs | 1 - src/main.rs | 1 - 4 files changed, 29 insertions(+), 47 deletions(-) delete mode 100644 src/file.rs diff --git a/src/config/config.rs b/src/config/config.rs index d26aba4..1cf13a1 100644 --- a/src/config/config.rs +++ b/src/config/config.rs @@ -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>(path: P) -> RudeResult { - 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(), + )) } } diff --git a/src/file.rs b/src/file.rs deleted file mode 100644 index 26eb235..0000000 --- a/src/file.rs +++ /dev/null @@ -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, T: Deserialize<'de>>(path: P) -> RudeResult { - read_file(path, false) -} - -/// The actual function to read a file. -fn read_file<'de, P: Into, T: Deserialize<'de>>(path: P, log: bool) -> RudeResult { - 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, - ) -} diff --git a/src/lib.rs b/src/lib.rs index 8e771c7..15f16e3 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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; diff --git a/src/main.rs b/src/main.rs index a083f8c..0a31317 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,7 +3,6 @@ mod client; mod command; mod config; mod database; -mod file; mod game; mod id; mod logger;