-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathRustIO.rs
More file actions
187 lines (172 loc) · 5.04 KB
/
Copy pathRustIO.rs
File metadata and controls
187 lines (172 loc) · 5.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
//! PluMA Rust Plugin Interface
//!
//! This module provides the interface for writing PluMA plugins in Rust.
//! It integrates with the `pluma-plugin-trait` crate and provides FFI exports
//! for the PluMA plugin system.
//!
//! # Example Plugin
//!
//! ```rust
//! use pluma_plugin_trait::PlumaPlugin;
//! use pluma_io::PluginManager;
//!
//! pub struct MyPlugin {
//! data: String,
//! }
//!
//! impl PlumaPlugin for MyPlugin {
//! fn new() -> Self {
//! MyPlugin { data: String::new() }
//! }
//!
//! fn input(&mut self, filename: &str) {
//! // Read input data
//! self.data = std::fs::read_to_string(filename).unwrap_or_default();
//! }
//!
//! fn run(&mut self) {
//! // Process data
//! self.data = self.data.to_uppercase();
//! }
//!
//! fn output(&mut self, filename: &str) {
//! // Write output data
//! std::fs::write(filename, &self.data).ok();
//! }
//! }
//!
//! // Export the plugin for PluMA
//! pluma_plugin_trait::export_plugin!(MyPlugin);
//! ```
use std::ffi::CStr;
use std::os::raw::c_char;
/// PluginManager provides access to PluMA runtime functions.
/// This mirrors the C++ PluginManager interface.
pub struct PluginManager;
impl PluginManager {
/// Log a message to the PluMA log file
pub fn log(msg: &str) {
eprintln!("[PluMA/Rust] {}", msg);
}
/// Check if a plugin dependency is installed
pub fn dependency(plugin: &str) {
Self::log(&format!("Checking dependency: {}", plugin));
}
/// Get the current prefix path
pub fn prefix() -> String {
std::env::var("PLUMA_PREFIX").unwrap_or_else(|_| String::from(""))
}
/// Add prefix to a filename
pub fn add_prefix(filename: &str) -> String {
format!("{}/{}", Self::prefix(), filename)
}
}
/// Convert a C string pointer to a Rust String
///
/// # Safety
/// The pointer must be a valid null-terminated C string
pub unsafe fn c_str_to_string(ptr: *const c_char) -> String {
if ptr.is_null() {
return String::new();
}
CStr::from_ptr(ptr)
.to_str()
.unwrap_or("")
.to_string()
}
/// Macro to generate FFI exports for a PluMA plugin
///
/// This macro generates the necessary C-compatible functions that PluMA
/// uses to load and execute Rust plugins.
///
/// # Usage
/// ```rust
/// use pluma_plugin_trait::PlumaPlugin;
///
/// struct MyPlugin { /* ... */ }
///
/// impl PlumaPlugin for MyPlugin {
/// // ... implementation
/// }
///
/// // Generate FFI exports
/// pluma_export_plugin!(MyPlugin);
/// ```
#[macro_export]
macro_rules! pluma_export_plugin {
($plugin_type:ty) => {
/// Create a new plugin instance
#[no_mangle]
pub extern "C" fn plugin_create() -> *mut std::ffi::c_void {
let plugin = Box::new(<$plugin_type as pluma_plugin_trait::PlumaPlugin>::new());
Box::into_raw(plugin) as *mut std::ffi::c_void
}
/// Destroy a plugin instance
#[no_mangle]
pub extern "C" fn plugin_destroy(ptr: *mut std::ffi::c_void) {
if !ptr.is_null() {
unsafe {
let _ = Box::from_raw(ptr as *mut $plugin_type);
}
}
}
/// Execute the input phase
#[no_mangle]
pub extern "C" fn plugin_input(ptr: *mut std::ffi::c_void, filename: *const std::os::raw::c_char) {
if ptr.is_null() || filename.is_null() {
return;
}
unsafe {
let plugin = &mut *(ptr as *mut $plugin_type);
let filename_str = std::ffi::CStr::from_ptr(filename)
.to_str()
.unwrap_or("");
plugin.input(filename_str);
}
}
/// Execute the run phase
#[no_mangle]
pub extern "C" fn plugin_run(ptr: *mut std::ffi::c_void) {
if ptr.is_null() {
return;
}
unsafe {
let plugin = &mut *(ptr as *mut $plugin_type);
plugin.run();
}
}
/// Execute the output phase
#[no_mangle]
pub extern "C" fn plugin_output(ptr: *mut std::ffi::c_void, filename: *const std::os::raw::c_char) {
if ptr.is_null() || filename.is_null() {
return;
}
unsafe {
let plugin = &mut *(ptr as *mut $plugin_type);
let filename_str = std::ffi::CStr::from_ptr(filename)
.to_str()
.unwrap_or("");
plugin.output(filename_str);
}
}
};
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_c_str_conversion() {
let test_str = std::ffi::CString::new("test").unwrap();
unsafe {
let result = c_str_to_string(test_str.as_ptr());
assert_eq!(result, "test");
}
}
#[test]
fn test_null_c_str() {
unsafe {
let result = c_str_to_string(std::ptr::null());
assert_eq!(result, "");
}
}
}