diff --git a/src/rhai/mod.rs b/src/rhai/mod.rs index 8c0269d..77fa96e 100644 --- a/src/rhai/mod.rs +++ b/src/rhai/mod.rs @@ -67,6 +67,9 @@ pub use crate::text::{ dedent, prefix }; +// Re-export TextReplacer functions +pub use text::*; + // Rename copy functions to avoid conflicts pub use os::copy as os_copy; diff --git a/src/rhai/text.rs b/src/rhai/text.rs index b552cec..cc1d91e 100644 --- a/src/rhai/text.rs +++ b/src/rhai/text.rs @@ -7,6 +7,8 @@ use std::collections::HashMap; use crate::text::{ TextReplacer, TextReplacerBuilder, TemplateBuilder, + dedent, prefix, + name_fix, path_fix }; /// Register Text module functions with the Rhai engine @@ -22,33 +24,35 @@ pub fn register_text_module(engine: &mut Engine) -> Result<(), Box TextReplacerBuilder { } /// Sets the pattern to search for -pub fn builder_pattern(builder: TextReplacerBuilder, pat: &str) -> TextReplacerBuilder { +pub fn pattern(builder: TextReplacerBuilder, pat: &str) -> TextReplacerBuilder { builder.pattern(pat) } /// Sets the replacement text -pub fn builder_replacement(builder: TextReplacerBuilder, rep: &str) -> TextReplacerBuilder { +pub fn replacement(builder: TextReplacerBuilder, rep: &str) -> TextReplacerBuilder { builder.replacement(rep) } /// Sets whether to use regex -pub fn builder_regex(builder: TextReplacerBuilder, yes: bool) -> TextReplacerBuilder { +pub fn regex(builder: TextReplacerBuilder, yes: bool) -> TextReplacerBuilder { builder.regex(yes) } /// Sets whether the replacement should be case-insensitive -pub fn builder_case_insensitive(builder: TextReplacerBuilder, yes: bool) -> TextReplacerBuilder { +pub fn case_insensitive(builder: TextReplacerBuilder, yes: bool) -> TextReplacerBuilder { builder.case_insensitive(yes) } /// Adds another replacement operation to the chain and resets the builder for a new operation -pub fn builder_and(builder: TextReplacerBuilder) -> TextReplacerBuilder { +pub fn and(builder: TextReplacerBuilder) -> TextReplacerBuilder { builder.and() } /// Builds the TextReplacer with all configured replacement operations -pub fn builder_build(builder: TextReplacerBuilder) -> Result> { +pub fn build(builder: TextReplacerBuilder) -> Result> { string_error_to_rhai_error(builder.build()) } /// Applies all configured replacement operations to the input text -pub fn replacer_replace(replacer: &mut TextReplacer, input: &str) -> String { +pub fn replace(replacer: &mut TextReplacer, input: &str) -> String { replacer.replace(input) } /// Reads a file, applies all replacements, and returns the result as a string -pub fn replacer_replace_file(replacer: &mut TextReplacer, path: &str) -> Result> { +pub fn replace_file(replacer: &mut TextReplacer, path: &str) -> Result> { io_error_to_rhai_error(replacer.replace_file(path)) } /// Reads a file, applies all replacements, and writes the result back to the file -pub fn replacer_replace_file_in_place(replacer: &mut TextReplacer, path: &str) -> Result<(), Box> { +pub fn replace_file_in_place(replacer: &mut TextReplacer, path: &str) -> Result<(), Box> { io_error_to_rhai_error(replacer.replace_file_in_place(path)) } /// Reads a file, applies all replacements, and writes the result to a new file -pub fn replacer_replace_file_to(replacer: &mut TextReplacer, input_path: &str, output_path: &str) -> Result<(), Box> { +pub fn replace_file_to(replacer: &mut TextReplacer, input_path: &str, output_path: &str) -> Result<(), Box> { io_error_to_rhai_error(replacer.replace_file_to(input_path, output_path)) } @@ -168,27 +172,27 @@ pub fn template_builder_open(template_path: &str) -> Result TemplateBuilder { +pub fn add_var_string(builder: TemplateBuilder, name: &str, value: &str) -> TemplateBuilder { builder.add_var(name, value) } /// Adds an integer variable to the template context -pub fn template_add_var_int(builder: TemplateBuilder, name: &str, value: i64) -> TemplateBuilder { +pub fn add_var_int(builder: TemplateBuilder, name: &str, value: i64) -> TemplateBuilder { builder.add_var(name, value) } /// Adds a float variable to the template context -pub fn template_add_var_float(builder: TemplateBuilder, name: &str, value: f64) -> TemplateBuilder { +pub fn add_var_float(builder: TemplateBuilder, name: &str, value: f64) -> TemplateBuilder { builder.add_var(name, value) } /// Adds a boolean variable to the template context -pub fn template_add_var_bool(builder: TemplateBuilder, name: &str, value: bool) -> TemplateBuilder { +pub fn add_var_bool(builder: TemplateBuilder, name: &str, value: bool) -> TemplateBuilder { builder.add_var(name, value) } /// Adds an array variable to the template context -pub fn template_add_var_array(builder: TemplateBuilder, name: &str, array: Array) -> TemplateBuilder { +pub fn add_var_array(builder: TemplateBuilder, name: &str, array: Array) -> TemplateBuilder { // Convert Rhai Array to Vec let vec: Vec = array.iter() .filter_map(|v| v.clone().into_string().ok()) @@ -198,7 +202,7 @@ pub fn template_add_var_array(builder: TemplateBuilder, name: &str, array: Array } /// Adds multiple variables to the template context from a Map -pub fn template_add_vars(builder: TemplateBuilder, vars: Map) -> TemplateBuilder { +pub fn add_vars(builder: TemplateBuilder, vars: Map) -> TemplateBuilder { // Convert Rhai Map to Rust HashMap let mut hash_map = HashMap::new(); @@ -213,11 +217,11 @@ pub fn template_add_vars(builder: TemplateBuilder, vars: Map) -> TemplateBuilder } /// Renders the template with the current context -pub fn template_render(builder: &mut TemplateBuilder) -> Result> { +pub fn render(builder: &mut TemplateBuilder) -> Result> { tera_error_to_rhai_error(builder.render()) } /// Renders the template and writes the result to a file -pub fn template_render_to_file(builder: &mut TemplateBuilder, output_path: &str) -> Result<(), Box> { +pub fn render_to_file(builder: &mut TemplateBuilder, output_path: &str) -> Result<(), Box> { io_error_to_rhai_error(builder.render_to_file(output_path)) } \ No newline at end of file diff --git a/src/rhaiexamples/text_tools.rhai b/src/rhaiexamples/text_tools.rhai index 4550c57..f902c53 100644 --- a/src/rhaiexamples/text_tools.rhai +++ b/src/rhaiexamples/text_tools.rhai @@ -6,7 +6,7 @@ println("===== TextReplacer Examples ====="); // Create a temporary file for testing let temp_file = "text_replacer_test.txt"; -write_file(temp_file, "This is a foo bar example with FOO and foo occurrences.\nAnother line with foo and bar."); +file_write(temp_file, "This is a foo bar example with FOO and foo occurrences.\nAnother line with foo and bar."); // Example 1: Simple replacement println("\n--- Example 1: Simple replacement ---"); @@ -68,7 +68,7 @@ println("\n\n===== TemplateBuilder Examples ====="); // Create a temporary template file let template_file = "template_test.txt"; -write_file(template_file, "Hello, {{ name }}! Welcome to {{ place }}.\n{% if show_greeting %}Glad to have you here!{% endif %}\nYour items:\n{% for item in items %} - {{ item }}{% if not loop.last %}\n{% endif %}{% endfor %}\n"); +file_write(template_file, "Hello, {{ name }}! Welcome to {{ place }}.\n{% if show_greeting %}Glad to have you here!{% endif %}\nYour items:\n{% for item in items %} - {{ item }}{% if not loop.last %}\n{% endif %}{% endfor %}\n"); // Example 1: Simple template rendering println("\n--- Example 1: Simple template rendering ---"); @@ -108,7 +108,7 @@ let template = template_builder_open(template_file) template.render_to_file(output_file); println(`Template rendered to file: ${output_file}`); -println(`Content of the rendered file:\n${read_file(output_file)}`); +println(`Content of the rendered file:\n${file_read(output_file)}`); // Clean up temporary files delete(template_file);