Added some doc and tests

This commit is contained in:
2020-03-27 18:26:31 +01:00
parent 2e0c16e97c
commit 488ceba1bb
5 changed files with 163 additions and 41 deletions

View File

@@ -23,7 +23,22 @@ pub struct Settings {
rules: Vec<(String, String)>,
}
/// Representation inside the crate of LangEvolves settings.
impl Settings {
/// 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());
/// ```
pub fn new() -> Self {
Self {
version: Self::get_ruleset_version(),
@@ -32,39 +47,31 @@ impl Settings {
}
}
fn get_ruleset_version() -> String {
String::from(RULESET_CURRENT_VERSION)
}
pub fn export(&self, path: &std::path::Path) -> std::io::Result<()> {
let filetype = utils::get_file_type(&path).unwrap();
let content = match filetype {
SettingsType::Yaml => match serde_yaml::to_string(&self) {
Err(e) => {
warn!("Could not serialize settings: {}", e.to_string());
return Err(std::io::Error::new(
std::io::ErrorKind::InvalidData,
e,
));
}
Ok(val) => val,
},
SettingsType::Json => match serde_json::to_string(&self) {
Err(e) => {
warn!("Could not serialize settings: {}", e.to_string());
return Err(std::io::Error::new(
std::io::ErrorKind::InvalidData,
e,
));
}
Ok(val) => val,
},
};
utils::write_file(&path, content)
}
#[allow(dead_code)]
/// 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();
/// ```
pub fn import(path: &std::path::Path) -> std::io::Result<Self> {
use SettingsType::*;
let display = path.display();
@@ -102,6 +109,65 @@ impl Settings {
Ok(settings)
}
/// 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<()> {
let filetype = utils::get_file_type(&path).unwrap();
let content = match filetype {
SettingsType::Yaml => match serde_yaml::to_string(&self) {
Err(e) => {
warn!("Could not serialize settings: {}", e.to_string());
return Err(std::io::Error::new(
std::io::ErrorKind::InvalidData,
e,
));
}
Ok(val) => val,
},
SettingsType::Json => match serde_json::to_string(&self) {
Err(e) => {
warn!("Could not serialize settings: {}", e.to_string());
return Err(std::io::Error::new(
std::io::ErrorKind::InvalidData,
e,
));
}
Ok(val) => val,
},
};
utils::write_file(&path, &content)
}
/// Get the current ruleset version of LangEvolve.
fn get_ruleset_version() -> String {
String::from(RULESET_CURRENT_VERSION)
}
}
impl PartialEq for Settings {
@@ -126,3 +192,11 @@ rules: []"#;
assert_eq!(settings, utils::read_file(&path).unwrap());
}
#[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);
}