fix: use rename_all and fix tests

This commit is contained in:
Harsh Shandilya 2023-01-24 17:32:57 +00:00
parent ae1e2a203f
commit 7918e78d65
2 changed files with 25 additions and 29 deletions

View file

@ -28,31 +28,21 @@ pub enum MatcherType {
}
#[derive(Debug, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum Matcher {
#[serde(rename = "starts_with")]
StartsWith { prefix: String },
#[serde(rename = "ends_with")]
EndsWith { suffix: String },
#[serde(rename = "contains")]
Contains { substring: String },
#[serde(rename = "regex")]
Regex { pattern: String },
#[serde(rename = "exactly")]
Exactly { content: String },
}
#[derive(Debug, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum Action {
#[serde(rename = "set")]
Set { content: String },
#[serde(rename = "replace")]
Replace {
from: String,
to: String,
},
#[serde(rename = "prefix")]
Replace { from: String, to: String },
Prefix { prefix: String },
#[serde(rename = "suffix")]
Suffix { suffix: String },
}

View file

@ -5,7 +5,7 @@ use crate::config::{Act, Action, Match, Matcher, MatcherType, Replacements};
#[assay]
fn regex_matcher() {
let matcher = Matcher::Regex {
pattern: "^https.*",
pattern: "^https.*".to_string(),
};
assert!(matcher.check_match("https://example.com"));
assert!(!matcher.check_match("example.com"));
@ -13,29 +13,35 @@ fn regex_matcher() {
#[assay]
fn set_action() {
let action = Action::Set { content: "doe" };
assert_eq!("doe", action.apply_action("john"));
let action = Action::Set {
content: "doe".to_string(),
};
assert_eq!("doe", &action.apply_action("john"));
}
#[assay]
fn replace_action() {
let action = Action::Replace {
from: "doe",
to: "bow",
from: "doe".to_string(),
to: "bow".to_string(),
};
assert_eq!("john bow", action.apply_action("john doe"));
assert_eq!("john bow", &action.apply_action("john doe"));
}
#[assay]
fn prefix_action() {
let action = Action::Prefix { prefix: "hello " };
assert_eq!("hello john", action.apply_action("john"));
let action = Action::Prefix {
prefix: "hello ".to_string(),
};
assert_eq!("hello john", &action.apply_action("john"));
}
#[assay]
fn suffix_action() {
let action = Action::Suffix { suffix: " doe" };
assert_eq!("john doe", action.apply_action("john"));
let action = Action::Suffix {
suffix: " doe".to_string(),
};
assert_eq!("john doe", &action.apply_action("john"));
}
#[assay]
@ -49,10 +55,10 @@ fn parse_with_multiple_matchers() {
]
action = { prefix = { prefix = "/mirror" } }
"#;
let config: Replacements<'_> = toml::from_str(config)?;
let config: Replacements = toml::from_str(config)?;
assert_eq!(1, config.substitutors.len());
let subst = &config.substitutors[0];
assert_eq!("Example", subst.name);
assert_eq!("Example", &subst.name);
assert!(matches!(subst.matcher, MatcherType::Multiple(_)));
assert!(matches!(subst.action, Action::Prefix { .. }));
}
@ -65,10 +71,10 @@ fn parse_with_single_matcher() {
matcher = { starts_with = { prefix = "https://example.com" } }
action = { prefix = { prefix = "/mirror" } }
"#;
let config: Replacements<'_> = toml::from_str(config)?;
let config: Replacements = toml::from_str(config)?;
assert_eq!(1, config.substitutors.len());
let subst = &config.substitutors[0];
assert_eq!("Example", subst.name);
assert_eq!("Example", &subst.name);
assert!(matches!(subst.matcher, MatcherType::Single(_)));
assert!(matches!(subst.action, Action::Prefix { .. }));
}
@ -81,7 +87,7 @@ name = "vxTwitter"
matcher = { regex = { pattern = "^https://(?P<host>(?:mobile.)?twitter.com)/.*/status/[0-9]+.*" } }
action = { replace = { from = "twitter.com", to = "vxtwitter.com" } }
"#;
let config: Replacements<'_> = toml::from_str(config)?;
let config: Replacements = toml::from_str(config)?;
assert!(matches!(config.validate(), Ok(_)));
}
@ -93,6 +99,6 @@ name = "vxTwitter"
matcher = { regex = { pattern = "^https://(?P<>(?:mobile.)?twitter.com)/.*/status/[0-9]+.*" } }
action = { replace = { from = "twitter.com", to = "vxtwitter.com" } }
"#;
let config: Replacements<'_> = toml::from_str(config)?;
let config: Replacements = toml::from_str(config)?;
assert!(matches!(config.validate(), Err(_)));
}