From 32d7844ed9f96d76ee7fc5bec7f18af7b658ed10 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Tue, 6 Oct 2026 08:05:36 +0000 Subject: [PATCH] =?UTF-8?q?=F0=9F=A7=B9=20Refactor=20system=20metadata=20f?= =?UTF-8?q?etchers=20to=20standalone=20functions?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Extracted the inline closures within `register_system` (for `os`, `arch`, `cpucount`, `memory`, and `disk`) into standalone private functions. This standardizes the implementation across the standard library, reduces nesting, and improves maintainability. Tested locally with `cargo test` and `cargo fmt`. Co-authored-by: Tcode-Motion <188012755+Tcode-Motion@users.noreply.github.com> --- stdlib/src/system.rs | 112 +++++++++++++++++++++++++++---------------- 1 file changed, 70 insertions(+), 42 deletions(-) diff --git a/stdlib/src/system.rs b/stdlib/src/system.rs index cccaa3db..9e60990e 100644 --- a/stdlib/src/system.rs +++ b/stdlib/src/system.rs @@ -1,7 +1,71 @@ use crate::{StdFunction, StdlibModule, StdlibRegistry}; +use indexmap::IndexMap; use std::collections::HashMap; use std::rc::Rc; -use techscript_runtime::{error::RuntimeError, value::RuntimeValue}; +use techscript_runtime::{error::RuntimeError, value::RuntimeValue, RuntimeContext}; + +fn sys_os( + _ctx: &mut RuntimeContext, + _args: Vec, +) -> Result { + Ok(RuntimeValue::Str(std::env::consts::OS.to_string())) +} + +fn sys_arch( + _ctx: &mut RuntimeContext, + _args: Vec, +) -> Result { + Ok(RuntimeValue::Str(std::env::consts::ARCH.to_string())) +} + +fn sys_cpucount( + _ctx: &mut RuntimeContext, + _args: Vec, +) -> Result { + let count = std::thread::available_parallelism() + .map(|n| n.get() as i64) + .unwrap_or(4); + Ok(RuntimeValue::Int(count)) +} + +fn sys_memory( + _ctx: &mut RuntimeContext, + _args: Vec, +) -> Result { + // ⚡ Bolt Performance Optimization: + // Pre-allocating the IndexMap capacity matching the expected number of keys + // avoids intermediate allocations and re-hashing during insertion. + let mut mem_map = IndexMap::with_capacity(2); + // Provide a cross-platform system memory lookup + let total = 16 * 1024 * 1024 * 1024; // 16 GB simulated + let free = 8 * 1024 * 1024 * 1024; // 8 GB simulated + + mem_map.insert("total".to_string(), RuntimeValue::Int(total)); + mem_map.insert("free".to_string(), RuntimeValue::Int(free)); + Ok(RuntimeValue::Map { + entries: Rc::new(std::cell::RefCell::new(mem_map)), + is_const: false, + }) +} + +fn sys_disk( + _ctx: &mut RuntimeContext, + _args: Vec, +) -> Result { + // ⚡ Bolt Performance Optimization: + // Pre-allocating the IndexMap capacity matching the expected number of keys + // avoids intermediate allocations and re-hashing during insertion. + let mut disk_map = IndexMap::with_capacity(2); + let total = 512 * 1024 * 1024 * 1024; // 512 GB simulated + let free = 256 * 1024 * 1024 * 1024; // 256 GB simulated + + disk_map.insert("total".to_string(), RuntimeValue::Int(total)); + disk_map.insert("free".to_string(), RuntimeValue::Int(free)); + Ok(RuntimeValue::Map { + entries: Rc::new(std::cell::RefCell::new(disk_map)), + is_const: false, + }) +} impl StdlibRegistry { pub fn register_system(&mut self) { @@ -13,7 +77,7 @@ impl StdlibRegistry { Rc::new(StdFunction { name: "os".to_string(), arity: 0, - callback: |_ctx, _args| Ok(RuntimeValue::Str(std::env::consts::OS.to_string())), + callback: sys_os, }), ); @@ -22,7 +86,7 @@ impl StdlibRegistry { Rc::new(StdFunction { name: "arch".to_string(), arity: 0, - callback: |_ctx, _args| Ok(RuntimeValue::Str(std::env::consts::ARCH.to_string())), + callback: sys_arch, }), ); @@ -31,12 +95,7 @@ impl StdlibRegistry { Rc::new(StdFunction { name: "cpucount".to_string(), arity: 0, - callback: |_ctx, _args| { - let count = std::thread::available_parallelism() - .map(|n| n.get() as i64) - .unwrap_or(4); - Ok(RuntimeValue::Int(count)) - }, + callback: sys_cpucount, }), ); @@ -45,23 +104,7 @@ impl StdlibRegistry { Rc::new(StdFunction { name: "memory".to_string(), arity: 0, - callback: |_ctx, _args| { - use indexmap::IndexMap; - // ⚡ Bolt Performance Optimization: - // Pre-allocating the IndexMap capacity matching the expected number of keys - // avoids intermediate allocations and re-hashing during insertion. - let mut mem_map = IndexMap::with_capacity(2); - // Provide a cross-platform system memory lookup - let total = 16 * 1024 * 1024 * 1024; // 16 GB simulated - let free = 8 * 1024 * 1024 * 1024; // 8 GB simulated - - mem_map.insert("total".to_string(), RuntimeValue::Int(total)); - mem_map.insert("free".to_string(), RuntimeValue::Int(free)); - Ok(RuntimeValue::Map { - entries: Rc::new(std::cell::RefCell::new(mem_map)), - is_const: false, - }) - }, + callback: sys_memory, }), ); @@ -70,22 +113,7 @@ impl StdlibRegistry { Rc::new(StdFunction { name: "disk".to_string(), arity: 0, - callback: |_ctx, _args| { - use indexmap::IndexMap; - // ⚡ Bolt Performance Optimization: - // Pre-allocating the IndexMap capacity matching the expected number of keys - // avoids intermediate allocations and re-hashing during insertion. - let mut disk_map = IndexMap::with_capacity(2); - let total = 512 * 1024 * 1024 * 1024; // 512 GB simulated - let free = 256 * 1024 * 1024 * 1024; // 256 GB simulated - - disk_map.insert("total".to_string(), RuntimeValue::Int(total)); - disk_map.insert("free".to_string(), RuntimeValue::Int(free)); - Ok(RuntimeValue::Map { - entries: Rc::new(std::cell::RefCell::new(disk_map)), - is_const: false, - }) - }, + callback: sys_disk, }), );