//! Rhai wrappers for OS module functions //! //! This module provides Rhai wrappers for the functions in the OS module. use rhai::{Engine, EvalAltResult, Array}; use crate::os; use super::error::{ToRhaiError, register_error_types}; /// Register OS module functions with the Rhai engine /// /// # Arguments /// /// * `engine` - The Rhai engine to register the functions with /// /// # Returns /// /// * `Result<(), Box>` - Ok if registration was successful, Err otherwise pub fn register_os_module(engine: &mut Engine) -> Result<(), Box> { // Register error types register_error_types(engine)?; // Register file system functions engine.register_fn("copy", copy); engine.register_fn("exist", exist); engine.register_fn("find_file", find_file); engine.register_fn("find_files", find_files); engine.register_fn("find_dir", find_dir); engine.register_fn("find_dirs", find_dirs); engine.register_fn("delete", delete); engine.register_fn("mkdir", mkdir); engine.register_fn("file_size", file_size); engine.register_fn("rsync", rsync); engine.register_fn("chdir", chdir); engine.register_fn("file_read", file_read); engine.register_fn("file_write", file_write); engine.register_fn("file_write_append", file_write_append); // Register command check functions engine.register_fn("which", which); engine.register_fn("cmd_ensure_exists", cmd_ensure_exists); // Register download functions engine.register_fn("download", download); engine.register_fn("download_file", download_file); engine.register_fn("download_install", download_install); engine.register_fn("chmod_exec", chmod_exec); Ok(()) } // // File System Function Wrappers // /// Wrapper for os::copy /// /// Recursively copy a file or directory from source to destination. pub fn copy(src: &str, dest: &str) -> Result> { os::copy(src, dest).to_rhai_error() } /// Wrapper for os::exist /// /// Check if a file or directory exists. pub fn exist(path: &str) -> bool { os::exist(path) } /// Wrapper for os::find_file /// /// Find a file in a directory (with support for wildcards). pub fn find_file(dir: &str, filename: &str) -> Result> { os::find_file(dir, filename).to_rhai_error() } /// Wrapper for os::find_files /// /// Find multiple files in a directory (recursive, with support for wildcards). pub fn find_files(dir: &str, filename: &str) -> Result> { let files = os::find_files(dir, filename).to_rhai_error()?; // Convert Vec to Rhai Array let mut array = Array::new(); for file in files { array.push(file.into()); } Ok(array) } /// Wrapper for os::find_dir /// /// Find a directory in a parent directory (with support for wildcards). pub fn find_dir(dir: &str, dirname: &str) -> Result> { os::find_dir(dir, dirname).to_rhai_error() } /// Wrapper for os::find_dirs /// /// Find multiple directories in a parent directory (recursive, with support for wildcards). pub fn find_dirs(dir: &str, dirname: &str) -> Result> { let dirs = os::find_dirs(dir, dirname).to_rhai_error()?; // Convert Vec to Rhai Array let mut array = Array::new(); for dir in dirs { array.push(dir.into()); } Ok(array) } /// Wrapper for os::delete /// /// Delete a file or directory (defensive - doesn't error if file doesn't exist). pub fn delete(path: &str) -> Result> { os::delete(path).to_rhai_error() } /// Wrapper for os::mkdir /// /// Create a directory and all parent directories (defensive - doesn't error if directory exists). pub fn mkdir(path: &str) -> Result> { os::mkdir(path).to_rhai_error() } /// Wrapper for os::file_size /// /// Get the size of a file in bytes. pub fn file_size(path: &str) -> Result> { os::file_size(path).to_rhai_error() } /// Wrapper for os::rsync /// /// Sync directories using rsync (or platform equivalent). pub fn rsync(src: &str, dest: &str) -> Result> { os::rsync(src, dest).to_rhai_error() } /// Wrapper for os::chdir /// /// Change the current working directory. pub fn chdir(path: &str) -> Result> { os::chdir(path).to_rhai_error() } /// Wrapper for os::file_read /// /// Read the contents of a file. pub fn file_read(path: &str) -> Result> { os::file_read(path).to_rhai_error() } /// Wrapper for os::file_write /// /// Write content to a file (creates the file if it doesn't exist, overwrites if it does). pub fn file_write(path: &str, content: &str) -> Result> { os::file_write(path, content).to_rhai_error() } /// Wrapper for os::file_write_append /// /// Append content to a file (creates the file if it doesn't exist). pub fn file_write_append(path: &str, content: &str) -> Result> { os::file_write_append(path, content).to_rhai_error() } // // Download Function Wrappers // /// Wrapper for os::download /// /// Download a file from URL to destination using the curl command. pub fn download(url: &str, dest: &str, min_size_kb: i64) -> Result> { os::download(url, dest, min_size_kb).to_rhai_error() } /// Wrapper for os::download_file /// /// Download a file from URL to a specific file destination using the curl command. pub fn download_file(url: &str, dest: &str, min_size_kb: i64) -> Result> { os::download_file(url, dest, min_size_kb).to_rhai_error() } /// Wrapper for os::download_install /// /// Download a file and install it if it's a supported package format. pub fn download_install(url: &str, min_size_kb: i64) -> Result> { os::download_install(url, min_size_kb).to_rhai_error() } /// Wrapper for os::chmod_exec /// /// Make a file executable (equivalent to chmod +x). pub fn chmod_exec(path: &str) -> Result> { os::chmod_exec(path).to_rhai_error() } /// Wrapper for os::which /// /// Check if a command exists in the system PATH. pub fn which(command: &str) -> String { os::which(command) } /// Wrapper for os::cmd_ensure_exists /// /// Ensure that one or more commands exist in the system PATH. /// If any command doesn't exist, an error is thrown. pub fn cmd_ensure_exists(commands: &str) -> Result> { os::cmd_ensure_exists(commands).to_rhai_error() }