From a03f09cfed3c9cfdd4bf21b5e948410bdf726283 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Sat, 28 Mar 2026 09:21:09 +0000 Subject: [PATCH] Refactor native function registration using `define_native` - Added `define_native` method to `Environment` in `src/interpreter/environment.rs` - Refactored native function registration in `src/stdlib/*.rs` to use `define_native` - Fixed `let _ = ` unit value bindings flagged by clippy Co-authored-by: logbie <1138960+logbie@users.noreply.github.com> --- src/interpreter/environment.rs | 11 ++++ src/stdlib/core.rs | 20 +++---- src/stdlib/crypto.rs | 25 ++------ src/stdlib/filesystem.rs | 83 ++++++-------------------- src/stdlib/json.rs | 15 +---- src/stdlib/list.rs | 81 +++++++++---------------- src/stdlib/math.rs | 24 ++++---- src/stdlib/pattern.rs | 15 +---- src/stdlib/random.rs | 32 +++------- src/stdlib/text.rs | 105 ++++++++------------------------- src/stdlib/time.rs | 59 ++++-------------- 11 files changed, 131 insertions(+), 339 deletions(-) diff --git a/src/interpreter/environment.rs b/src/interpreter/environment.rs index 53d63b92..7cbb72aa 100644 --- a/src/interpreter/environment.rs +++ b/src/interpreter/environment.rs @@ -89,6 +89,17 @@ impl Environment { Ok(()) } + pub fn define_native( + &mut self, + name: &'static str, + func: crate::interpreter::value::NativeFunction, + ) { + let _ = self.define( + name, + crate::interpreter::value::Value::NativeFunction(name, func), + ); + } + /// Defines a variable in the current scope without checking parent scopes for shadowing. /// This is an optimization for when existence in parent scopes has already been checked. pub fn define_direct(&mut self, name: &str, value: Value) -> Result<(), String> { diff --git a/src/stdlib/core.rs b/src/stdlib/core.rs index 6ec54f98..39a512dc 100644 --- a/src/stdlib/core.rs +++ b/src/stdlib/core.rs @@ -32,17 +32,11 @@ pub fn native_isnothing(args: Vec) -> Result { } pub fn register_core(env: &mut Environment) { - let _ = env.define("print", Value::NativeFunction("print", native_print)); - - let _ = env.define("typeof", Value::NativeFunction("typeof", native_typeof)); - let _ = env.define( - "isnothing", - Value::NativeFunction("isnothing", native_isnothing), - ); - - let _ = env.define("type_of", Value::NativeFunction("type_of", native_typeof)); - let _ = env.define( - "is_nothing", - Value::NativeFunction("is_nothing", native_isnothing), - ); + env.define_native("print", native_print); + + env.define_native("typeof", native_typeof); + env.define_native("isnothing", native_isnothing); + + env.define_native("type_of", native_typeof); + env.define_native("is_nothing", native_isnothing); } diff --git a/src/stdlib/crypto.rs b/src/stdlib/crypto.rs index 931469a2..f486402d 100644 --- a/src/stdlib/crypto.rs +++ b/src/stdlib/crypto.rs @@ -512,26 +512,11 @@ pub fn native_generate_csrf_token(args: Vec) -> Result) -> Result { } pub fn register_filesystem(env: &mut crate::interpreter::environment::Environment) { - let _ = env.define( - "list_dir", - Value::NativeFunction("list_dir", native_list_dir), - ); - let _ = env.define("glob", Value::NativeFunction("glob", native_glob)); - let _ = env.define("rglob", Value::NativeFunction("rglob", native_rglob)); - let _ = env.define( - "path_join", - Value::NativeFunction("path_join", native_path_join), - ); - let _ = env.define( - "path_basename", - Value::NativeFunction("path_basename", native_path_basename), - ); - let _ = env.define( - "path_dirname", - Value::NativeFunction("path_dirname", native_path_dirname), - ); - let _ = env.define( - "makedirs", - Value::NativeFunction("makedirs", native_makedirs), - ); - let _ = env.define( - "file_mtime", - Value::NativeFunction("file_mtime", native_file_mtime), - ); - let _ = env.define( - "path_exists", - Value::NativeFunction("path_exists", native_path_exists), - ); - let _ = env.define("is_file", Value::NativeFunction("is_file", native_is_file)); - let _ = env.define("is_dir", Value::NativeFunction("is_dir", native_is_dir)); - let _ = env.define( - "count_lines", - Value::NativeFunction("count_lines", native_count_lines), - ); - let _ = env.define( - "path_extension", - Value::NativeFunction("path_extension", native_path_extension), - ); - let _ = env.define( - "path_stem", - Value::NativeFunction("path_stem", native_path_stem), - ); - let _ = env.define( - "file_size", - Value::NativeFunction("file_size", native_file_size), - ); - let _ = env.define( - "copy_file", - Value::NativeFunction("copy_file", native_copy_file), - ); - let _ = env.define( - "move_file", - Value::NativeFunction("move_file", native_move_file), - ); - let _ = env.define( - "remove_file", - Value::NativeFunction("remove_file", native_remove_file), - ); - let _ = env.define( - "remove_dir", - Value::NativeFunction("remove_dir", native_remove_dir), - ); + env.define_native("list_dir", native_list_dir); + env.define_native("glob", native_glob); + env.define_native("rglob", native_rglob); + env.define_native("path_join", native_path_join); + env.define_native("path_basename", native_path_basename); + env.define_native("path_dirname", native_path_dirname); + env.define_native("makedirs", native_makedirs); + env.define_native("file_mtime", native_file_mtime); + env.define_native("path_exists", native_path_exists); + env.define_native("is_file", native_is_file); + env.define_native("is_dir", native_is_dir); + env.define_native("count_lines", native_count_lines); + env.define_native("path_extension", native_path_extension); + env.define_native("path_stem", native_path_stem); + env.define_native("file_size", native_file_size); + env.define_native("copy_file", native_copy_file); + env.define_native("move_file", native_move_file); + env.define_native("remove_file", native_remove_file); + env.define_native("remove_dir", native_remove_dir); } #[cfg(test)] diff --git a/src/stdlib/json.rs b/src/stdlib/json.rs index 9bc2d835..65d60059 100644 --- a/src/stdlib/json.rs +++ b/src/stdlib/json.rs @@ -127,18 +127,9 @@ pub fn native_stringify_json_pretty(args: Vec) -> Result) -> Result { } pub fn register_list(env: &mut Environment) { - let _ = env.define("length", Value::NativeFunction("length", native_length)); - let _ = env.define("push", Value::NativeFunction("push", native_push)); - let _ = env.define("pop", Value::NativeFunction("pop", native_pop)); - let _ = env.define( - "contains", - Value::NativeFunction("contains", native_contains), - ); - let _ = env.define("indexof", Value::NativeFunction("indexof", native_indexof)); - let _ = env.define( - "index_of", - Value::NativeFunction("index_of", native_indexof), - ); + env.define_native("length", native_length); + env.define_native("push", native_push); + env.define_native("pop", native_pop); + env.define_native("contains", native_contains); + env.define_native("indexof", native_indexof); + env.define_native("index_of", native_indexof); // Batch 3: Basic List Manipulation - let _ = env.define("shift", Value::NativeFunction("shift", native_shift)); - let _ = env.define("unshift", Value::NativeFunction("unshift", native_unshift)); - let _ = env.define( - "remove_at", - Value::NativeFunction("remove_at", native_remove_at), - ); - let _ = env.define( - "removeat", - Value::NativeFunction("removeat", native_remove_at), - ); - let _ = env.define( - "insert_at", - Value::NativeFunction("insert_at", native_insert_at), - ); - let _ = env.define( - "insertat", - Value::NativeFunction("insertat", native_insert_at), - ); - let _ = env.define("clear", Value::NativeFunction("clear", native_clear)); - let _ = env.define("slice", Value::NativeFunction("slice", native_slice)); - let _ = env.define("concat", Value::NativeFunction("concat", native_concat)); - let _ = env.define( - "includes", - Value::NativeFunction("includes", native_contains), - ); + env.define_native("shift", native_shift); + env.define_native("unshift", native_unshift); + env.define_native("remove_at", native_remove_at); + env.define_native("removeat", native_remove_at); + env.define_native("insert_at", native_insert_at); + env.define_native("insertat", native_insert_at); + env.define_native("clear", native_clear); + env.define_native("slice", native_slice); + env.define_native("concat", native_concat); + env.define_native("includes", native_contains); // Batch 4: List Utilities - let _ = env.define("join", Value::NativeFunction("join", native_join)); - let _ = env.define("unique", Value::NativeFunction("unique", native_unique)); - let _ = env.define("count", Value::NativeFunction("count", native_count)); - let _ = env.define("size", Value::NativeFunction("size", native_length)); - let _ = env.define("fill", Value::NativeFunction("fill", native_fill)); + env.define_native("join", native_join); + env.define_native("unique", native_unique); + env.define_native("count", native_count); + env.define_native("size", native_length); + env.define_native("fill", native_fill); // Batch 5: Sort & Reverse - let _ = env.define("sort", Value::NativeFunction("sort", native_sort)); - let _ = env.define( - "reverse_list", - Value::NativeFunction("reverse_list", native_reverse_list), - ); + env.define_native("sort", native_sort); + env.define_native("reverse_list", native_reverse_list); // Batch 6: List Search - let _ = env.define("find", Value::NativeFunction("find", native_find)); - let _ = env.define( - "find_index", - Value::NativeFunction("find_index", native_indexof), - ); - let _ = env.define("every", Value::NativeFunction("every", native_every)); - let _ = env.define("some", Value::NativeFunction("some", native_some)); + env.define_native("find", native_find); + env.define_native("find_index", native_indexof); + env.define_native("every", native_every); + env.define_native("some", native_some); } #[cfg(test)] diff --git a/src/stdlib/math.rs b/src/stdlib/math.rs index b5a69049..8d9cfb19 100644 --- a/src/stdlib/math.rs +++ b/src/stdlib/math.rs @@ -77,18 +77,18 @@ pub fn native_tan(args: Vec) -> Result { } pub fn register_math(env: &mut Environment) { - let _ = env.define("abs", Value::NativeFunction("abs", native_abs)); - let _ = env.define("round", Value::NativeFunction("round", native_round)); - let _ = env.define("floor", Value::NativeFunction("floor", native_floor)); - let _ = env.define("ceil", Value::NativeFunction("ceil", native_ceil)); - let _ = env.define("clamp", Value::NativeFunction("clamp", native_clamp)); - let _ = env.define("min", Value::NativeFunction("min", native_min)); - let _ = env.define("max", Value::NativeFunction("max", native_max)); - let _ = env.define("power", Value::NativeFunction("power", native_power)); - let _ = env.define("sqrt", Value::NativeFunction("sqrt", native_sqrt)); - let _ = env.define("sin", Value::NativeFunction("sin", native_sin)); - let _ = env.define("cos", Value::NativeFunction("cos", native_cos)); - let _ = env.define("tan", Value::NativeFunction("tan", native_tan)); + env.define_native("abs", native_abs); + env.define_native("round", native_round); + env.define_native("floor", native_floor); + env.define_native("ceil", native_ceil); + env.define_native("clamp", native_clamp); + env.define_native("min", native_min); + env.define_native("max", native_max); + env.define_native("power", native_power); + env.define_native("sqrt", native_sqrt); + env.define_native("sin", native_sin); + env.define_native("cos", native_cos); + env.define_native("tan", native_tan); } #[cfg(test)] diff --git a/src/stdlib/pattern.rs b/src/stdlib/pattern.rs index 57ce9fd9..4b3012c9 100644 --- a/src/stdlib/pattern.rs +++ b/src/stdlib/pattern.rs @@ -8,18 +8,9 @@ use std::sync::Arc; pub fn register(env: &mut Environment) { // Register new pattern functions that work with our pattern system - let _ = env.define( - "pattern_matches", - Value::NativeFunction("pattern_matches", pattern_matches_native), - ); - let _ = env.define( - "pattern_find", - Value::NativeFunction("pattern_find", pattern_find_native), - ); - let _ = env.define( - "pattern_find_all", - Value::NativeFunction("pattern_find_all", pattern_find_all_native), - ); + env.define_native("pattern_matches", pattern_matches_native); + env.define_native("pattern_find", pattern_find_native); + env.define_native("pattern_find_all", pattern_find_all_native); // Register pattern_split - this was missing! // Note: pattern_split is called directly from the interpreter for PatternSplit expressions, // but we don't register it as a standalone function since it uses special syntax diff --git a/src/stdlib/random.rs b/src/stdlib/random.rs index 95c0ed66..dae2905b 100644 --- a/src/stdlib/random.rs +++ b/src/stdlib/random.rs @@ -148,31 +148,13 @@ pub fn native_generate_uuid(args: Vec) -> Result { /// Register all random functions in the environment pub fn register_random(env: &mut Environment) { - let _ = env.define("random", Value::NativeFunction("random", native_random)); - let _ = env.define( - "random_between", - Value::NativeFunction("random_between", native_random_between), - ); - let _ = env.define( - "random_int", - Value::NativeFunction("random_int", native_random_int), - ); - let _ = env.define( - "random_boolean", - Value::NativeFunction("random_boolean", native_random_boolean), - ); - let _ = env.define( - "random_from", - Value::NativeFunction("random_from", native_random_from), - ); - let _ = env.define( - "random_seed", - Value::NativeFunction("random_seed", native_random_seed), - ); - let _ = env.define( - "generate_uuid", - Value::NativeFunction("generate_uuid", native_generate_uuid), - ); + env.define_native("random", native_random); + env.define_native("random_between", native_random_between); + env.define_native("random_int", native_random_int); + env.define_native("random_boolean", native_random_boolean); + env.define_native("random_from", native_random_from); + env.define_native("random_seed", native_random_seed); + env.define_native("generate_uuid", native_generate_uuid); } #[cfg(test)] diff --git a/src/stdlib/text.rs b/src/stdlib/text.rs index 373ac4e6..4086a0c7 100644 --- a/src/stdlib/text.rs +++ b/src/stdlib/text.rs @@ -368,95 +368,38 @@ pub fn native_reverse_text(args: Vec) -> Result { pub fn register_text(env: &mut Environment) { // Note: length function is registered by the list module instead - let _ = env.define( - "touppercase", - Value::NativeFunction("touppercase", native_touppercase), - ); - let _ = env.define( - "tolowercase", - Value::NativeFunction("tolowercase", native_tolowercase), - ); - let _ = env.define( - "substring", - Value::NativeFunction("substring", native_substring), - ); - let _ = env.define( - "string_split", - Value::NativeFunction("string_split", native_string_split), - ); - - let _ = env.define( - "to_uppercase", - Value::NativeFunction("to_uppercase", native_touppercase), - ); - let _ = env.define( - "to_lowercase", - Value::NativeFunction("to_lowercase", native_tolowercase), - ); + env.define_native("touppercase", native_touppercase); + env.define_native("tolowercase", native_tolowercase); + env.define_native("substring", native_substring); + env.define_native("string_split", native_string_split); + + env.define_native("to_uppercase", native_touppercase); + env.define_native("to_lowercase", native_tolowercase); // New string manipulation functions - let _ = env.define("trim", Value::NativeFunction("trim", native_trim)); - let _ = env.define( - "starts_with", - Value::NativeFunction("starts_with", native_starts_with), - ); - let _ = env.define( - "ends_with", - Value::NativeFunction("ends_with", native_ends_with), - ); + env.define_native("trim", native_trim); + env.define_native("starts_with", native_starts_with); + env.define_native("ends_with", native_ends_with); // Aliases for split, startswith, endswith - let _ = env.define("split", Value::NativeFunction("split", native_string_split)); - let _ = env.define( - "startswith", - Value::NativeFunction("startswith", native_starts_with), - ); - let _ = env.define( - "endswith", - Value::NativeFunction("endswith", native_ends_with), - ); + env.define_native("split", native_string_split); + env.define_native("startswith", native_starts_with); + env.define_native("endswith", native_ends_with); // New text manipulation functions - let _ = env.define("replace", Value::NativeFunction("replace", native_replace)); - let _ = env.define( - "last_index_of", - Value::NativeFunction("last_index_of", native_last_index_of), - ); - let _ = env.define( - "lastindexof", - Value::NativeFunction("lastindexof", native_last_index_of), - ); - let _ = env.define("padleft", Value::NativeFunction("padleft", native_padleft)); - let _ = env.define( - "padright", - Value::NativeFunction("padright", native_padright), - ); - let _ = env.define( - "capitalize", - Value::NativeFunction("capitalize", native_capitalize), - ); - let _ = env.define( - "reverse", - Value::NativeFunction("reverse", native_reverse_text), - ); - let _ = env.define( - "reverse_text", - Value::NativeFunction("reverse_text", native_reverse_text), - ); + env.define_native("replace", native_replace); + env.define_native("last_index_of", native_last_index_of); + env.define_native("lastindexof", native_last_index_of); + env.define_native("padleft", native_padleft); + env.define_native("padright", native_padright); + env.define_native("capitalize", native_capitalize); + env.define_native("reverse", native_reverse_text); + env.define_native("reverse_text", native_reverse_text); // Query string and form parsing - let _ = env.define( - "parse_query_string", - Value::NativeFunction("parse_query_string", native_parse_query_string), - ); - let _ = env.define( - "parse_cookies", - Value::NativeFunction("parse_cookies", native_parse_cookies), - ); - let _ = env.define( - "parse_form_urlencoded", - Value::NativeFunction("parse_form_urlencoded", native_parse_form_urlencoded), - ); + env.define_native("parse_query_string", native_parse_query_string); + env.define_native("parse_cookies", native_parse_cookies); + env.define_native("parse_form_urlencoded", native_parse_form_urlencoded); } #[cfg(test)] diff --git a/src/stdlib/time.rs b/src/stdlib/time.rs index 0269ad16..416fbc88 100644 --- a/src/stdlib/time.rs +++ b/src/stdlib/time.rs @@ -220,50 +220,17 @@ pub fn native_current_date(args: Vec) -> Result { /// Register all time-related functions in the environment pub fn register_time(env: &mut Environment) { - let _ = env.define("today", Value::NativeFunction("today", native_today)); - let _ = env.define("now", Value::NativeFunction("now", native_now)); - let _ = env.define( - "datetime_now", - Value::NativeFunction("datetime_now", native_datetime_now), - ); - let _ = env.define( - "format_date", - Value::NativeFunction("format_date", native_format_date), - ); - let _ = env.define( - "format_time", - Value::NativeFunction("format_time", native_format_time), - ); - let _ = env.define( - "format_datetime", - Value::NativeFunction("format_datetime", native_format_datetime), - ); - let _ = env.define( - "parse_date", - Value::NativeFunction("parse_date", native_parse_date), - ); - let _ = env.define( - "parse_time", - Value::NativeFunction("parse_time", native_parse_time), - ); - let _ = env.define( - "create_time", - Value::NativeFunction("create_time", native_create_time), - ); - let _ = env.define( - "create_date", - Value::NativeFunction("create_date", native_create_date), - ); - let _ = env.define( - "add_days", - Value::NativeFunction("add_days", native_add_days), - ); - let _ = env.define( - "days_between", - Value::NativeFunction("days_between", native_days_between), - ); - let _ = env.define( - "current_date", - Value::NativeFunction("current_date", native_current_date), - ); + env.define_native("today", native_today); + env.define_native("now", native_now); + env.define_native("datetime_now", native_datetime_now); + env.define_native("format_date", native_format_date); + env.define_native("format_time", native_format_time); + env.define_native("format_datetime", native_format_datetime); + env.define_native("parse_date", native_parse_date); + env.define_native("parse_time", native_parse_time); + env.define_native("create_time", native_create_time); + env.define_native("create_date", native_create_date); + env.define_native("add_days", native_add_days); + env.define_native("days_between", native_days_between); + env.define_native("current_date", native_current_date); }