Added some basic code

Added basic settings representation and a way to load them from either
a YAML or Json file.
This commit is contained in:
Lucien Cartier-Tilet 2020-03-26 20:10:07 +01:00
parent 47100b3476
commit ff95cb05eb
Signed by: phundrak
GPG Key ID: BD7789E705CB8DCA
5 changed files with 173 additions and 4 deletions

View File

@ -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"

View File

@ -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<String> {
utils::read_file(&path)
}

94
src/settings/mod.rs Normal file
View File

@ -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<Self> {
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)
}
}

28
src/settings/utils.rs Normal file
View File

@ -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<String> {
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)
}
}
}

4
src/tests.rs Normal file
View File

@ -0,0 +1,4 @@
#[test]
fn it_works() {
assert_eq!(2 + 2, 4);
}