From c76209eb0277c7dd61469a32f2bd923faba86249 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Tue, 27 Oct 2026 18:12:50 +0000 Subject: [PATCH] refactor(stdlib): extract show_fn and alert_fn from register_notification Co-authored-by: Tcode-Motion <188012755+Tcode-Motion@users.noreply.github.com> --- stdlib/src/notification.rs | 169 ++++++++++++++++++++----------------- 1 file changed, 91 insertions(+), 78 deletions(-) diff --git a/stdlib/src/notification.rs b/stdlib/src/notification.rs index b4898fe7..c488213c 100644 --- a/stdlib/src/notification.rs +++ b/stdlib/src/notification.rs @@ -7,89 +7,102 @@ use techscript_runtime::{ value::RuntimeValue, }; -impl StdlibRegistry { - pub fn register_notification(&mut self) { - let mut exports: HashMap> = - HashMap::new(); +fn show_fn() -> Rc { + Rc::new(StdFunction { + name: "show".to_string(), + arity: 2, + callback: |ctx, args| { + if !ctx.config.capabilities.contains(&Capability::Process) { + return Err(RuntimeError::new( + RuntimeErrorKind::InvalidOperation( + "Security policy violation: Process capability is denied".to_string(), + ), + None, + None, + )); + } + let title = args[0].to_string(); + let body = args[1].to_string(); + #[cfg(target_os = "windows")] + { + use base64::Engine; + use std::process::Command; - exports.insert( - "show".to_string(), - Rc::new(StdFunction { - name: "show".to_string(), - arity: 2, - callback: |ctx, args| { - if !ctx.config.capabilities.contains(&Capability::Process) { - return Err(RuntimeError::new( - RuntimeErrorKind::InvalidOperation( - "Security policy violation: Process capability is denied" - .to_string(), - ), - None, - None, - )); - } - let title = args[0].to_string(); - let body = args[1].to_string(); - #[cfg(target_os = "windows")] - { - use base64::Engine; - use std::process::Command; + let b64_title = base64::engine::general_purpose::STANDARD.encode( + title + .encode_utf16() + .flat_map(|c| c.to_le_bytes()) + .collect::>(), + ); + let b64_body = base64::engine::general_purpose::STANDARD.encode( + body.encode_utf16() + .flat_map(|c| c.to_le_bytes()) + .collect::>(), + ); - let b64_title = base64::engine::general_purpose::STANDARD.encode(title.encode_utf16().flat_map(|c| c.to_le_bytes()).collect::>()); - let b64_body = base64::engine::general_purpose::STANDARD.encode(body.encode_utf16().flat_map(|c| c.to_le_bytes()).collect::>()); + let script = format!( + "Add-Type -AssemblyName PresentationFramework; [System.Windows.MessageBox]::Show([System.Text.Encoding]::Unicode.GetString([System.Convert]::FromBase64String('{}')), [System.Text.Encoding]::Unicode.GetString([System.Convert]::FromBase64String('{}')))", + b64_body, b64_title + ); + let b64_script = base64::engine::general_purpose::STANDARD.encode( + script + .encode_utf16() + .flat_map(|c| c.to_le_bytes()) + .collect::>(), + ); - let script = format!( - "Add-Type -AssemblyName PresentationFramework; [System.Windows.MessageBox]::Show([System.Text.Encoding]::Unicode.GetString([System.Convert]::FromBase64String('{}')), [System.Text.Encoding]::Unicode.GetString([System.Convert]::FromBase64String('{}')))", - b64_body, b64_title - ); - let b64_script = base64::engine::general_purpose::STANDARD.encode(script.encode_utf16().flat_map(|c| c.to_le_bytes()).collect::>()); + let _ = Command::new("powershell") + .args(["-EncodedCommand", &b64_script]) + .spawn(); + } + #[cfg(not(target_os = "windows"))] + { + let _ = std::process::Command::new("notify-send") + .args([&title, &body]) + .spawn(); + } + Ok(RuntimeValue::Null) + }, + }) +} - let _ = Command::new("powershell") - .args(["-EncodedCommand", &b64_script]) - .spawn(); - } - #[cfg(not(target_os = "windows"))] - { - let _ = std::process::Command::new("notify-send") - .args([&title, &body]) - .spawn(); - } - Ok(RuntimeValue::Null) - }, - }), - ); +fn alert_fn() -> Rc { + Rc::new(StdFunction { + name: "alert".to_string(), + arity: 1, + callback: |ctx, args| { + if !ctx.config.capabilities.contains(&Capability::Process) { + return Err(RuntimeError::new( + RuntimeErrorKind::InvalidOperation( + "Security policy violation: Process capability is denied".to_string(), + ), + None, + None, + )); + } + let msg = args[0].to_string(); + #[cfg(target_os = "windows")] + { + let _ = std::process::Command::new("msg").args(["*", &msg]).spawn(); + } + #[cfg(not(target_os = "windows"))] + { + let _ = std::process::Command::new("notify-send") + .args(["Alert", &msg]) + .spawn(); + } + Ok(RuntimeValue::Null) + }, + }) +} - exports.insert( - "alert".to_string(), - Rc::new(StdFunction { - name: "alert".to_string(), - arity: 1, - callback: |ctx, args| { - if !ctx.config.capabilities.contains(&Capability::Process) { - return Err(RuntimeError::new( - RuntimeErrorKind::InvalidOperation( - "Security policy violation: Process capability is denied" - .to_string(), - ), - None, - None, - )); - } - let msg = args[0].to_string(); - #[cfg(target_os = "windows")] - { - let _ = std::process::Command::new("msg").args(["*", &msg]).spawn(); - } - #[cfg(not(target_os = "windows"))] - { - let _ = std::process::Command::new("notify-send") - .args(["Alert", &msg]) - .spawn(); - } - Ok(RuntimeValue::Null) - }, - }), - ); +impl StdlibRegistry { + pub fn register_notification(&mut self) { + let mut exports: HashMap> = + HashMap::new(); + + exports.insert("show".to_string(), show_fn()); + exports.insert("alert".to_string(), alert_fn()); self.register_module( "std.notification",