2020-03-26 19:10:07 +00:00
|
|
|
|
extern crate serde;
|
|
|
|
|
extern crate serde_json;
|
|
|
|
|
extern crate serde_yaml;
|
2020-03-27 17:25:20 +00:00
|
|
|
|
use serde::{Deserialize, Serialize};
|
2020-03-26 19:10:07 +00:00
|
|
|
|
|
|
|
|
|
extern crate log;
|
2020-03-28 21:03:21 +00:00
|
|
|
|
use log::{error, info};
|
2020-03-26 19:10:07 +00:00
|
|
|
|
|
|
|
|
|
// mod utils;
|
|
|
|
|
pub mod utils;
|
2020-03-27 17:25:20 +00:00
|
|
|
|
use utils::SettingsType;
|
2020-03-26 19:10:07 +00:00
|
|
|
|
|
2020-03-27 22:31:24 +00:00
|
|
|
|
/// Current version of the ruleset. It will help determine if the ruleset is
|
|
|
|
|
/// outdated or from a more recent version of the software than the one being in
|
|
|
|
|
/// use.
|
|
|
|
|
pub const RULESET_CURRENT_VERSION: i32 = 1;
|
2020-03-26 19:10:07 +00:00
|
|
|
|
|
2020-03-28 21:03:21 +00:00
|
|
|
|
/// Encode a [`Settings`] struct to a filetype, returns a
|
|
|
|
|
/// `std::result::Result<std::string::String, std::io::Error>`
|
|
|
|
|
///
|
|
|
|
|
/// # Arguments
|
|
|
|
|
///
|
|
|
|
|
/// * `funcrate` - `serde`-compatible crate to use, must implement `to_string`
|
|
|
|
|
/// * `content` - content to encode, must be `Settings` struct
|
|
|
|
|
///
|
|
|
|
|
/// # Example
|
|
|
|
|
///
|
|
|
|
|
/// ```ignore
|
|
|
|
|
/// # use lang_evolve_core::settings::*;
|
|
|
|
|
/// # use lang_evolve_core::encode_settings;
|
|
|
|
|
/// use std::io::{Error, ErrorKind};
|
|
|
|
|
/// use std::path::Path;
|
|
|
|
|
/// let filetype = utils::get_file_type(Path::new("./path/to/file.json"));
|
|
|
|
|
/// let s = Settings::new();
|
|
|
|
|
/// let content = match filetype {
|
|
|
|
|
/// utils::SettingsType::Yaml => encode_settings!(serde_yaml, &s).unwrap(),
|
|
|
|
|
/// utils::SettingsType::Json => encode_settings!(serde_json, &s).unwrap(),
|
|
|
|
|
/// _ => panic!("Could not encode settings"),
|
|
|
|
|
/// };
|
|
|
|
|
/// ```
|
|
|
|
|
///
|
|
|
|
|
/// [`Settings`]: ./settings/struct.Settings.html
|
|
|
|
|
#[macro_export(local_inner_macros)]
|
|
|
|
|
macro_rules! encode_settings {
|
|
|
|
|
($funcrate:ident, $content:expr) => {
|
|
|
|
|
match $funcrate::to_string($content) {
|
|
|
|
|
Err(e) => {
|
|
|
|
|
log::error!("Could not serialize settings: {}", e.to_string());
|
|
|
|
|
return Err(std::io::Error::new(
|
|
|
|
|
std::io::ErrorKind::InvalidData,
|
|
|
|
|
e,
|
|
|
|
|
));
|
|
|
|
|
}
|
|
|
|
|
Ok(val) => val,
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Decode a [`Settings`] struct from a `std::std::String`, returns a
|
|
|
|
|
/// std::result::Result<lang_evolve_core::settings::Settings, std::io::Error>
|
|
|
|
|
///
|
|
|
|
|
/// # Arguments
|
|
|
|
|
///
|
|
|
|
|
/// * `funcrate` - `serde`-compatible crate to use, mus implement `from_string`
|
|
|
|
|
/// * `content` - `&str` to decode into a [`Settings`]
|
|
|
|
|
///
|
|
|
|
|
/// # Example
|
|
|
|
|
///
|
|
|
|
|
/// ```ignore
|
|
|
|
|
/// # use lang_evolve_core::decode_settings;
|
|
|
|
|
/// let str = r#"{"version":"1","categories":[],"rules":[]}"#;
|
|
|
|
|
/// let settings = decode_settings!(serde_json, str);
|
|
|
|
|
/// ```
|
|
|
|
|
///
|
|
|
|
|
/// [`Settings`]: ./settings/struct.Settings.html
|
|
|
|
|
#[macro_export(local_inner_macros)]
|
|
|
|
|
macro_rules! decode_settings {
|
|
|
|
|
($funcrate:ident, $content:expr) => {
|
|
|
|
|
match $funcrate::from_str($content) {
|
|
|
|
|
Err(e) => {
|
|
|
|
|
log::error!("Could not import settings: {}", e.to_string());
|
|
|
|
|
return Err(std::io::Error::new(
|
|
|
|
|
std::io::ErrorKind::InvalidInput,
|
|
|
|
|
e,
|
|
|
|
|
));
|
|
|
|
|
}
|
|
|
|
|
Ok(val) => val,
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-27 17:25:20 +00:00
|
|
|
|
#[derive(Debug, Deserialize, Serialize)]
|
2020-03-26 19:10:07 +00:00
|
|
|
|
pub struct Settings {
|
|
|
|
|
#[serde(default = "Settings::get_ruleset_version")]
|
|
|
|
|
version: String,
|
|
|
|
|
#[serde(default)]
|
|
|
|
|
categories: Vec<(String, String)>,
|
|
|
|
|
#[serde(default)]
|
|
|
|
|
rules: Vec<(String, String)>,
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-27 17:26:31 +00:00
|
|
|
|
/// Representation inside the crate of LangEvolve’s settings.
|
2020-03-26 19:10:07 +00:00
|
|
|
|
impl Settings {
|
2020-03-27 17:26:31 +00:00
|
|
|
|
/// Creates a new empty instance of `Settings`
|
|
|
|
|
///
|
|
|
|
|
/// # Example
|
|
|
|
|
///
|
|
|
|
|
/// ```
|
|
|
|
|
/// let s = lang_evolve_core::settings::Settings::new();
|
|
|
|
|
/// let content_yaml = r#"---
|
|
|
|
|
/// version: "1"
|
|
|
|
|
/// categories: []
|
|
|
|
|
/// rules: []"#;
|
|
|
|
|
/// let content_json = r#"{"version":"1","categories":[],"rules":[]}"#;
|
|
|
|
|
/// assert_eq!(content_yaml, serde_yaml::to_string(&s).unwrap());
|
|
|
|
|
/// assert_eq!(content_json, serde_json::to_string(&s).unwrap());
|
|
|
|
|
/// ```
|
2020-03-26 19:10:07 +00:00
|
|
|
|
pub fn new() -> Self {
|
|
|
|
|
Self {
|
|
|
|
|
version: Self::get_ruleset_version(),
|
|
|
|
|
categories: Vec::new(),
|
|
|
|
|
rules: Vec::new(),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-27 17:26:31 +00:00
|
|
|
|
/// Import settings from an imput file. The currently allowed file formats
|
|
|
|
|
/// are:
|
|
|
|
|
/// - JSON - with the `.json` extension
|
|
|
|
|
/// - Yaml - with the `.yaml` or `.yml` extension
|
|
|
|
|
/// The format will be automatically detected based on the filename
|
|
|
|
|
/// extension.
|
|
|
|
|
///
|
|
|
|
|
/// # Arguments
|
|
|
|
|
///
|
|
|
|
|
/// * `path` - File to open and load settings from
|
|
|
|
|
///
|
|
|
|
|
/// # Example
|
|
|
|
|
///
|
|
|
|
|
/// ```no_run
|
|
|
|
|
/// use std::path::Path;
|
|
|
|
|
/// use lang_evolve_core::settings::Settings;
|
|
|
|
|
/// let path_json = Path::new("settings.json");
|
|
|
|
|
/// let _s_json = Settings::import(&path_json).unwrap();
|
|
|
|
|
///
|
|
|
|
|
/// let path_yaml = Path::new("settings.yaml");
|
|
|
|
|
/// let _s_yaml = Settings::import(&path_yaml).unwrap();
|
|
|
|
|
///
|
|
|
|
|
/// let path_yml = Path::new("settings.yml");
|
|
|
|
|
/// let _s_yml = Settings::import(&path_yml).unwrap();
|
|
|
|
|
/// ```
|
2020-03-27 17:25:20 +00:00
|
|
|
|
pub fn import(path: &std::path::Path) -> std::io::Result<Self> {
|
2020-03-28 21:03:21 +00:00
|
|
|
|
use utils::SettingsType::{Json, Yaml};
|
|
|
|
|
let file_type = utils::get_file_type(&path);
|
|
|
|
|
let content = utils::read_file(&path)?;
|
2020-03-26 19:10:07 +00:00
|
|
|
|
|
2020-03-27 17:25:20 +00:00
|
|
|
|
let settings: Settings = match file_type {
|
2020-03-28 21:03:21 +00:00
|
|
|
|
Yaml => decode_settings!(serde_yaml, &content),
|
|
|
|
|
Json => decode_settings!(serde_json, &content),
|
|
|
|
|
// Attempt to decode anyway
|
|
|
|
|
_ => match Settings::from_str(&content.as_str()) {
|
2020-03-26 19:10:07 +00:00
|
|
|
|
Ok(val) => val,
|
|
|
|
|
Err(e) => {
|
2020-03-28 21:03:21 +00:00
|
|
|
|
error!(
|
|
|
|
|
"Could not decode input {}: {}",
|
|
|
|
|
content,
|
|
|
|
|
e.to_string()
|
|
|
|
|
);
|
2020-03-26 19:10:07 +00:00
|
|
|
|
return Err(std::io::Error::new(
|
2020-03-28 21:03:21 +00:00
|
|
|
|
std::io::ErrorKind::InvalidData,
|
2020-03-26 19:10:07 +00:00
|
|
|
|
e,
|
|
|
|
|
));
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
};
|
2020-03-27 22:26:16 +00:00
|
|
|
|
info!("Successfuly imported {}", path.display());
|
2020-03-26 19:10:07 +00:00
|
|
|
|
Ok(settings)
|
|
|
|
|
}
|
2020-03-27 17:26:31 +00:00
|
|
|
|
|
|
|
|
|
/// Export the current rules to a file. The allowed file formats are either
|
|
|
|
|
/// a YAML file or a Json file, hence the allowed filename extension are:
|
|
|
|
|
/// * "yml" or "yaml" for Yaml files
|
|
|
|
|
/// * "json" for Json files
|
|
|
|
|
/// The format is detected automatically depending on the extension of the
|
|
|
|
|
/// filename.
|
|
|
|
|
///
|
|
|
|
|
/// # Arguments
|
|
|
|
|
///
|
|
|
|
|
/// * `path` - Path to write and export settings to
|
|
|
|
|
///
|
|
|
|
|
/// # Example
|
|
|
|
|
///
|
|
|
|
|
/// ```
|
|
|
|
|
/// use std::path::Path;
|
|
|
|
|
/// let s = lang_evolve_core::settings::Settings::new();
|
|
|
|
|
///
|
|
|
|
|
/// // Export to JSON
|
|
|
|
|
/// let path_json = Path::new("./output.json");
|
|
|
|
|
/// s.export(&path_json).unwrap();
|
|
|
|
|
///
|
|
|
|
|
/// // Export to Yaml, both ".yml" and ".yaml" work
|
|
|
|
|
/// let path_yaml = Path::new("./output.yaml");
|
|
|
|
|
/// s.export(&path_yaml).unwrap();
|
|
|
|
|
/// let path_yml = Path::new("./output.yml");
|
|
|
|
|
/// s.export(&path_yml).unwrap();
|
|
|
|
|
/// ```
|
|
|
|
|
pub fn export(&self, path: &std::path::Path) -> std::io::Result<()> {
|
2020-03-28 21:03:21 +00:00
|
|
|
|
let filetype = utils::get_file_type(&path);
|
2020-03-27 17:26:31 +00:00
|
|
|
|
let content = match filetype {
|
2020-03-28 21:03:21 +00:00
|
|
|
|
SettingsType::Yaml => encode_settings!(serde_yaml, &self),
|
|
|
|
|
SettingsType::Json => encode_settings!(serde_json, &self),
|
|
|
|
|
_ => {
|
|
|
|
|
error!("Unknown filetype {}", path.to_str().unwrap());
|
|
|
|
|
return Err(std::io::Error::new(
|
|
|
|
|
std::io::ErrorKind::InvalidData,
|
|
|
|
|
"Unknown file type",
|
|
|
|
|
));
|
|
|
|
|
}
|
2020-03-27 17:26:31 +00:00
|
|
|
|
};
|
2020-03-27 22:26:16 +00:00
|
|
|
|
info!("Successfuly exported settings to {}", path.display());
|
2020-03-27 17:26:31 +00:00
|
|
|
|
utils::write_file(&path, &content)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Get the current ruleset version of LangEvolve.
|
|
|
|
|
fn get_ruleset_version() -> String {
|
2020-03-27 22:26:16 +00:00
|
|
|
|
RULESET_CURRENT_VERSION.to_string()
|
2020-03-27 17:26:31 +00:00
|
|
|
|
}
|
2020-03-26 19:10:07 +00:00
|
|
|
|
}
|
2020-03-27 17:25:20 +00:00
|
|
|
|
|
2020-03-28 21:03:21 +00:00
|
|
|
|
use std::str::FromStr;
|
|
|
|
|
impl FromStr for Settings {
|
|
|
|
|
type Err = serde_yaml::Error;
|
2020-03-27 17:25:20 +00:00
|
|
|
|
|
2020-03-28 21:03:21 +00:00
|
|
|
|
/// Decode a litteral string into a `Settings` struct. Works only for
|
|
|
|
|
/// supported file types described in `SettingsType`. It will try to decode
|
|
|
|
|
/// the input `s` by any mean known by `SettingsType`.
|
2020-03-27 22:25:16 +00:00
|
|
|
|
///
|
|
|
|
|
/// # Arguments
|
|
|
|
|
///
|
2020-03-28 21:03:21 +00:00
|
|
|
|
/// * `s` - litteral string to decode into a `Settings` struct
|
2020-03-27 22:25:16 +00:00
|
|
|
|
///
|
|
|
|
|
/// # Example
|
|
|
|
|
///
|
|
|
|
|
/// ```
|
2020-03-28 21:03:21 +00:00
|
|
|
|
/// # use std::str::FromStr;
|
|
|
|
|
/// let s = r#"{"version":"1","categories":[],"rules":[]}"#;
|
|
|
|
|
/// let settings = lang_evolve_core::settings::Settings::from_str(s).unwrap();
|
|
|
|
|
/// ```
|
|
|
|
|
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
|
|
|
|
match serde_json::from_str::<Settings>(s) {
|
|
|
|
|
Ok(val) => Ok(val),
|
|
|
|
|
Err(_) => match serde_yaml::from_str::<Settings>(s) {
|
|
|
|
|
Ok(val) => Ok(val),
|
|
|
|
|
Err(e) => {
|
|
|
|
|
error!("Could not decode input {}: {}", s, e.to_string());
|
|
|
|
|
return Err(e);
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
use std::fmt;
|
|
|
|
|
use std::fmt::Display;
|
|
|
|
|
impl Display for Settings {
|
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
|
write!(f, "{}", serde_json::to_string(&self).unwrap())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl PartialEq for Settings {
|
|
|
|
|
fn eq(&self, other: &Self) -> bool {
|
|
|
|
|
self.version == other.version
|
|
|
|
|
&& self.categories == other.categories
|
|
|
|
|
&& self.rules == other.rules
|
2020-03-27 22:25:16 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-27 17:25:20 +00:00
|
|
|
|
impl Eq for Settings {}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn write_settings() {
|
|
|
|
|
let s = Settings::new();
|
|
|
|
|
let path = std::path::Path::new("test.yaml");
|
|
|
|
|
let settings = r#"---
|
|
|
|
|
version: "1"
|
|
|
|
|
categories: []
|
|
|
|
|
rules: []"#;
|
2020-03-27 18:09:34 +00:00
|
|
|
|
utils::write_file(&path, &serde_yaml::to_string(&s).unwrap()).unwrap();
|
2020-03-27 17:25:20 +00:00
|
|
|
|
assert_eq!(settings, utils::read_file(&path).unwrap());
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-27 17:26:31 +00:00
|
|
|
|
#[test]
|
|
|
|
|
fn read_settings() {
|
|
|
|
|
let s1 = Settings::new();
|
|
|
|
|
let path = std::path::Path::new("test.yml");
|
|
|
|
|
s1.export(&path).unwrap();
|
|
|
|
|
let s2 = Settings::import(&path).unwrap();
|
|
|
|
|
assert_eq!(s1, s2);
|
|
|
|
|
}
|