Skip to content

Commit

Permalink
Merge pull request #38 from yuma140902/use-serde-regex
Browse files Browse the repository at this point in the history
use `serde_regex`
  • Loading branch information
yuma140902 authored Oct 15, 2023
2 parents 5c4eb84 + a516f52 commit 86ce702
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 30 deletions.
11 changes: 11 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ pulldown-cmark = "0.9.3"
regex = "1.10.0"
serde = { version = "1", features = ["derive"] }
serde_json = "1"
serde_regex = "1.1.0"
serde_yaml = "0.9.25"
tracing = "0.1.39"
tracing-subscriber = "0.3.17"
Expand Down
14 changes: 2 additions & 12 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,7 @@ fn build_single_file(
pub fn build(project_root: &Path) -> io::Result<()> {
let pages_directory = directory::get_pages_directory(project_root);
let project_config_path = directory::get_project_config_path(project_root);
let config = {
let mut config: ProjectConfig =
serde_json::from_str(&fs::read_to_string(project_config_path)?)?;
config.generate_regex();
config
};
let config: ProjectConfig = serde_json::from_str(&fs::read_to_string(project_config_path)?)?;

let generators = generator::get_generators(project_root);

Expand All @@ -84,12 +79,7 @@ pub fn build(project_root: &Path) -> io::Result<()> {
{
let mut selected_rule = None;
for rule in config.generator.rules.iter() {
if rule
.match_regex
.get()
.unwrap()
.is_match(filepath.to_string_lossy().borrow())
{
if rule.match_.is_match(filepath.to_string_lossy().borrow()) {
selected_rule = Some(rule);
}
}
Expand Down
22 changes: 4 additions & 18 deletions src/project_config.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use once_cell::sync::OnceCell;
use regex::Regex;
use serde::{Deserialize, Serialize};

Expand All @@ -14,10 +13,8 @@ pub struct Generator {

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct GeneratorRule {
#[serde(rename = "match")]
pub match_: String,
#[serde(skip)]
pub match_regex: OnceCell<Regex>,
#[serde(rename = "match", with = "serde_regex")]
pub match_: Regex,
pub generator: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub export_base: Option<String>,
Expand All @@ -32,8 +29,7 @@ impl Default for ProjectConfig {
Self {
generator: Generator {
rules: vec![GeneratorRule {
match_: ".*[.]md".to_owned(),
match_regex: OnceCell::new(),
match_: Regex::new(".*[.]md").unwrap(),
generator: "handlebars".to_owned(),
export_base: None,
export_extension: Some("html".to_owned()),
Expand All @@ -47,21 +43,11 @@ impl Default for ProjectConfig {
impl Default for GeneratorRule {
fn default() -> Self {
Self {
match_: ".*".to_owned(),
match_regex: OnceCell::new(),
match_: Regex::new(".*").unwrap(),
generator: "echo".to_owned(),
export_base: None,
export_extension: None,
template: None,
}
}
}

impl ProjectConfig {
pub fn generate_regex(&mut self) {
for rule in self.generator.rules.iter_mut() {
rule.match_regex
.get_or_init(|| Regex::new(&format!("^{}$", rule.match_)).unwrap());
}
}
}

0 comments on commit 86ce702

Please sign in to comment.