refactor: don't move value in Act and Match implementations

This commit is contained in:
Harsh Shandilya 2022-02-15 02:54:16 +05:30
parent 28710ab3ed
commit a112cbefc6
No known key found for this signature in database
GPG key ID: 366D7BBAD1031E80

View file

@ -49,19 +49,19 @@ pub enum Action<'config> {
} }
pub trait Match { pub trait Match {
fn check_match(self, string: &str) -> bool; fn check_match(&self, string: &str) -> bool;
} }
pub trait Act { pub trait Act {
fn apply_action(self, input: &str) -> String; fn apply_action(&self, input: &str) -> String;
} }
impl Match for Matcher<'_> { impl Match for Matcher<'_> {
fn check_match(self, string: &str) -> bool { fn check_match(&self, string: &str) -> bool {
match self { match self {
Matcher::StartsWith { prefix } => string.starts_with(&prefix), Matcher::StartsWith { prefix } => string.starts_with(prefix),
Matcher::EndsWith { suffix } => string.ends_with(&suffix), Matcher::EndsWith { suffix } => string.ends_with(suffix),
Matcher::Contains { substring } => string.contains(&substring), Matcher::Contains { substring } => string.contains(substring),
Matcher::Regex { pattern } => { Matcher::Regex { pattern } => {
return if let Ok(regex) = Regex::from_str(pattern) { return if let Ok(regex) = Regex::from_str(pattern) {
regex.is_match(string) regex.is_match(string)
@ -69,18 +69,18 @@ impl Match for Matcher<'_> {
false false
} }
} }
Matcher::Exactly { content } => string == content, Matcher::Exactly { content } => &string == content,
} }
} }
} }
impl Act for Action<'_> { impl Act for Action<'_> {
fn apply_action(self, input: &str) -> String { fn apply_action(&self, input: &str) -> String {
return match self { return match self {
Action::Replace { from, to } => input.replace(from, to), Action::Replace { from, to } => input.replace(from, to),
Action::Prefix { prefix } => format!("{prefix}{input}"), Action::Prefix { prefix } => format!("{prefix}{input}"),
Action::Suffix { suffix } => format!("{input}{suffix}"), Action::Suffix { suffix } => format!("{input}{suffix}"),
Action::Set { content } => content.to_owned(), Action::Set { content } => content.to_owned().to_owned(),
}; };
} }
} }