From 7918e78d6576d2b5027e86d5de95b8a905e0199b Mon Sep 17 00:00:00 2001 From: Harsh Shandilya Date: Tue, 24 Jan 2023 17:32:57 +0000 Subject: [PATCH] fix: use `rename_all` and fix tests --- src/config.rs | 16 +++------------- src/test.rs | 38 ++++++++++++++++++++++---------------- 2 files changed, 25 insertions(+), 29 deletions(-) diff --git a/src/config.rs b/src/config.rs index ff81a65..3c855f7 100644 --- a/src/config.rs +++ b/src/config.rs @@ -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 }, } diff --git a/src/test.rs b/src/test.rs index 09cb8bc..ba1531b 100644 --- a/src/test.rs +++ b/src/test.rs @@ -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(?: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(_))); }