Compare commits
35 Commits
ff95cb05eb
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
531c9b1acc
|
|||
|
cb933d4896
|
|||
|
d33190a46d
|
|||
|
bb9d8703f3
|
|||
|
144771a2c0
|
|||
|
22fbe5aba7
|
|||
|
9cc1a52e5a
|
|||
|
78541f10ba
|
|||
|
8ddd33d76d
|
|||
|
a8bafc072b
|
|||
|
3c960eaa35
|
|||
|
de45ffc15c
|
|||
|
042fd066f0
|
|||
|
071c8b0728
|
|||
|
5f30c6d636
|
|||
|
dbbb1616dd
|
|||
|
23e3acb182
|
|||
|
6fb1c287e0
|
|||
|
ef8c02fc97
|
|||
|
bae1d86544
|
|||
|
6be0f7e8f6
|
|||
|
d13836e433
|
|||
|
f3e672e29c
|
|||
|
c418323b5c
|
|||
|
c6baa46aca
|
|||
|
18eb16e777
|
|||
|
d200367ee0
|
|||
|
e9a161f526
|
|||
|
a7ef031090
|
|||
|
9f5c040893
|
|||
|
7d0a371311
|
|||
|
e00f489f55
|
|||
|
8642067eb3
|
|||
|
488ceba1bb
|
|||
|
2e0c16e97c
|
4
.gitignore
vendored
4
.gitignore
vendored
@@ -1,2 +1,6 @@
|
|||||||
/target
|
/target
|
||||||
Cargo.lock
|
Cargo.lock
|
||||||
|
*.log
|
||||||
|
*.yaml
|
||||||
|
*.yml
|
||||||
|
*.json
|
||||||
|
|||||||
17
Cargo.toml
17
Cargo.toml
@@ -7,8 +7,17 @@ edition = "2018"
|
|||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
serde = {version = "1.0", features = ["derive"]}
|
# Logger
|
||||||
serde_yaml = "0.7"
|
|
||||||
serde_json = "1.0"
|
|
||||||
log = "0.4"
|
log = "0.4"
|
||||||
simplelog = "0.7"
|
simplelog = "0.8"
|
||||||
|
# Struct serializing and deserializing
|
||||||
|
serde = {version = "1.0", features = ["derive"]}
|
||||||
|
serde_yaml = "0.8"
|
||||||
|
serde_json = "1.0"
|
||||||
|
# Regex support
|
||||||
|
regex = "1.3"
|
||||||
|
lazy_static = "1.4"
|
||||||
|
|
||||||
|
# [dev-dependencies]
|
||||||
|
# Pretty output
|
||||||
|
prettydiff = "0.3"
|
||||||
|
|||||||
107
src/lib.rs
107
src/lib.rs
@@ -1,33 +1,79 @@
|
|||||||
use std::fs::File;
|
#![crate_name = "lang_evolve_core"]
|
||||||
use std::io::Result;
|
#![deny(clippy::all)]
|
||||||
use std::path::PathBuf;
|
|
||||||
|
//! # LangEvolveCore
|
||||||
|
//!
|
||||||
|
//! `lang_evolve_core` is the core crate used by two other crates:
|
||||||
|
//! `lang_evolve_cli` and `lang_evolve_gui`.
|
||||||
|
//!
|
||||||
|
//! # What it does
|
||||||
|
//!
|
||||||
|
//! LangEvolveCore is the crate that hosts all the core logic behind LangEvolve,
|
||||||
|
//! a conlanging software developped after the
|
||||||
|
//! [original software](https://github.com/ceronyon/LangEvolve) which applies
|
||||||
|
//! user-defined sound changes to words and texts based on regex expressions.
|
||||||
|
|
||||||
|
use std::fs::File;
|
||||||
|
|
||||||
extern crate log;
|
|
||||||
extern crate simplelog;
|
|
||||||
use log::{info, warn};
|
use log::{info, warn};
|
||||||
use simplelog::*;
|
use simplelog::*;
|
||||||
|
|
||||||
#[cfg(test)]
|
pub mod settings;
|
||||||
mod tests;
|
pub mod utils;
|
||||||
|
|
||||||
mod settings;
|
/// Initializes the crate
|
||||||
use settings::utils;
|
///
|
||||||
|
/// # What it does
|
||||||
#[allow(dead_code)]
|
///
|
||||||
|
/// Initializing the crate allows to initialize its logging system. All its logs
|
||||||
|
/// will be written to the file `core.log` while the errors will also be shown
|
||||||
|
/// in the terminal.
|
||||||
|
///
|
||||||
|
/// # Return type
|
||||||
|
///
|
||||||
|
/// This function returns a null object if it succeeds. However, if it does not,
|
||||||
|
/// it will send a `log::SetLoggerError` back.
|
||||||
|
///
|
||||||
|
/// # Example usage
|
||||||
|
///
|
||||||
|
/// Its usage is extremely simple, just call it before you do anything with it:
|
||||||
|
/// ```
|
||||||
|
/// lang_evolve_core::init();
|
||||||
|
/// ```
|
||||||
pub fn init() -> std::result::Result<(), log::SetLoggerError> {
|
pub fn init() -> std::result::Result<(), log::SetLoggerError> {
|
||||||
match CombinedLogger::init(vec![
|
// #[cfg(debug_assertions)]
|
||||||
TermLogger::new(
|
match CombinedLogger::init(if cfg!(debug_assertions) {
|
||||||
|
vec![
|
||||||
|
WriteLogger::new(
|
||||||
LevelFilter::Warn,
|
LevelFilter::Warn,
|
||||||
Config::default(),
|
Config::default(),
|
||||||
TerminalMode::Mixed,
|
File::create("core.log").unwrap(),
|
||||||
)
|
),
|
||||||
.unwrap(),
|
WriteLogger::new(
|
||||||
|
LevelFilter::Debug,
|
||||||
|
Config::default(),
|
||||||
|
File::create("core.log").unwrap(),
|
||||||
|
),
|
||||||
WriteLogger::new(
|
WriteLogger::new(
|
||||||
LevelFilter::Info,
|
LevelFilter::Info,
|
||||||
Config::default(),
|
Config::default(),
|
||||||
File::create("core.log").unwrap(),
|
File::create("core.log").unwrap(),
|
||||||
),
|
),
|
||||||
]) {
|
]
|
||||||
|
} else {
|
||||||
|
vec![
|
||||||
|
WriteLogger::new(
|
||||||
|
LevelFilter::Warn,
|
||||||
|
Config::default(),
|
||||||
|
File::create("core.log").unwrap(),
|
||||||
|
),
|
||||||
|
WriteLogger::new(
|
||||||
|
LevelFilter::Info,
|
||||||
|
Config::default(),
|
||||||
|
File::create("core.log").unwrap(),
|
||||||
|
),
|
||||||
|
]
|
||||||
|
}) {
|
||||||
Err(why) => {
|
Err(why) => {
|
||||||
warn!("Could not initialize logger: {}", why.to_string());
|
warn!("Could not initialize logger: {}", why.to_string());
|
||||||
Err(why)
|
Err(why)
|
||||||
@@ -39,7 +85,28 @@ pub fn init() -> std::result::Result<(), log::SetLoggerError> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[allow(dead_code)]
|
#[cfg(test)]
|
||||||
fn import_words(path: PathBuf) -> Result<String> {
|
mod tests {
|
||||||
utils::read_file(&path)
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn write_settings() {
|
||||||
|
let s = settings::Settings::new();
|
||||||
|
let path = std::path::Path::new("settings.yaml");
|
||||||
|
let settings = r#"---
|
||||||
|
version: "1"
|
||||||
|
categories: {}
|
||||||
|
rules: []"#;
|
||||||
|
utils::write_file(&path, &serde_yaml::to_string(&s).unwrap()).unwrap();
|
||||||
|
assert_eq!(settings, utils::read_file(&path).unwrap());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn read_settings() {
|
||||||
|
let s1 = settings::Settings::new();
|
||||||
|
let path = std::path::Path::new("settings.yml");
|
||||||
|
s1.export(&path).unwrap();
|
||||||
|
let s2 = settings::Settings::import(&path).unwrap();
|
||||||
|
assert_eq!(s1, s2);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,94 +1,479 @@
|
|||||||
extern crate serde;
|
use serde::{Deserialize, Serialize};
|
||||||
extern crate serde_json;
|
|
||||||
extern crate serde_yaml;
|
|
||||||
use serde::Deserialize;
|
|
||||||
|
|
||||||
extern crate log;
|
use log::{debug, error, info};
|
||||||
use log::warn;
|
|
||||||
|
|
||||||
// mod utils;
|
use crate::utils::{self, SettingsType};
|
||||||
pub mod utils;
|
|
||||||
|
|
||||||
#[allow(dead_code)]
|
use prettydiff::diff_words;
|
||||||
const RULESET_CURRENT_VERSION: &'static str = "1";
|
|
||||||
|
|
||||||
pub enum SettingsType {
|
mod rule;
|
||||||
Yaml,
|
use rule::Rule;
|
||||||
Json,
|
|
||||||
|
/// 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.
|
||||||
|
const RULESET_CURRENT_VERSION: i32 = 1;
|
||||||
|
|
||||||
|
/// 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
|
||||||
|
///
|
||||||
|
/// ```no_run
|
||||||
|
/// use std::path::Path;
|
||||||
|
/// use lang_evolve_core::utils;
|
||||||
|
///
|
||||||
|
/// let settings = Settings::new();
|
||||||
|
/// let filetype = utils::get_file_type(Path::new("settings.yml"));
|
||||||
|
///
|
||||||
|
/// let content = match filetype {
|
||||||
|
/// SettingsType::Yaml => encode_settings!(serde_yaml, &settings),
|
||||||
|
/// SettingsType::Json => encode_settings!(serde_json, &settings),
|
||||||
|
/// _ => String::from("Error!"),
|
||||||
|
/// };
|
||||||
|
/// ```
|
||||||
|
///
|
||||||
|
/// [`Settings`]: ./settings/struct.Settings.html
|
||||||
|
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,
|
||||||
|
}
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Deserialize)]
|
/// 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
|
||||||
|
///
|
||||||
|
/// ```no_run
|
||||||
|
/// # 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_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,
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
use std::collections::HashMap;
|
||||||
|
|
||||||
|
/// Representation of the software’s settings
|
||||||
|
///
|
||||||
|
/// This struct represents all the settings the software has to follow
|
||||||
|
/// while running, which includes the phoneme categories as well as
|
||||||
|
/// the soundchange rules to apply to the input text.
|
||||||
|
#[derive(Clone, Debug, Deserialize, Serialize)]
|
||||||
pub struct Settings {
|
pub struct Settings {
|
||||||
|
/// Represents the version of the loaded ruleset.
|
||||||
|
///
|
||||||
|
/// It is used to detect obsolete ruleset representations or if a
|
||||||
|
/// loaded ruleset comes from a newer version of lang_evolve_core
|
||||||
|
/// than the one used by the user.
|
||||||
#[serde(default = "Settings::get_ruleset_version")]
|
#[serde(default = "Settings::get_ruleset_version")]
|
||||||
version: String,
|
version: String,
|
||||||
|
|
||||||
|
/// Categories of phonemes
|
||||||
|
///
|
||||||
|
/// This is a vector of categories of phonemes, represented
|
||||||
|
/// themselves as pairs of strings. Each pair of strings has its
|
||||||
|
/// first element represent the name of the category, which is
|
||||||
|
/// generally represented by a single capital letter. The second
|
||||||
|
/// element is a string where all its characters represent
|
||||||
|
/// phonemes. It is currently not possible to have more than one
|
||||||
|
/// character to be considered as one sound.
|
||||||
#[serde(default)]
|
#[serde(default)]
|
||||||
categories: Vec<(String, String)>,
|
categories: HashMap<String, String>,
|
||||||
|
|
||||||
|
/// Soundchange rules
|
||||||
|
///
|
||||||
|
/// This is a vector of pairs of strings, the first one represents
|
||||||
|
/// a regex to be matched while the second represents the change
|
||||||
|
/// to be made to the input data.
|
||||||
#[serde(default)]
|
#[serde(default)]
|
||||||
rules: Vec<(String, String)>,
|
rules: Vec<Rule>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Representation inside the crate of LangEvolve’s settings.
|
||||||
impl Settings {
|
impl Settings {
|
||||||
#[allow(dead_code)]
|
/// 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());
|
||||||
|
/// ```
|
||||||
|
///
|
||||||
|
/// [`Settings`]: ./settings/struct.Settings.html
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
Self {
|
Self {
|
||||||
version: Self::get_ruleset_version(),
|
version: Self::get_ruleset_version(),
|
||||||
categories: Vec::new(),
|
categories: HashMap::new(),
|
||||||
rules: Vec::new(),
|
rules: Vec::new(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_ruleset_version() -> String {
|
/// Import settings from an imput file.
|
||||||
String::from(RULESET_CURRENT_VERSION)
|
///
|
||||||
}
|
/// The currently allowed file formats are described in the
|
||||||
|
/// [`utils::SettingsType`] enum. If the ruleset version is higher than the
|
||||||
#[allow(dead_code)]
|
/// current version (see [`Settings.version`]), then an error is returned.
|
||||||
pub fn import(path: &std::path::PathBuf) -> std::io::Result<Self> {
|
///
|
||||||
use SettingsType::*;
|
/// # Arguments
|
||||||
let display = path.display();
|
///
|
||||||
let extension = path.extension().unwrap();
|
/// * `path` - File to open and load settings from
|
||||||
let extension = extension.to_str().unwrap();
|
///
|
||||||
let method = match extension {
|
/// # Example
|
||||||
"yaml" => Yaml,
|
///
|
||||||
"json" => Json,
|
/// ```
|
||||||
_ => {
|
/// use std::path::Path;
|
||||||
use std::io::{Error, ErrorKind};
|
/// # use lang_evolve_core::settings::Settings;
|
||||||
return Err(Error::new(
|
/// # let s = Settings::new();
|
||||||
ErrorKind::InvalidInput,
|
/// # for path in vec!["settings.json", "settings.yaml", "settings.yml"] {
|
||||||
"File must have \"yaml\" or \"json\" extension",
|
/// # let path = Path::new(path);
|
||||||
));
|
/// # s.export(&path).unwrap();
|
||||||
}
|
/// # }
|
||||||
};
|
///
|
||||||
let content = match utils::read_file(&path) {
|
/// 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();
|
||||||
|
/// ```
|
||||||
|
///
|
||||||
|
/// [`utils::SettingsTYpe`]: ./utils/enum.SettingsType.html
|
||||||
|
/// [`Settings.version`]: ./struct.Settings.html#structfield.version
|
||||||
|
pub fn import(path: &std::path::Path) -> std::io::Result<Self> {
|
||||||
|
use SettingsType::{Json, Yaml};
|
||||||
|
let file_type = utils::get_file_type(&path);
|
||||||
|
let content = utils::read_file(&path)?;
|
||||||
|
let settings: Settings = match file_type {
|
||||||
|
Yaml => decode_settings!(serde_yaml, &content),
|
||||||
|
Json => decode_settings!(serde_json, &content),
|
||||||
|
// Attempt to decode anyway
|
||||||
|
_ => match Settings::from_str(&content.as_str()) {
|
||||||
|
Ok(val) => val,
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
warn!("Could not read file {}: {}", display, e.to_string());
|
error!(
|
||||||
return Err(e);
|
"Could not decode input {}: {}",
|
||||||
}
|
content,
|
||||||
Ok(content) => content,
|
e.to_string()
|
||||||
};
|
);
|
||||||
|
|
||||||
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(
|
return Err(std::io::Error::new(
|
||||||
std::io::ErrorKind::InvalidInput,
|
std::io::ErrorKind::InvalidData,
|
||||||
e,
|
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,
|
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
if settings.version > Self::get_ruleset_version() {
|
||||||
|
error!("Ruleset version too high!");
|
||||||
|
Err(std::io::Error::new(
|
||||||
|
std::io::ErrorKind::InvalidData,
|
||||||
|
"Ruleset version too high!",
|
||||||
|
))
|
||||||
|
} else {
|
||||||
|
info!("Successfuly imported {}", path.display());
|
||||||
Ok(settings)
|
Ok(settings)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Import settings from file path described by the argument `path`
|
||||||
|
///
|
||||||
|
/// # Arguments
|
||||||
|
///
|
||||||
|
/// * `path` - path to the file from which settings should be imported
|
||||||
|
///
|
||||||
|
/// # Example
|
||||||
|
///
|
||||||
|
/// ```
|
||||||
|
/// # use lang_evolve_core::settings::Settings;
|
||||||
|
/// # use std::path::Path;
|
||||||
|
/// # let s = Settings::default();
|
||||||
|
/// # s.export(Path::new("settings.yml"));
|
||||||
|
/// let s = Settings::from("settings.yml");
|
||||||
|
/// ```
|
||||||
|
pub fn from<S>(s: S) -> std::io::Result<Self>
|
||||||
|
where
|
||||||
|
S: ToString,
|
||||||
|
{
|
||||||
|
let s = s.to_string();
|
||||||
|
let path = std::path::Path::new(&s);
|
||||||
|
Self::import(&path)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Add a new rule to the current settings
|
||||||
|
///
|
||||||
|
/// # Arguments
|
||||||
|
///
|
||||||
|
/// * `from` - Regex that should match the text to be replaced
|
||||||
|
/// * `to` - Regex that should replace some text
|
||||||
|
///
|
||||||
|
/// # Example
|
||||||
|
///
|
||||||
|
/// ```
|
||||||
|
/// # use lang_evolve_core::settings::Settings;
|
||||||
|
/// let mut settings = Settings::default();
|
||||||
|
/// settings.add_rule("ha", "wa");
|
||||||
|
///
|
||||||
|
/// use std::str::FromStr;
|
||||||
|
/// let reference = Settings::from_str(
|
||||||
|
/// r#"{"version":"1","categories":{},"rules":[{"from":"ha","to":"wa"}]}"#)
|
||||||
|
/// .unwrap();
|
||||||
|
/// assert_eq!(reference, settings);
|
||||||
|
/// ```
|
||||||
|
pub fn add_rule(&mut self, from: &str, to: &str) {
|
||||||
|
self.rules.push(Rule::new(from, to))
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Add a new category of phonemes to the current settings
|
||||||
|
///
|
||||||
|
/// # Arguments
|
||||||
|
///
|
||||||
|
/// * `name` - Name of the category
|
||||||
|
/// * `content` - Content of the category, phonemes
|
||||||
|
///
|
||||||
|
/// # Example
|
||||||
|
///
|
||||||
|
/// ```
|
||||||
|
/// # use lang_evolve_core::settings::Settings;
|
||||||
|
/// let mut settings = Settings::default();
|
||||||
|
/// settings.add_category("C", "abcde");
|
||||||
|
///
|
||||||
|
/// use std::str::FromStr;
|
||||||
|
/// let reference = Settings::from_str(
|
||||||
|
/// r#"{"version":"1","categories":{"C": "abcde"},"rules":[]}"#)
|
||||||
|
/// .unwrap();
|
||||||
|
/// assert_eq!(reference, settings);
|
||||||
|
/// ```
|
||||||
|
pub fn add_category(&mut self, name: &str, content: &str) {
|
||||||
|
self.categories.insert(String::from(name), String::from(content));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Export current settings to a file.
|
||||||
|
///
|
||||||
|
/// The allowed file formats are described in the [`SettingsType`] enum.
|
||||||
|
///
|
||||||
|
/// # Arguments
|
||||||
|
///
|
||||||
|
/// * `path` - Path to write and export settings to
|
||||||
|
///
|
||||||
|
/// # Example
|
||||||
|
///
|
||||||
|
/// ```
|
||||||
|
/// # use lang_evolve_core::settings::Settings;
|
||||||
|
/// use std::path::Path;
|
||||||
|
///
|
||||||
|
/// let s = Settings::new();
|
||||||
|
///
|
||||||
|
/// // Export to JSON
|
||||||
|
/// let path_json = Path::new("settings.json");
|
||||||
|
/// s.export(&path_json).unwrap();
|
||||||
|
///
|
||||||
|
/// // Export to Yaml, both ".yml" and ".yaml" work
|
||||||
|
/// let path_yaml = Path::new("settings.yaml");
|
||||||
|
/// s.export(&path_yaml).unwrap();
|
||||||
|
/// let path_yml = Path::new("settings.yml");
|
||||||
|
/// s.export(&path_yml).unwrap();
|
||||||
|
/// ```
|
||||||
|
///
|
||||||
|
/// [`SettingsType`]: ./utils/enum.SettingsType.html
|
||||||
|
pub fn export(&self, path: &std::path::Path) -> std::io::Result<()> {
|
||||||
|
let filetype = utils::get_file_type(&path);
|
||||||
|
let content = match filetype {
|
||||||
|
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",
|
||||||
|
));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
info!("Successfuly exported settings to {}", path.display());
|
||||||
|
utils::write_file(&path, &content)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Get the current ruleset version of LangEvolve.
|
||||||
|
fn get_ruleset_version() -> String {
|
||||||
|
RULESET_CURRENT_VERSION.to_string()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Transform input rules into Regexes that can be understood by Rust.
|
||||||
|
fn update_rules(&self) -> std::result::Result<Vec<Rule>, String> {
|
||||||
|
let rules = self.rules.clone();
|
||||||
|
let rules: Vec<Rule> = rules
|
||||||
|
.iter()
|
||||||
|
.map(|rule| rule.update(&self.categories).unwrap())
|
||||||
|
.collect();
|
||||||
|
Ok(rules)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Apply list of rules to input
|
||||||
|
///
|
||||||
|
/// The list of rules in the struct will be applied to the input `new`. If the
|
||||||
|
/// rule contains the `%` character followed by a capital letter, this marks
|
||||||
|
/// a category of phonemes and should be replaced by them. For instance, we
|
||||||
|
/// have:
|
||||||
|
/// - the category `C` defined as `bcdfg`
|
||||||
|
/// - the rule `%Ci` to `%Cj`
|
||||||
|
/// The rule should be rewritten as `[bcdfg]` to `[bcdfg]j`
|
||||||
|
///
|
||||||
|
/// # Arguments
|
||||||
|
///
|
||||||
|
/// * `new` - Input to modify
|
||||||
|
///
|
||||||
|
/// # Example
|
||||||
|
///
|
||||||
|
/// ```
|
||||||
|
/// # use lang_evolve_core::settings::Settings;
|
||||||
|
/// let settings = Settings::new();
|
||||||
|
/// // add some rules...
|
||||||
|
/// // set some input
|
||||||
|
/// let input = String::new();
|
||||||
|
/// let _output = settings.apply(input);
|
||||||
|
/// ```
|
||||||
|
pub fn apply(&self, s: String) -> std::result::Result<String, String> {
|
||||||
|
// TODO Add Error handling
|
||||||
|
let rules = self.update_rules().unwrap();
|
||||||
|
let mut s = s;
|
||||||
|
debug!("===============================================");
|
||||||
|
for rule in rules {
|
||||||
|
debug!(
|
||||||
|
"from: \"{}\"\tto: \"{}\"",
|
||||||
|
rule.get_from().to_string(),
|
||||||
|
rule.get_to()
|
||||||
|
);
|
||||||
|
let old = s.clone();
|
||||||
|
let new = rule
|
||||||
|
.get_from()
|
||||||
|
.replace_all(&s, rule.get_to().as_str())
|
||||||
|
.to_string();
|
||||||
|
if cfg!(debug_assertions) {
|
||||||
|
let diffs = diff_words(&old, &new);
|
||||||
|
if diffs.diff().len() > 1 {
|
||||||
|
debug!("diff:\n{}", diff_words(&old, &new));
|
||||||
|
} else {
|
||||||
|
debug!("diff: No changes");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
s = new;
|
||||||
|
}
|
||||||
|
Ok(s)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Default for Settings {
|
||||||
|
/// Creates a new empty instance of [`Settings`]
|
||||||
|
///
|
||||||
|
/// # Example
|
||||||
|
///
|
||||||
|
/// ```
|
||||||
|
/// let s = lang_evolve_core::settings::Settings::default();
|
||||||
|
/// 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());
|
||||||
|
/// ```
|
||||||
|
///
|
||||||
|
/// [`Settings`]: ./settings/struct.Settings.html
|
||||||
|
fn default() -> Self {
|
||||||
|
Self::new()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
use std::str::FromStr;
|
||||||
|
impl FromStr for Settings {
|
||||||
|
type Err = serde_yaml::Error;
|
||||||
|
|
||||||
|
/// 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`.
|
||||||
|
///
|
||||||
|
/// # Arguments
|
||||||
|
///
|
||||||
|
/// * `s` - litteral string to decode into a `Settings` struct
|
||||||
|
///
|
||||||
|
/// # Example
|
||||||
|
///
|
||||||
|
/// ```
|
||||||
|
/// # 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());
|
||||||
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Eq for Settings {}
|
||||||
|
|||||||
179
src/settings/rule/mod.rs
Normal file
179
src/settings/rule/mod.rs
Normal file
@@ -0,0 +1,179 @@
|
|||||||
|
use std::collections::HashMap;
|
||||||
|
|
||||||
|
use lazy_static::lazy_static;
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
|
mod regex_wrapper;
|
||||||
|
use regex_wrapper::Regex;
|
||||||
|
|
||||||
|
lazy_static! {
|
||||||
|
static ref RE: Regex = Regex::new("%([A-Z])");
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Representation of a rule in LangEvolveRs
|
||||||
|
#[derive(Debug, Serialize, Deserialize, Clone)]
|
||||||
|
pub struct Rule {
|
||||||
|
/// Regex that should match the input text
|
||||||
|
from: Regex,
|
||||||
|
/// Text to replace matched text
|
||||||
|
to: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Rule {
|
||||||
|
/// Create new rule
|
||||||
|
///
|
||||||
|
/// # Arguments
|
||||||
|
///
|
||||||
|
/// * `from` - literal string that represents the regex that should match
|
||||||
|
/// the input text
|
||||||
|
/// * `to` - literal string that represents the regex text that should
|
||||||
|
/// replaced the text matched by `from`
|
||||||
|
///
|
||||||
|
/// # Example
|
||||||
|
/// ```
|
||||||
|
/// # use lazy_static::lazy_static;
|
||||||
|
/// # #[path = "mod.rs"]
|
||||||
|
/// # mod rule;
|
||||||
|
/// # use rule::Rule;
|
||||||
|
/// let rule = Rule::new("ab+c*", "ab");
|
||||||
|
/// ```
|
||||||
|
pub fn new(from: &str, to: &str) -> Self {
|
||||||
|
Rule {
|
||||||
|
from: Regex::new(from),
|
||||||
|
to: String::from(to),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Detect the number of categories in a rule
|
||||||
|
///
|
||||||
|
/// For a rule, this function detects the number of categories set in the
|
||||||
|
/// `from` member of a `Rule` and in its `to` member. The result is returned
|
||||||
|
/// as a tuple of `u8`s.
|
||||||
|
///
|
||||||
|
/// # Example
|
||||||
|
///
|
||||||
|
/// ```
|
||||||
|
/// # #[path = "mod.rs"]
|
||||||
|
/// # mod rule;
|
||||||
|
/// # use rule::Rule;
|
||||||
|
/// let rule = Rule::new("%Bea*i+", "a%A%C");
|
||||||
|
/// let nb_rules = rule.detect_number_categories();
|
||||||
|
/// assert_eq!((1 as u8, 2 as u8), nb_rules);
|
||||||
|
/// ```
|
||||||
|
pub fn detect_number_categories(&self) -> (u8, u8) {
|
||||||
|
let captures_from = self.from.to_string().matches('%').count() as u8;
|
||||||
|
let captures_to = self.to.matches('%').count() as u8;
|
||||||
|
(captures_from, captures_to)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn simple_rewrite(&self, categories: &HashMap<String, String>) -> Self {
|
||||||
|
let mut rule = self.clone();
|
||||||
|
for (category, content) in categories {
|
||||||
|
rule.from = Regex::new(
|
||||||
|
rule.from
|
||||||
|
.to_string()
|
||||||
|
.replace(
|
||||||
|
format!("%{}", category).as_str(),
|
||||||
|
format!("[{}]", content).as_str(),
|
||||||
|
)
|
||||||
|
.as_str(),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
rule
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO break categories in different rules
|
||||||
|
pub fn update(
|
||||||
|
&self,
|
||||||
|
categories: &HashMap<String, String>,
|
||||||
|
) -> std::result::Result<Rule, String> {
|
||||||
|
let mut rule = self.clone();
|
||||||
|
let (from_match, to_match) = self.detect_number_categories();
|
||||||
|
// If there are only simple rewrites to make in the from String
|
||||||
|
if from_match > 0 && to_match == 0 {
|
||||||
|
rule = self.simple_rewrite(&categories);
|
||||||
|
}
|
||||||
|
|
||||||
|
// If there are equivalences between from and to
|
||||||
|
if from_match > 0 && to_match <= from_match && to_match > 0 {}
|
||||||
|
|
||||||
|
Ok(rule)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_from(&self) -> &Regex {
|
||||||
|
&self.from
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_to(&self) -> String {
|
||||||
|
self.to.clone()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<&str> for Rule {
|
||||||
|
/// Allow to create a rule from a single literal string
|
||||||
|
///
|
||||||
|
/// It is possible to create a rule from a string, delimited by a `>`. This
|
||||||
|
/// means a rule like `%C>%D` will be interpreted as going from `%C` to
|
||||||
|
/// `%D`.
|
||||||
|
///
|
||||||
|
/// # Example
|
||||||
|
///
|
||||||
|
/// ```
|
||||||
|
/// # #[path = "mod.rs"]
|
||||||
|
/// # mod rule;
|
||||||
|
/// # use rule::Rule;
|
||||||
|
/// let rule1 = Rule::new("%C", "%D");
|
||||||
|
/// let rule2 = Rule::from("%C>%D");
|
||||||
|
/// assert_eq!(rule1, rule2);
|
||||||
|
/// ```
|
||||||
|
fn from(source: &str) -> Self {
|
||||||
|
let components: Vec<&str> = source.split_terminator('>').collect();
|
||||||
|
Rule::new(components[0], components[1])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<String> for Rule {
|
||||||
|
/// Allow to create a rule from a single `String`
|
||||||
|
///
|
||||||
|
/// It is possible to create a rule from a string, delimited by a `>`. This
|
||||||
|
/// means a rule like `%C>%D` will be interpreted as going from `%C` to
|
||||||
|
/// `%D`.
|
||||||
|
///
|
||||||
|
/// # Example
|
||||||
|
///
|
||||||
|
/// ```
|
||||||
|
/// # #[path = "mod.rs"]
|
||||||
|
/// # mod rule;
|
||||||
|
/// # use rule::Rule;
|
||||||
|
/// let rule1 = Rule::new("%C", "%D");
|
||||||
|
/// let rule2 = Rule::from(String::from("%C>%D"));
|
||||||
|
/// assert_eq!(rule1, rule2);
|
||||||
|
/// ```
|
||||||
|
fn from(source: String) -> Self {
|
||||||
|
let components: Vec<&str> = source.split_terminator('>').collect();
|
||||||
|
Rule::new(components[0], components[1])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl PartialEq for Rule {
|
||||||
|
/// Equality between `Rule` structs
|
||||||
|
///
|
||||||
|
/// This allows for equality comparison between two `Rule` structs.
|
||||||
|
///
|
||||||
|
/// # Example
|
||||||
|
///
|
||||||
|
/// ```
|
||||||
|
/// # #[path = "mod.rs"]
|
||||||
|
/// # mod rule;
|
||||||
|
/// use rule::Rule;
|
||||||
|
/// let rule1 = Rule::new("%C", "%D");
|
||||||
|
/// let rule2 = Rule::from("%C>%D");
|
||||||
|
/// assert!(rule1 == rule2);
|
||||||
|
/// assert!(rule2 == rule1);
|
||||||
|
/// ```
|
||||||
|
fn eq(&self, other: &Self) -> bool {
|
||||||
|
self.from == other.from && self.to == other.to
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Eq for Rule {}
|
||||||
86
src/settings/rule/regex_wrapper.rs
Normal file
86
src/settings/rule/regex_wrapper.rs
Normal file
@@ -0,0 +1,86 @@
|
|||||||
|
use std::{fmt, ops};
|
||||||
|
|
||||||
|
#[derive(Clone, Debug)]
|
||||||
|
pub struct Regex(regex::Regex);
|
||||||
|
|
||||||
|
impl Regex {
|
||||||
|
/// Create a new Regex wrapper around regex::Regex;
|
||||||
|
///
|
||||||
|
/// # Arguments
|
||||||
|
///
|
||||||
|
/// * `s` - string litteral from which to create the new Regex
|
||||||
|
pub fn new(s: &str) -> Self {
|
||||||
|
Self(regex::Regex::new(s).unwrap())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns a string literal representation of the Regex
|
||||||
|
#[allow(unused)]
|
||||||
|
pub fn as_str(&self) -> &str {
|
||||||
|
self.0.as_str()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ToString for Regex {
|
||||||
|
fn to_string(&self) -> String {
|
||||||
|
self.0.to_string()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
use std::hash::{Hash, Hasher};
|
||||||
|
impl Hash for Regex {
|
||||||
|
fn hash<H: Hasher>(&self, state: &mut H) {
|
||||||
|
self.0.as_str().hash(state);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ops::Deref for Regex {
|
||||||
|
type Target = regex::Regex;
|
||||||
|
fn deref(&self) -> ®ex::Regex {
|
||||||
|
&self.0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'de> serde::Deserialize<'de> for Regex {
|
||||||
|
fn deserialize<D>(de: D) -> Result<Regex, D::Error>
|
||||||
|
where
|
||||||
|
D: serde::Deserializer<'de>,
|
||||||
|
{
|
||||||
|
use serde::de::{Error, Visitor};
|
||||||
|
|
||||||
|
struct RegexVisitor;
|
||||||
|
|
||||||
|
impl<'de> Visitor<'de> for RegexVisitor {
|
||||||
|
type Value = Regex;
|
||||||
|
|
||||||
|
fn expecting(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
|
f.write_str("a regular expression pattern")
|
||||||
|
}
|
||||||
|
|
||||||
|
fn visit_str<E: Error>(self, v: &str) -> Result<Regex, E> {
|
||||||
|
regex::Regex::new(v)
|
||||||
|
.map(Regex)
|
||||||
|
.map_err(|err| E::custom(err.to_string()))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
de.deserialize_str(RegexVisitor)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
use serde::{Serialize, Serializer};
|
||||||
|
impl Serialize for Regex {
|
||||||
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||||
|
where
|
||||||
|
S: Serializer,
|
||||||
|
{
|
||||||
|
serializer.serialize_str(self.0.as_str())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl PartialEq for Regex {
|
||||||
|
fn eq(&self, other: &Self) -> bool {
|
||||||
|
self.0.to_string() == other.0.to_string()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Eq for Regex {}
|
||||||
@@ -1,28 +0,0 @@
|
|||||||
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)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,4 +0,0 @@
|
|||||||
#[test]
|
|
||||||
fn it_works() {
|
|
||||||
assert_eq!(2 + 2, 4);
|
|
||||||
}
|
|
||||||
130
src/utils/mod.rs
Normal file
130
src/utils/mod.rs
Normal file
@@ -0,0 +1,130 @@
|
|||||||
|
use log::{info, error};
|
||||||
|
|
||||||
|
use std::fs::File;
|
||||||
|
use std::io::{Read, Result};
|
||||||
|
use std::path::Path;
|
||||||
|
|
||||||
|
/// Type of supported settings format: yaml or json
|
||||||
|
#[derive(Debug, PartialEq)]
|
||||||
|
pub enum SettingsType {
|
||||||
|
/// Files ending with the `yml` or `yaml` extension
|
||||||
|
Yaml,
|
||||||
|
|
||||||
|
/// Files ending with the `json` extension
|
||||||
|
Json,
|
||||||
|
|
||||||
|
/// Other file type, used to describe files without an extension or with an
|
||||||
|
/// unsupported extension
|
||||||
|
Other,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Read a file’s content into a `String`
|
||||||
|
///
|
||||||
|
/// # Example
|
||||||
|
///
|
||||||
|
/// ```no_run
|
||||||
|
/// # use lang_evolve_core::utils;
|
||||||
|
/// let path = std::path::Path::new("./some/path/to/my/file.json");
|
||||||
|
/// let content = utils::read_file(&path).unwrap();
|
||||||
|
/// ```
|
||||||
|
pub fn read_file(path: &Path) -> Result<String> {
|
||||||
|
let display = path.display();
|
||||||
|
let mut file = match File::open(&path) {
|
||||||
|
Err(why) => {
|
||||||
|
error!("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) => {
|
||||||
|
error!("Could not read {}: {}", display, why.to_string());
|
||||||
|
Err(why)
|
||||||
|
}
|
||||||
|
Ok(_) => {
|
||||||
|
info!("Content of {} read", display);
|
||||||
|
Ok(content)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Write a `String` into a file
|
||||||
|
///
|
||||||
|
/// # Example
|
||||||
|
///
|
||||||
|
/// ```no_run
|
||||||
|
/// # use lang_evolve_core::utils;
|
||||||
|
/// let content = String::from("This is my content");
|
||||||
|
/// let path = std::path::Path::new("./path/to/my/file.txt");
|
||||||
|
/// utils::write_file(&path, &content).unwrap();
|
||||||
|
/// ```
|
||||||
|
pub fn write_file<S>(path: &Path, content: &S) -> Result<()>
|
||||||
|
where
|
||||||
|
S: std::string::ToString,
|
||||||
|
{
|
||||||
|
use std::io::prelude::*;
|
||||||
|
let mut file = match File::create(&path) {
|
||||||
|
Err(e) => {
|
||||||
|
error!("Could not open file {}: {}", path.display(), e.to_string());
|
||||||
|
return Err(e);
|
||||||
|
}
|
||||||
|
Ok(file) => file,
|
||||||
|
};
|
||||||
|
match file.write_all(content.to_string().as_bytes()) {
|
||||||
|
Err(e) => {
|
||||||
|
error!(
|
||||||
|
"Could not write settings to file {}: {}",
|
||||||
|
path.display(),
|
||||||
|
e.to_string()
|
||||||
|
);
|
||||||
|
return Err(e);
|
||||||
|
}
|
||||||
|
Ok(_) => {
|
||||||
|
info!("Wrote settings to file {}", path.display());
|
||||||
|
}
|
||||||
|
};
|
||||||
|
info!("Successfuly written {}", path.display());
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Get the type of file to be opened based on its extension. Currently
|
||||||
|
/// supported file types are:
|
||||||
|
/// * JSON - `.json` extension
|
||||||
|
/// * Yaml - `.yml` or `.yaml` extensions
|
||||||
|
///
|
||||||
|
/// # Arguments
|
||||||
|
///
|
||||||
|
/// - `path` - Path of the file to be determined
|
||||||
|
///
|
||||||
|
/// # Example
|
||||||
|
///
|
||||||
|
/// ```
|
||||||
|
/// # use lang_evolve_core::utils;
|
||||||
|
/// let file_json = std::path::Path::new("file.json");
|
||||||
|
/// assert_eq!(utils::SettingsType::Json,
|
||||||
|
/// utils::get_file_type(&file_json));
|
||||||
|
///
|
||||||
|
/// let file_yaml = std::path::Path::new("file.yaml");
|
||||||
|
/// assert_eq!(utils::SettingsType::Yaml,
|
||||||
|
/// utils::get_file_type(&file_yaml));
|
||||||
|
///
|
||||||
|
/// let file_yml = std::path::Path::new("file.yml");
|
||||||
|
/// assert_eq!(utils::SettingsType::Yaml,
|
||||||
|
/// utils::get_file_type(&file_yml));
|
||||||
|
/// ```
|
||||||
|
pub fn get_file_type(path: &Path) -> SettingsType {
|
||||||
|
let extension = match path.extension() {
|
||||||
|
None => { return SettingsType::Other; }
|
||||||
|
Some(val) => val,
|
||||||
|
};
|
||||||
|
let extension = extension
|
||||||
|
.to_str()
|
||||||
|
.expect("Could not get String out of extension")
|
||||||
|
.to_lowercase();
|
||||||
|
match extension.as_str() {
|
||||||
|
"yml" | "yaml" => SettingsType::Yaml,
|
||||||
|
"json" => SettingsType::Json,
|
||||||
|
_ => SettingsType::Other,
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user