From ff95cb05ebee5ee45ad4f8c6e3cc2d5d4f72e1e9 Mon Sep 17 00:00:00 2001 From: Lucien Cartier-Tilet Date: Thu, 26 Mar 2020 20:10:07 +0100 Subject: [PATCH] Added some basic code Added basic settings representation and a way to load them from either a YAML or Json file. --- Cargo.toml | 5 +++ src/lib.rs | 46 +++++++++++++++++++-- src/settings/mod.rs | 94 +++++++++++++++++++++++++++++++++++++++++++ src/settings/utils.rs | 28 +++++++++++++ src/tests.rs | 4 ++ 5 files changed, 173 insertions(+), 4 deletions(-) create mode 100644 src/settings/mod.rs create mode 100644 src/settings/utils.rs create mode 100644 src/tests.rs diff --git a/Cargo.toml b/Cargo.toml index 543a54d..019ab59 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,3 +7,8 @@ edition = "2018" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +serde = {version = "1.0", features = ["derive"]} +serde_yaml = "0.7" +serde_json = "1.0" +log = "0.4" +simplelog = "0.7" \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs index 31e1bb2..65ef47a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,7 +1,45 @@ +use std::fs::File; +use std::io::Result; +use std::path::PathBuf; + +extern crate log; +extern crate simplelog; +use log::{info, warn}; +use simplelog::*; + #[cfg(test)] -mod tests { - #[test] - fn it_works() { - assert_eq!(2 + 2, 4); +mod tests; + +mod settings; +use settings::utils; + +#[allow(dead_code)] +pub fn init() -> std::result::Result<(), log::SetLoggerError> { + match CombinedLogger::init(vec![ + TermLogger::new( + LevelFilter::Warn, + Config::default(), + TerminalMode::Mixed, + ) + .unwrap(), + WriteLogger::new( + LevelFilter::Info, + Config::default(), + File::create("core.log").unwrap(), + ), + ]) { + Err(why) => { + warn!("Could not initialize logger: {}", why.to_string()); + Err(why) + } + Ok(_) => { + info!("Logger initialized"); + Ok(()) + } } } + +#[allow(dead_code)] +fn import_words(path: PathBuf) -> Result { + utils::read_file(&path) +} diff --git a/src/settings/mod.rs b/src/settings/mod.rs new file mode 100644 index 0000000..ec8a19c --- /dev/null +++ b/src/settings/mod.rs @@ -0,0 +1,94 @@ +extern crate serde; +extern crate serde_json; +extern crate serde_yaml; +use serde::Deserialize; + +extern crate log; +use log::warn; + +// mod utils; +pub mod utils; + +#[allow(dead_code)] +const RULESET_CURRENT_VERSION: &'static str = "1"; + +pub enum SettingsType { + Yaml, + Json, +} + +#[derive(Debug, Deserialize)] +pub struct Settings { + #[serde(default = "Settings::get_ruleset_version")] + version: String, + #[serde(default)] + categories: Vec<(String, String)>, + #[serde(default)] + rules: Vec<(String, String)>, +} + +impl Settings { + #[allow(dead_code)] + pub fn new() -> Self { + Self { + version: Self::get_ruleset_version(), + categories: Vec::new(), + rules: Vec::new(), + } + } + + fn get_ruleset_version() -> String { + String::from(RULESET_CURRENT_VERSION) + } + + #[allow(dead_code)] + pub fn import(path: &std::path::PathBuf) -> std::io::Result { + use SettingsType::*; + let display = path.display(); + let extension = path.extension().unwrap(); + let extension = extension.to_str().unwrap(); + let method = match extension { + "yaml" => Yaml, + "json" => Json, + _ => { + use std::io::{Error, ErrorKind}; + return Err(Error::new( + ErrorKind::InvalidInput, + "File must have \"yaml\" or \"json\" extension", + )); + } + }; + let content = match utils::read_file(&path) { + Err(e) => { + warn!("Could not read file {}: {}", display, e.to_string()); + return Err(e); + } + Ok(content) => content, + }; + + let settings: Settings = match method { + Yaml => match serde_yaml::from_str(&content) { + Err(e) => { + warn!("Could not import settings: {}", e.to_string()); + return Err(std::io::Error::new( + std::io::ErrorKind::InvalidInput, + e, + )); + } + Ok(val) => val, + }, + Json => match serde_json::from_str(&content) { + Err(e) => { + warn!("Could not import settings: {}", e.to_string()); + return Err(std::io::Error::new( + std::io::ErrorKind::InvalidInput, + e, + )); + } + Ok(val) => val, + }, + }; + + Ok(settings) + } +} diff --git a/src/settings/utils.rs b/src/settings/utils.rs new file mode 100644 index 0000000..ec67075 --- /dev/null +++ b/src/settings/utils.rs @@ -0,0 +1,28 @@ +extern crate log; +use log::{info, warn}; + +use std::fs::File; +use std::io::{Read, Result}; +use std::path::PathBuf; + +pub fn read_file(path: &PathBuf) -> Result { + let display = path.display(); + let mut file = match File::open(&path) { + Err(why) => { + warn!("Could not read {}: {}", display, why.to_string()); + return Err(why); + } + Ok(file) => file, + }; + let mut content = String::new(); + match file.read_to_string(&mut content) { + Err(why) => { + warn!("Could not read {}: {}", display, why.to_string()); + return Err(why); + } + Ok(_) => { + info!("Content of {} read", display); + Ok(content) + } + } +} diff --git a/src/tests.rs b/src/tests.rs new file mode 100644 index 0000000..2837a57 --- /dev/null +++ b/src/tests.rs @@ -0,0 +1,4 @@ +#[test] +fn it_works() { + assert_eq!(2 + 2, 4); +}