Add From<&str> trait implementation to Rule, ToString to Regex
Also remove obvious test for Rule
This commit is contained in:
parent
cb933d4896
commit
531c9b1acc
@ -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<String> 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);
|
||||
}
|
||||
|
@ -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<H: Hasher>(&self, state: &mut H) {
|
||||
|
Loading…
Reference in New Issue
Block a user