3.5 KiB
3.5 KiB
Implementation Plan: Rhai Integration for OS Module Functions
1. Project Structure Changes
We'll create a new directory structure for the Rhai integration:
src/
├── rhai/
│ ├── mod.rs # Main module file for Rhai integration
│ ├── os.rs # OS module wrappers
│ └── error.rs # Error type conversions
2. Dependencies
Add Rhai as a dependency in Cargo.toml:
[dependencies]
# Existing dependencies...
rhai = { version = "1.12.0", features = ["sync"] }
3. Implementation Steps
3.1. Create Basic Module Structure
-
Create the
src/rhai/mod.rs
file with:- Module declarations
- A modular registration system
- Public exports
-
Create the
src/rhai/error.rs
file with:- Conversions from our custom error types to Rhai's
EvalAltResult
- Helper functions for error handling
- Conversions from our custom error types to Rhai's
3.2. Implement OS Module Wrappers
Create the src/rhai/os.rs
file with:
- Import necessary modules and types
- Create wrapper functions for each function in
src/os/fs.rs
andsrc/os/download.rs
- Implement a registration function specific to the OS module
- Expose error types to Rhai
3.3. Update Main Library File
Update src/lib.rs
to expose the new Rhai module.
4. Detailed Implementation
4.1. Error Handling
For each function that returns a Result<T, E>
where E
is one of our custom error types:
- Create a wrapper function that converts our error type to Rhai's
EvalAltResult
- Register the error types with Rhai to allow for proper error handling in scripts
4.2. Function Wrappers
For each function in the OS module:
- Create a wrapper function with the same name
- Handle any necessary type conversions
- Convert error types to Rhai's error system
- Register the function with the Rhai engine
4.3. Registration System
Create a modular registration system:
- Implement a general
register
function that takes a Rhai engine - Implement module-specific registration functions (e.g.,
register_os_module
) - Design the system to be extensible for future modules
5. Implementation Flow Diagram
flowchart TD
A[Add Rhai dependency] --> B[Create directory structure]
B --> C[Implement error conversions]
C --> D[Implement OS module wrappers]
D --> E[Create registration system]
E --> F[Update lib.rs]
F --> G[Test the implementation]
6. Function Mapping
Here's a mapping of the OS module functions to their Rhai wrappers:
File System Functions (from fs.rs)
copy
→ Wrap with error conversionexist
→ Direct wrapper (returns bool)find_file
→ Wrap with error conversionfind_files
→ Wrap with error conversionfind_dir
→ Wrap with error conversionfind_dirs
→ Wrap with error conversiondelete
→ Wrap with error conversionmkdir
→ Wrap with error conversionfile_size
→ Wrap with error conversionrsync
→ Wrap with error conversion
Download Functions (from download.rs)
download
→ Wrap with error conversiondownload_install
→ Wrap with error conversion
7. Error Type Handling
We'll expose our custom error types to Rhai:
- Register
FsError
andDownloadError
as custom types - Implement proper error conversion to allow for detailed error handling in Rhai scripts
- Create helper functions to extract error information
8. Testing Strategy
- Create unit tests for each wrapper function
- Create integration tests with sample Rhai scripts
- Test error handling scenarios