Skip to content

Commit

Permalink
🔀 Merge pull request #24 from ShellWen/feat/13-add-more-sourceprocessor
Browse files Browse the repository at this point in the history
add more sourceprocessor
  • Loading branch information
ShellWen authored Nov 28, 2023
2 parents f65c17d + 41888dd commit acb252c
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 6 deletions.
1 change: 1 addition & 0 deletions crates/core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ lazy_static = "1.4.0"
ctor = "0.2.4"
toml = "0.8.1"
serde = { version = "1.0.188", features = ["derive"] }
regex = "1.10.2"

[target.'cfg(target_os = "windows")'.dependencies]
windows = { version = "0.51.1", features = [
Expand Down
19 changes: 17 additions & 2 deletions crates/core/src/matcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,16 @@ pub(crate) trait SourceMatcher {
#[serde(tag = "type")]
pub(crate) enum SourceMatcherEnum {
#[serde(rename = "resource-name-keyword")]
ResourceNameKeywordMatcher(ResourceNameKeywordMatcher),
ResourceNameKeyword(ResourceNameKeywordMatcher),
#[serde(rename = "resource-name-regexp")]
ResourceNameRegexp(ResourceNameRegexpMatcher),
}

impl SourceMatcher for SourceMatcherEnum {
fn matches(&self, source: &Source) -> bool {
match self {
SourceMatcherEnum::ResourceNameKeywordMatcher(matcher) => matcher.matches(source),
SourceMatcherEnum::ResourceNameKeyword(matcher) => matcher.matches(source),
SourceMatcherEnum::ResourceNameRegexp(matcher) => matcher.matches(source),
}
}
}
Expand All @@ -31,3 +34,15 @@ impl SourceMatcher for ResourceNameKeywordMatcher {
source.resource_name.contains(&self.keyword)
}
}

#[derive(Deserialize, Debug)]
pub struct ResourceNameRegexpMatcher {
pub regexp: String,
}

impl SourceMatcher for ResourceNameRegexpMatcher {
fn matches(&self, source: &Source) -> bool {
let re = regex::Regex::new(&self.regexp).unwrap();
re.is_match(&source.resource_name)
}
}
41 changes: 37 additions & 4 deletions crates/core/src/processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,22 @@ pub(crate) trait SourceProcessorTrait {
#[serde(tag = "type")]
pub enum SourceProcessor {
#[serde(rename = "insert-before")]
InsertBeforeProcessor(InsertBeforeProcessor),
InsertBefore(InsertBeforeProcessor),
#[serde(rename = "insert-after")]
InsertAfterProcessor(InsertAfterProcessor),
InsertAfter(InsertAfterProcessor),
#[serde(rename = "replace")]
Replace(ReplaceProcessor),
#[serde(rename = "replace-regexp")]
ReplaceRegexp(ReplaceRegexpProcessor),
}

impl SourceProcessor {
pub fn process<'a>(&self, source: &'a mut Source) -> Result<&'a mut Source, Error> {
match self {
SourceProcessor::InsertBeforeProcessor(processor) => processor.process(source),
SourceProcessor::InsertAfterProcessor(processor) => processor.process(source),
SourceProcessor::InsertBefore(processor) => processor.process(source),
SourceProcessor::InsertAfter(processor) => processor.process(source),
SourceProcessor::Replace(processor) => processor.process(source),
SourceProcessor::ReplaceRegexp(processor) => processor.process(source),
}
}
}
Expand Down Expand Up @@ -49,3 +55,30 @@ impl SourceProcessorTrait for InsertAfterProcessor {
Ok(source)
}
}

#[derive(Deserialize, Debug)]
pub struct ReplaceProcessor {
from: String,
to: String,
}

impl SourceProcessorTrait for ReplaceProcessor {
fn process<'a>(&self, source: &'a mut Source) -> Result<&'a mut Source, Error> {
source.source_string = source.source_string.replace(&self.from, &self.to);
Ok(source)
}
}

#[derive(Deserialize, Debug)]
pub struct ReplaceRegexpProcessor {
regexp: String,
to: String,
}

impl SourceProcessorTrait for ReplaceRegexpProcessor {
fn process<'a>(&self, source: &'a mut Source) -> Result<&'a mut Source, Error> {
let re = regex::Regex::new(&self.regexp).unwrap();
source.source_string = re.replace_all(&source.source_string, &self.to).to_string();
Ok(source)
}
}

0 comments on commit acb252c

Please sign in to comment.