Skip to content

Commit

Permalink
feat: add StringMatcher
Browse files Browse the repository at this point in the history
  • Loading branch information
yuma140902 committed Nov 10, 2023
1 parent d9b0b59 commit f8953d3
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 5 deletions.
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@ mod loader;
pub mod pipeline;
pub mod project_config;
pub mod store;
mod string_matcher;
mod template_engine;
pub mod transformer;
mod value;

pub use loader::*;
pub use string_matcher::*;
pub use template_engine::*;
pub use value::*;

Expand Down
7 changes: 4 additions & 3 deletions src/pipeline/entry.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
use regex::Regex;
use serde::{Deserialize, Serialize};

use crate::StringMatcher;

use super::EnumLoader;

#[derive(Debug, Deserialize, Serialize)]
pub struct Entry {
#[serde(rename = "match", with = "serde_regex")]
pub match_regex: Regex,
#[serde(rename = "match")]
pub match_regex: StringMatcher,
#[serde(rename = "with")]
pub type_: EnumLoader,
}
9 changes: 7 additions & 2 deletions src/project_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use serde::{Deserialize, Serialize};
use crate::{
pipeline::{Entry, EnumLoader, EnumTransformer, InputKey, Pipeline, Step},
transformer::{CurrentDirectory, TemplateRenderer},
StringMatcher,
};

#[derive(Serialize, Deserialize, Debug)]
Expand All @@ -18,7 +19,9 @@ impl Default for ProjectConfig {
Pipeline {
name: "markdown to html".to_string(),
entry: Entry {
match_regex: Regex::new(".*[.]md").unwrap(),
match_regex: StringMatcher::Regex {
regex: Regex::new("^.*[.]md$").unwrap(),
},
type_: EnumLoader::TextWithFrontmatter,
},
steps: vec![
Expand All @@ -42,7 +45,9 @@ impl Default for ProjectConfig {
Pipeline {
name: "static resources".to_string(),
entry: Entry {
match_regex: Regex::new(".*").unwrap(),
match_regex: StringMatcher::Regex {
regex: Regex::new(".*").unwrap(),
},
type_: EnumLoader::Blob,
},
steps: vec![],
Expand Down
35 changes: 35 additions & 0 deletions src/string_matcher.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
use regex::Regex;
use serde::{Deserialize, Serialize};

#[derive(Debug, Deserialize, Serialize)]
#[serde(tag = "type")]
pub enum StringMatcher {
Regex {
#[serde(with = "serde_regex")]
regex: Regex,
},
Not {
matcher: Box<StringMatcher>,
},
All {
matchers: Vec<StringMatcher>,
},
Any {
matchers: Vec<StringMatcher>,
},
}

impl StringMatcher {
pub fn is_match(&self, string: &str) -> bool {
match self {
StringMatcher::Regex { regex } => regex.is_match(string),
StringMatcher::Not { matcher } => !matcher.is_match(string),
StringMatcher::All { matchers } => {
matchers.iter().all(|matcher| matcher.is_match(string))
}
StringMatcher::Any { matchers } => {
matchers.iter().any(|matcher| matcher.is_match(string))
}
}
}
}

0 comments on commit f8953d3

Please sign in to comment.