This commit is contained in:
2025-04-04 15:21:45 +02:00
parent eecbed4b1f
commit 071dee514a
8 changed files with 547 additions and 0 deletions

View File

@@ -42,6 +42,7 @@ pub mod os;
pub mod redisclient;
pub mod text;
pub mod virt;
pub mod rhai;
// Version information
/// Returns the version of the SAL library

60
src/rhai/error.rs Normal file
View File

@@ -0,0 +1,60 @@
//! Error handling for Rhai integration
//!
//! This module provides utilities for converting SAL error types to Rhai error types.
use rhai::{EvalAltResult, Position};
use crate::os::{FsError, DownloadError};
/// Convert a FsError to a Rhai EvalAltResult
pub fn fs_error_to_rhai_error(err: FsError) -> Box<EvalAltResult> {
let err_msg = err.to_string();
Box::new(EvalAltResult::ErrorRuntime(
err_msg.into(),
Position::NONE
))
}
/// Convert a DownloadError to a Rhai EvalAltResult
pub fn download_error_to_rhai_error(err: DownloadError) -> Box<EvalAltResult> {
let err_msg = err.to_string();
Box::new(EvalAltResult::ErrorRuntime(
err_msg.into(),
Position::NONE
))
}
/// Register error types with the Rhai engine
pub fn register_error_types(engine: &mut rhai::Engine) -> Result<(), Box<EvalAltResult>> {
// Register helper functions for error handling
// Note: We don't register the error types directly since they don't implement Clone
// Instead, we'll convert them to strings in the wrappers
// Register functions to get error messages
engine.register_fn("fs_error_message", |err_msg: &str| -> String {
format!("File system error: {}", err_msg)
});
engine.register_fn("download_error_message", |err_msg: &str| -> String {
format!("Download error: {}", err_msg)
});
Ok(())
}
/// Trait for converting SAL errors to Rhai errors
pub trait ToRhaiError<T> {
/// Convert the error to a Rhai EvalAltResult
fn to_rhai_error(self) -> Result<T, Box<EvalAltResult>>;
}
impl<T> ToRhaiError<T> for Result<T, FsError> {
fn to_rhai_error(self) -> Result<T, Box<EvalAltResult>> {
self.map_err(fs_error_to_rhai_error)
}
}
impl<T> ToRhaiError<T> for Result<T, DownloadError> {
fn to_rhai_error(self) -> Result<T, Box<EvalAltResult>> {
self.map_err(download_error_to_rhai_error)
}
}

43
src/rhai/mod.rs Normal file
View File

@@ -0,0 +1,43 @@
//! Rhai scripting integration for the SAL library
//!
//! This module provides integration with the Rhai scripting language,
//! allowing SAL functions to be called from Rhai scripts.
mod error;
mod os;
#[cfg(test)]
mod tests;
use rhai::Engine;
pub use error::*;
pub use os::*;
/// Register all SAL modules with the Rhai engine
///
/// # Arguments
///
/// * `engine` - The Rhai engine to register the modules with
///
/// # Example
///
/// ```
/// use rhai::Engine;
/// use sal::rhai;
///
/// let mut engine = Engine::new();
/// rhai::register(&mut engine);
///
/// // Now you can use SAL functions in Rhai scripts
/// let result = engine.eval::<bool>("exist('some_file.txt')").unwrap();
/// ```
pub fn register(engine: &mut Engine) -> Result<(), Box<rhai::EvalAltResult>> {
// Register OS module functions
os::register_os_module(engine)?;
// Future modules can be registered here
// e.g., process::register_process_module(engine)?;
Ok(())
}

147
src/rhai/os.rs Normal file
View File

@@ -0,0 +1,147 @@
//! 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<EvalAltResult>>` - Ok if registration was successful, Err otherwise
pub fn register_os_module(engine: &mut Engine) -> Result<(), Box<EvalAltResult>> {
// 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);
// Register download functions
engine.register_fn("download", download);
engine.register_fn("download_install", download_install);
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<String, Box<EvalAltResult>> {
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<String, Box<EvalAltResult>> {
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<Array, Box<EvalAltResult>> {
let files = os::find_files(dir, filename).to_rhai_error()?;
// Convert Vec<String> 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<String, Box<EvalAltResult>> {
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<Array, Box<EvalAltResult>> {
let dirs = os::find_dirs(dir, dirname).to_rhai_error()?;
// Convert Vec<String> 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<String, Box<EvalAltResult>> {
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<String, Box<EvalAltResult>> {
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<i64, Box<EvalAltResult>> {
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<String, Box<EvalAltResult>> {
os::rsync(src, dest).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<String, Box<EvalAltResult>> {
os::download(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<String, Box<EvalAltResult>> {
os::download_install(url, min_size_kb).to_rhai_error()
}

93
src/rhai/tests.rs Normal file
View File

@@ -0,0 +1,93 @@
//! Tests for Rhai integration
//!
//! This module contains tests for the Rhai integration.
#[cfg(test)]
mod tests {
use rhai::Engine;
use super::super::register;
use std::fs;
use std::path::Path;
#[test]
fn test_register() {
let mut engine = Engine::new();
assert!(register(&mut engine).is_ok());
}
#[test]
fn test_exist_function() {
let mut engine = Engine::new();
register(&mut engine).unwrap();
// Test with a file that definitely exists
let result = engine.eval::<bool>(r#"exist("Cargo.toml")"#).unwrap();
assert!(result);
// Test with a file that definitely doesn't exist
let result = engine.eval::<bool>(r#"exist("non_existent_file.xyz")"#).unwrap();
assert!(!result);
}
#[test]
fn test_mkdir_and_delete() {
let mut engine = Engine::new();
register(&mut engine).unwrap();
let test_dir = "test_rhai_dir";
// Clean up from previous test runs if necessary
if Path::new(test_dir).exists() {
fs::remove_dir_all(test_dir).unwrap();
}
// Create directory using Rhai
let script = format!(r#"mkdir("{}")"#, test_dir);
let result = engine.eval::<String>(&script).unwrap();
assert!(result.contains("Successfully created directory"));
assert!(Path::new(test_dir).exists());
// Delete directory using Rhai
let script = format!(r#"delete("{}")"#, test_dir);
let result = engine.eval::<String>(&script).unwrap();
assert!(result.contains("Successfully deleted directory"));
assert!(!Path::new(test_dir).exists());
}
#[test]
fn test_file_size() {
let mut engine = Engine::new();
register(&mut engine).unwrap();
// Create a test file
let test_file = "test_rhai_file.txt";
let test_content = "Hello, Rhai!";
fs::write(test_file, test_content).unwrap();
// Get file size using Rhai
let script = format!(r#"file_size("{}")"#, test_file);
let size = engine.eval::<i64>(&script).unwrap();
assert_eq!(size, test_content.len() as i64);
// Clean up
fs::remove_file(test_file).unwrap();
}
#[test]
fn test_error_handling() {
let mut engine = Engine::new();
register(&mut engine).unwrap();
// Try to get the size of a non-existent file
let result = engine.eval::<i64>(r#"file_size("non_existent_file.xyz")"#);
assert!(result.is_err());
let err = result.unwrap_err();
let err_str = err.to_string();
println!("Error string: {}", err_str);
// The actual error message is "No files found matching..."
assert!(err_str.contains("No files found matching") ||
err_str.contains("File not found") ||
err_str.contains("File system error"));
}
}