diff options
Diffstat (limited to 'gen/gen_renderers_file.rs')
| -rw-r--r-- | gen/gen_renderers_file.rs | 58 |
1 files changed, 15 insertions, 43 deletions
diff --git a/gen/gen_renderers_file.rs b/gen/gen_renderers_file.rs index 497d258..f16c504 100644 --- a/gen/gen_renderers_file.rs +++ b/gen/gen_renderers_file.rs @@ -1,18 +1,19 @@ use std::path::PathBuf; +use just_template::{Template, tmpl}; + use crate::r#gen::constants::{ OVERRIDE_RENDERER_DISPATCHER, OVERRIDE_RENDERER_DISPATCHER_TEMPLATE, REGISTRY_TOML, - TEMPLATE_END, TEMPLATE_START, }; -/// Generate renderer list file from Registry.toml configuration +/// Generate renderer list file from Registry.toml configuration using just_template pub async fn generate_renderers_file(repo_root: &PathBuf) { let template_path = repo_root.join(OVERRIDE_RENDERER_DISPATCHER_TEMPLATE); let output_path = repo_root.join(OVERRIDE_RENDERER_DISPATCHER); let config_path = repo_root.join(REGISTRY_TOML); // Read the template - let template = tokio::fs::read_to_string(&template_path).await.unwrap(); + let template_content = tokio::fs::read_to_string(&template_path).await.unwrap(); // Read and parse the TOML configuration let config_content = tokio::fs::read_to_string(&config_path).await.unwrap(); @@ -45,53 +46,24 @@ pub async fn generate_renderers_file(repo_root: &PathBuf) { renderers.push((name.to_string(), renderer_type.to_string())); } - // Extract the template section from the template content - const MATCH_MARKER: &str = "// MATCH"; - - let template_start_index = template - .find(TEMPLATE_START) - .ok_or("Template start marker not found") - .unwrap(); - let template_end_index = template - .find(TEMPLATE_END) - .ok_or("Template end marker not found") - .unwrap(); + // Create template + let mut template = Template::from(template_content); - let template_slice = &template[template_start_index..template_end_index + TEMPLATE_END.len()]; - let renderer_template = template_slice - .trim_start_matches(TEMPLATE_START) - .trim_end_matches(TEMPLATE_END) - .trim_matches('\n'); - - // Generate the match arms for each renderer - let match_arms: String = renderers - .iter() - .map(|(name, renderer_type)| { - renderer_template - .replace("<<NAME>>", name) - .replace("RendererType", renderer_type) - .trim_matches('\n') - .to_string() - }) - .collect::<Vec<String>>() - .join("\n"); + for (name, renderer_type) in &renderers { + tmpl!(template += { + renderer_match_arms { + (name = name, renderer_type = renderer_type) + } + }); + } - // Replace the template section with the generated match arms - let final_content = template - .replace(renderer_template, "") - .replace(TEMPLATE_START, "") - .replace(TEMPLATE_END, "") - .replace(MATCH_MARKER, &match_arms) - .lines() - .filter(|line| !line.trim().is_empty()) - .collect::<Vec<_>>() - .join("\n"); + let final_content = template.expand().unwrap(); // Write the generated code tokio::fs::write(output_path, final_content).await.unwrap(); println!( - "Generated renderer list file with {} renderers", + "Generated renderer list file with {} renderers using just_template", renderers.len() ); } |
