Cleaner code

This commit is contained in:
Lucien Cartier-Tilet 2020-07-12 12:28:19 +02:00
parent bb9d8703f3
commit d33190a46d
Signed by: phundrak
GPG Key ID: BD7789E705CB8DCA
2 changed files with 17 additions and 27 deletions

View File

@ -206,16 +206,18 @@ impl Settings {
}
},
};
if settings.version > Self::get_ruleset_version() {
error!("Ruleset version too high!");
return Err(std::io::Error::new(
Err(std::io::Error::new(
std::io::ErrorKind::InvalidData,
"Ruleset version too high!",
));
}
))
} else {
info!("Successfuly imported {}", path.display());
Ok(settings)
}
}
/// Import settings from file path described by the argument `path`
///
@ -228,13 +230,13 @@ impl Settings {
/// ```no_run
/// let s = lang_evolve_core::settings::Settings::from("settings.yml");
/// ```
pub fn from<S>(s: S) -> Self
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).unwrap()
Self::import(&path)
}
/// Add a new rule to the current settings

View File

@ -1,14 +1,13 @@
use std::collections::HashMap;
use serde::{Deserialize, Serialize};
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])");
static ref RE: Regex = Regex::new("%([A-Z])");
}
/// Representation of a rule in LangEvolveRs
@ -45,15 +44,9 @@ impl Rule {
}
}
fn detect_categories(&self) -> (u8, u8) {
let captures_from = match RE.captures(self.from.as_str()) {
None => 0 as u8,
Some(c) => c.len() as u8,
};
let captures_to = match RE.captures(&self.to) {
None => 0 as u8,
Some(c) => c.len() as u8,
};
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)
}
@ -67,7 +60,7 @@ impl Rule {
format!("%{}", category).as_str(),
format!("[{}]", content).as_str(),
)
.as_str()
.as_str(),
);
}
rule
@ -79,16 +72,14 @@ impl Rule {
categories: &HashMap<String, String>,
) -> std::result::Result<Rule, String> {
let mut rule = self.clone();
let (from_match, to_match) = self.detect_categories();
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 {
}
if from_match > 0 && to_match <= from_match && to_match > 0 {}
Ok(rule)
}
@ -105,10 +96,7 @@ impl Rule {
impl From<String> for Rule {
fn from(source: String) -> Self {
let components: Vec<&str> = source.split_terminator('>').collect();
Self {
from: Regex::new(components[0]),
to: String::from(components[1]),
}
Rule::new(components[0], components[1])
}
}