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)] #[derive(Debug, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum Matcher { pub enum Matcher {
#[serde(rename = "starts_with")]
StartsWith { prefix: String }, StartsWith { prefix: String },
#[serde(rename = "ends_with")]
EndsWith { suffix: String }, EndsWith { suffix: String },
#[serde(rename = "contains")]
Contains { substring: String }, Contains { substring: String },
#[serde(rename = "regex")]
Regex { pattern: String }, Regex { pattern: String },
#[serde(rename = "exactly")]
Exactly { content: String }, Exactly { content: String },
} }
#[derive(Debug, Deserialize)] #[derive(Debug, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum Action { pub enum Action {
#[serde(rename = "set")]
Set { content: String }, Set { content: String },
#[serde(rename = "replace")] Replace { from: String, to: String },
Replace {
from: String,
to: String,
},
#[serde(rename = "prefix")]
Prefix { prefix: String }, Prefix { prefix: String },
#[serde(rename = "suffix")]
Suffix { suffix: String }, Suffix { suffix: String },
} }

View file

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