This commit is contained in:
despiegk 2025-04-05 11:09:49 +02:00
parent 8605e08a65
commit 88e4a2a4b1
3 changed files with 49 additions and 42 deletions

View File

@ -67,6 +67,9 @@ pub use crate::text::{
dedent, prefix dedent, prefix
}; };
// Re-export TextReplacer functions
pub use text::*;
// Rename copy functions to avoid conflicts // Rename copy functions to avoid conflicts
pub use os::copy as os_copy; pub use os::copy as os_copy;

View File

@ -7,6 +7,8 @@ use std::collections::HashMap;
use crate::text::{ use crate::text::{
TextReplacer, TextReplacerBuilder, TextReplacer, TextReplacerBuilder,
TemplateBuilder, TemplateBuilder,
dedent, prefix,
name_fix, path_fix
}; };
/// Register Text module functions with the Rhai engine /// Register Text module functions with the Rhai engine
@ -22,33 +24,35 @@ pub fn register_text_module(engine: &mut Engine) -> Result<(), Box<EvalAltResult
// Register types // Register types
register_text_types(engine)?; register_text_types(engine)?;
// Register TextReplacer functions // Register TextReplacer constructor
engine.register_fn("text_replacer_new", text_replacer_new); engine.register_fn("text_replacer_new", text_replacer_new);
// Register TextReplacer instance methods // Register TextReplacerBuilder instance methods
engine.register_fn("pattern", builder_pattern); engine.register_fn("pattern", pattern);
engine.register_fn("replacement", builder_replacement); engine.register_fn("replacement", replacement);
engine.register_fn("regex", builder_regex); engine.register_fn("regex", regex);
engine.register_fn("case_insensitive", builder_case_insensitive); engine.register_fn("case_insensitive", case_insensitive);
engine.register_fn("and", builder_and); engine.register_fn("and", and);
engine.register_fn("build", builder_build); engine.register_fn("build", build);
engine.register_fn("replace", replacer_replace);
engine.register_fn("replace_file", replacer_replace_file);
engine.register_fn("replace_file_in_place", replacer_replace_file_in_place);
engine.register_fn("replace_file_to", replacer_replace_file_to);
// Register TemplateBuilder functions // Register TextReplacer instance methods
engine.register_fn("replace", replace);
engine.register_fn("replace_file", replace_file);
engine.register_fn("replace_file_in_place", replace_file_in_place);
engine.register_fn("replace_file_to", replace_file_to);
// Register TemplateBuilder constructor
engine.register_fn("template_builder_open", template_builder_open); engine.register_fn("template_builder_open", template_builder_open);
// Register TemplateBuilder instance methods // Register TemplateBuilder instance methods
engine.register_fn("add_var", template_add_var_string); engine.register_fn("add_var", add_var_string);
engine.register_fn("add_var", template_add_var_int); engine.register_fn("add_var", add_var_int);
engine.register_fn("add_var", template_add_var_float); engine.register_fn("add_var", add_var_float);
engine.register_fn("add_var", template_add_var_bool); engine.register_fn("add_var", add_var_bool);
engine.register_fn("add_var", template_add_var_array); engine.register_fn("add_var", add_var_array);
engine.register_fn("add_vars", template_add_vars); engine.register_fn("add_vars", add_vars);
engine.register_fn("render", template_render); engine.register_fn("render", render);
engine.register_fn("render_to_file", template_render_to_file); engine.register_fn("render_to_file", render_to_file);
// Register Fix functions directly from text module // Register Fix functions directly from text module
engine.register_fn("name_fix", crate::text::name_fix); engine.register_fn("name_fix", crate::text::name_fix);
@ -111,52 +115,52 @@ pub fn text_replacer_new() -> TextReplacerBuilder {
} }
/// Sets the pattern to search for /// 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) builder.pattern(pat)
} }
/// Sets the replacement text /// Sets the replacement text
pub fn builder_replacement(builder: TextReplacerBuilder, rep: &str) -> TextReplacerBuilder { pub fn replacement(builder: TextReplacerBuilder, rep: &str) -> TextReplacerBuilder {
builder.replacement(rep) builder.replacement(rep)
} }
/// Sets whether to use regex /// 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) builder.regex(yes)
} }
/// Sets whether the replacement should be case-insensitive /// 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) builder.case_insensitive(yes)
} }
/// Adds another replacement operation to the chain and resets the builder for a new operation /// 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() builder.and()
} }
/// Builds the TextReplacer with all configured replacement operations /// Builds the TextReplacer with all configured replacement operations
pub fn builder_build(builder: TextReplacerBuilder) -> Result<TextReplacer, Box<EvalAltResult>> { pub fn build(builder: TextReplacerBuilder) -> Result<TextReplacer, Box<EvalAltResult>> {
string_error_to_rhai_error(builder.build()) string_error_to_rhai_error(builder.build())
} }
/// Applies all configured replacement operations to the input text /// 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) replacer.replace(input)
} }
/// Reads a file, applies all replacements, and returns the result as a string /// Reads a file, applies all replacements, and returns the result as a string
pub fn replacer_replace_file(replacer: &mut TextReplacer, path: &str) -> Result<String, Box<EvalAltResult>> { pub fn replace_file(replacer: &mut TextReplacer, path: &str) -> Result<String, Box<EvalAltResult>> {
io_error_to_rhai_error(replacer.replace_file(path)) io_error_to_rhai_error(replacer.replace_file(path))
} }
/// Reads a file, applies all replacements, and writes the result back to the file /// 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<EvalAltResult>> { pub fn replace_file_in_place(replacer: &mut TextReplacer, path: &str) -> Result<(), Box<EvalAltResult>> {
io_error_to_rhai_error(replacer.replace_file_in_place(path)) 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 /// 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<EvalAltResult>> { pub fn replace_file_to(replacer: &mut TextReplacer, input_path: &str, output_path: &str) -> Result<(), Box<EvalAltResult>> {
io_error_to_rhai_error(replacer.replace_file_to(input_path, output_path)) 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, Box
} }
/// Adds a string variable to the template context /// Adds a string variable to the template context
pub fn template_add_var_string(builder: TemplateBuilder, name: &str, value: &str) -> TemplateBuilder { pub fn add_var_string(builder: TemplateBuilder, name: &str, value: &str) -> TemplateBuilder {
builder.add_var(name, value) builder.add_var(name, value)
} }
/// Adds an integer variable to the template context /// 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) builder.add_var(name, value)
} }
/// Adds a float variable to the template context /// 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) builder.add_var(name, value)
} }
/// Adds a boolean variable to the template context /// 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) builder.add_var(name, value)
} }
/// Adds an array variable to the template context /// 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<String> // Convert Rhai Array to Vec<String>
let vec: Vec<String> = array.iter() let vec: Vec<String> = array.iter()
.filter_map(|v| v.clone().into_string().ok()) .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 /// 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 // Convert Rhai Map to Rust HashMap
let mut hash_map = HashMap::new(); 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 /// Renders the template with the current context
pub fn template_render(builder: &mut TemplateBuilder) -> Result<String, Box<EvalAltResult>> { pub fn render(builder: &mut TemplateBuilder) -> Result<String, Box<EvalAltResult>> {
tera_error_to_rhai_error(builder.render()) tera_error_to_rhai_error(builder.render())
} }
/// Renders the template and writes the result to a file /// Renders the template and writes the result to a file
pub fn template_render_to_file(builder: &mut TemplateBuilder, output_path: &str) -> Result<(), Box<EvalAltResult>> { pub fn render_to_file(builder: &mut TemplateBuilder, output_path: &str) -> Result<(), Box<EvalAltResult>> {
io_error_to_rhai_error(builder.render_to_file(output_path)) io_error_to_rhai_error(builder.render_to_file(output_path))
} }

View File

@ -6,7 +6,7 @@ println("===== TextReplacer Examples =====");
// Create a temporary file for testing // Create a temporary file for testing
let temp_file = "text_replacer_test.txt"; 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 // Example 1: Simple replacement
println("\n--- Example 1: Simple replacement ---"); println("\n--- Example 1: Simple replacement ---");
@ -68,7 +68,7 @@ println("\n\n===== TemplateBuilder Examples =====");
// Create a temporary template file // Create a temporary template file
let template_file = "template_test.txt"; 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 // Example 1: Simple template rendering
println("\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); template.render_to_file(output_file);
println(`Template rendered 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 // Clean up temporary files
delete(template_file); delete(template_file);