plux_rs/lib.rs
1//! # Plux - Extensible Plugin System for Rust
2//!
3//! Plux is a comprehensive plugin system for Rust applications, offering a robust and flexible
4//! architecture for extending application functionality through plugins. It enables seamless
5//! integration of third-party code while maintaining security and stability.
6//!
7//! ## Key Features
8//!
9//! - **Language Agnostic**: Write plugins in any programming language
10//! - **Hot Reloading**: Update plugins without restarting the host application
11//! - **Dynamic Loading**: Load and unload plugins at runtime
12//! - **Type Safety**: Rust's type system ensures safe plugin interactions
13//! - **Cross-Platform**: Works on all major platforms (Windows, macOS, Linux)
14//! - **Performance Optimized**: Efficient loading and caching of plugins
15//! - **Isolated Execution**: Secure sandboxing for plugin execution
16//!
17//! ## Core Components
18//!
19//! - **Loader**: Central component for managing plugin lifecycle and execution
20//! - **Manager**: Adapters providing standardized interfaces for plugin integration
21//! - **Plugin**: Self-contained modules that extend application functionality
22//!
23//! ## Quick Start
24//!
25//! ```rust,no_run
26//! use plux_rs::prelude::*;
27//! use plux_lua_manager::LuaManager;
28//!
29//! #[function]
30//! fn add(_: (), a: &i32, b: &i32) -> i32 {
31//! a + b
32//! }
33//!
34//! fn main() -> Result<(), Box<dyn std::error::Error>> {
35//! let mut loader = Loader::new();
36//!
37//! loader.context(move |mut ctx| {
38//! ctx.register_manager(LuaManager::new())?;
39//! ctx.register_function(add());
40//!
41//! // Load and manage plugins here
42//! Ok::<(), Box<dyn std::error::Error>>(())
43//! })?;
44//!
45//! Ok(())
46//! }
47//! ```
48
49#![warn(missing_docs)]
50#![doc(html_logo_url = "https://example.com/logo.png")]
51#![doc(html_favicon_url = "https://example.com/favicon.ico")]
52
53/// Context types used during plugin loading and registration.
54///
55/// This module provides the context types that are passed to plugin managers
56/// during various lifecycle events such as registration and loading.
57pub mod context;
58
59/// Function and request definitions for the plugin system.
60///
61/// This module defines the core function and request types that enable
62/// communication between plugins and the host application.
63pub mod function;
64
65/// Utility types and functions for the plugin system.
66///
67/// This module contains various utility types, error definitions, and helper
68/// functions used throughout the plugin system.
69pub mod utils;
70
71/// Variable types used for data exchange between plugins and host.
72///
73/// This module defines the Variable and VariableType enums that represent
74/// the data types that can be passed between plugins and the host application.
75pub mod variable;
76
77mod api;
78mod bundle;
79mod info;
80mod loader;
81mod manager;
82mod plugin;
83
84pub use api::*;
85pub use bundle::*;
86pub use context::*;
87pub use info::*;
88pub use loader::*;
89pub use manager::*;
90pub use plugin::*;
91
92use function::{Function, Request};
93use std::sync::Arc;
94
95/// Registry of functions that can be called by plugins.
96/// This type alias represents a collection of functions exposed by the host application
97/// that plugins can invoke during execution.
98pub type Registry<O> = Vec<Arc<dyn Function<Output = O>>>;
99
100/// Collection of function requests from plugins.
101/// This type alias represents a collection of requests that plugins can make to the host
102/// application, typically for accessing host-provided functionality.
103pub type Requests = Vec<Request>;
104
105/// Re-exports for procedural macros when the `derive` feature is enabled.
106///
107/// # Macros
108///
109/// ## `#[function]`
110///
111/// A procedural macro that transforms a Rust function into a plugin-compatible function.
112/// This macro enables the function to be called from plugins and handles serialization
113/// of arguments and return values.
114///
115/// ### Usage
116///
117/// ```rust,no_run
118/// use plux_rs::prelude::*;
119///
120/// // Basic usage with primitive types
121/// #[plux_rs::function]
122/// fn add(_: (), a: &i32, b: &i32) -> i32 {
123/// a + b
124/// }
125///
126/// // With references for better performance
127/// #[plux_rs::function]
128/// fn concat(_: (), a: &String, b: Vec<&String>) -> String {
129/// let v = b.into_iter().map(|s| s.clone()).collect::<Vec<String>>();
130/// format!("{} {}", a, v.join(" "))
131/// }
132///
133/// // With context parameter (first parameter is always the context)
134/// #[plux_rs::function]
135/// fn greet(message: &String, name: &Variable) -> String {
136/// format!("{} {}", message, name)
137/// }
138///
139/// let mut loader = Loader::<'_, FunctionOutput, StdInfo>::new();
140/// loader
141/// .context(move |mut ctx| {
142/// ctx.register_function(add());
143/// ctx.register_function(concat());
144/// ctx.register_function(greet("Hello world,".to_string()));
145///
146/// Ok::<(), Box<dyn std::error::Error>>(())
147/// })
148/// .unwrap();
149///
150/// let registry = loader.get_registry();
151///
152/// let add_function = registry.get(0).unwrap();
153/// let concat_function = registry.get(1).unwrap();
154/// let greet_function = registry.get(2).unwrap();
155///
156/// let result = add_function.call(&[1.into(), 2.into()]).unwrap().unwrap();
157/// assert_eq!(result, 3.into());
158///
159/// let result = concat_function
160/// .call(&["Hi".into(), vec!["guest", "!"].into()])
161/// .unwrap()
162/// .unwrap();
163/// assert_eq!(result, "Hi guest !".into());
164///
165/// let result = greet_function.call(&["guest".into()]).unwrap().unwrap();
166/// assert_eq!(result, "Hello world, guest".into());
167/// ```
168///
169/// ### Features
170///
171/// - **Type Safety**: Compile-time type checking of function signatures
172/// - **Zero-Copy**: Always uses references to avoid unnecessary cloning
173/// - **Context Support**: First parameter can be a context object
174///
175/// ### Notes
176///
177/// - The first parameter can be used for context (use `_` if not needed)
178/// - Supported parameter types: primitive types, `&T` and `Vec<&T>`
179/// - The function will be available to plugins under its Rust name by default
180#[cfg(feature = "derive")]
181pub use plux_codegen::function;
182
183/// Re-export of common types for plugin implementation.
184/// This module provides convenient access to the most commonly used types when
185/// implementing plugins.
186pub mod prelude {
187 pub use crate::LoaderContext;
188 pub use crate::api::*;
189 pub use crate::bundle::*;
190 pub use crate::function::*;
191 pub use crate::info::{Depend, Info, StdInfo};
192 pub use crate::loader::*;
193 pub use crate::plugin::*;
194 pub use crate::utils::*;
195 pub use crate::variable::*;
196
197 #[cfg(feature = "derive")]
198 pub use plux_codegen::*;
199}