95 lines
2.5 KiB
Rust
95 lines
2.5 KiB
Rust
|
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)
|
||
|
}
|
||
|
}
|