From 531c9b1acc2206e5f4159a49676ca4d1de80b786 Mon Sep 17 00:00:00 2001 From: Lucien Cartier-Tilet Date: Sun, 12 Jul 2020 12:30:15 +0200 Subject: [PATCH] Add From<&str> trait implementation to Rule, ToString to Regex Also remove obvious test for Rule --- src/settings/rule/mod.rs | 33 +++++++++++++++++++++--------- src/settings/rule/regex_wrapper.rs | 7 +++++++ 2 files changed, 30 insertions(+), 10 deletions(-) diff --git a/src/settings/rule/mod.rs b/src/settings/rule/mod.rs index bbc9883..22b5019 100644 --- a/src/settings/rule/mod.rs +++ b/src/settings/rule/mod.rs @@ -109,6 +109,29 @@ impl Rule { } } +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 for Rule { /// Allow to create a rule from a single `String` /// @@ -154,13 +177,3 @@ impl PartialEq for Rule { } impl Eq for Rule {} - -#[test] -fn rule_new() { - let rule1 = Rule::new("([ae]+)i", "${1}i"); - let rule2 = Rule { - from: Regex::new("([ae]+)i"), - to: String::from("${1}i"), - }; - assert_eq!(rule1, rule2); -} diff --git a/src/settings/rule/regex_wrapper.rs b/src/settings/rule/regex_wrapper.rs index 8a4a208..222655f 100644 --- a/src/settings/rule/regex_wrapper.rs +++ b/src/settings/rule/regex_wrapper.rs @@ -14,11 +14,18 @@ impl Regex { } /// 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(&self, state: &mut H) {