Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add more sourceprocessor #24

Merged
merged 3 commits into from
Nov 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}
}