libplugin 0.1.0

Minimal plugin framework: static registration plus runtime-loaded native plugins via a C-ABI vtable, with manifest discovery and lifecycle management
Documentation
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
// SPDX-License-Identifier: MIT
//! # libplugin — 轻量插件框架 / Minimal plugin framework
//!
//! 静态注册 + 运行时动态加载,一套生命周期,两种来源:
//! Static registration + runtime loading, one lifecycle, two sources:
//!
//! - **静态插件**:编译时注册,零开销 / **Static plugins**: registered at compile time, zero overhead
//! - **动态插件**:运行时通过 `libloading` 加载 .so/.dll/.dylib / **Dynamic plugins**: .so/.dll/.dylib loaded at runtime via `libloading`
//! - **插件发现**:扫描插件目录下的 manifest.toml / **Discovery**: scan plugin dirs for manifest.toml
//! - **生命周期**:on_load → on_message/on_tick → on_unload/on_shutdown / **Lifecycle**: on_load → on_message/on_tick → on_unload/on_shutdown
//! - **per-plugin 配置**:manifest.toml 中的 `[plugin.config]` 序列化为 JSON 传给 on_load / **Per-plugin config**: `[plugin.config]` in the manifest reaches on_load as JSON
//!
//! ## 快速开始 / Quick start
//!
//! ```rust,ignore
//! use libplugin::{Plugin, PluginManager};
//!
//! let mut mgr = PluginManager::new("/path/to/plugins");
//!
//! // 注册静态插件 / Register a static plugin
//! mgr.register(Box::new(MyPlugin));
//!
//! // 发现并加载动态插件 / Discover and load dynamic plugins
//! mgr.discover_and_load()?;
//!
//! // 调用生命周期 / Drive the lifecycle
//! mgr.load_all()?;           // 初始化所有已注册插件 / initialize all registered plugins
//! mgr.on_message("hello");   // 广播消息给所有插件 / broadcast to all plugins
//! mgr.on_tick()?;            // 周期 tick / periodic tick
//! mgr.unload_all()?;         // 卸载所有插件 / unload everything
//! ```
//!
//! ## 动态插件 ABI
//!
//! 动态插件必须导出 C 函数 `libplugin_entry() -> PluginVTable`。
//! 详见 [`vtable::PluginVTable`]。
//!
//! Dynamic plugins must export the C function
//! `libplugin_entry() -> PluginVTable`. See [`vtable::PluginVTable`].

pub mod dynamic;
pub mod error;
pub mod manifest;
pub mod vtable;

pub use error::Result;

use std::collections::HashMap;
use std::path::{Path, PathBuf};

use error::PluginError;

// ── 插件 trait / Plugin trait ──

/// 插件特质 — 所有插件(静态或动态)需实现此 trait
/// Plugin trait — all plugins (static or dynamic) must implement this.
pub trait Plugin: Send + Sync {
    /// 插件名称 / Plugin name
    fn name(&self) -> &str;

    /// 插件版本(默认 "0.1.0")/ Plugin version (defaults to "0.1.0")
    fn version(&self) -> &str {
        "0.1.0"
    }

    /// 初始化插件。config 为 JSON 配置字符串(来自 manifest.toml 的 [plugin.config])。
    /// 默认实现忽略配置。
    ///
    /// Initialize the plugin. `config` is a JSON string (from the manifest's
    /// [plugin.config]). The default implementation ignores it.
    fn on_load(&self, _config: &str) -> Result<()> {
        Ok(())
    }

    /// 反初始化插件。默认实现无操作。
    /// Deinitialize the plugin. Default is a no-op.
    fn on_unload(&self) -> Result<()> {
        Ok(())
    }

    /// 处理消息事件。返回 Some(response) 表示插件产生了输出(由宿主决定如何消费)。
    /// 默认实现忽略消息。
    ///
    /// Handle a message event. `Some(response)` marks plugin output
    /// (the host decides what to do with it). Default ignores the message.
    fn on_message(&self, _msg: &str) -> Option<String> {
        None
    }

    /// 周期性 tick 回调(由宿主的调度器驱动)。
    /// 默认实现无操作。
    ///
    /// Periodic tick callback (driven by the host's scheduler).
    /// Default is a no-op.
    fn on_tick(&self) -> Result<()> {
        Ok(())
    }

    /// 关机通知(进程退出前调用)。
    /// 默认实现无操作。
    ///
    /// Shutdown notification (called before process exit).
    /// Default is a no-op.
    fn on_shutdown(&self) -> Result<()> {
        Ok(())
    }
}

// ── 插件状态 / Plugin state ──

/// 插件运行状态 / Plugin runtime state.
#[derive(Debug, Clone)]
pub enum PluginState {
    /// 已注册但尚未初始化 / Registered but not yet initialized
    Registered,
    /// 已成功加载(on_load 成功)/ Successfully loaded (on_load succeeded)
    Loaded,
    /// 加载或运行时出错 / Load or runtime error
    Errored(String),
    /// 已卸载 / Unloaded
    Unloaded,
}

impl std::fmt::Display for PluginState {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            PluginState::Registered => write!(f, "registered"),
            PluginState::Loaded => write!(f, "loaded"),
            PluginState::Errored(e) => write!(f, "error: {}", e),
            PluginState::Unloaded => write!(f, "unloaded"),
        }
    }
}

// ── 插件条目 / Plugin entry ──

/// 已加载的插件条目(内部使用)/ A loaded plugin entry (internal).
struct PluginEntry {
    plugin: Box<dyn Plugin>,
    state: PluginState,
    /// 来源:static 或 dynamic / Source: static or dynamic
    source: PluginSource,
}

/// 插件来源 / Plugin origin.
#[derive(Debug, Clone, Copy)]
pub enum PluginSource {
    /// 编译时静态注册 / Registered at compile time
    Static,
    /// 运行时动态加载 / Loaded at runtime
    Dynamic,
}

// ── 插件管理器 / Plugin manager ──

/// 插件管理器 — 负责注册、发现、加载、卸载插件
/// Plugin manager — registration, discovery, loading, unloading.
pub struct PluginManager {
    /// 已注册/已加载的插件(按名称索引)/ Registered/loaded plugins, indexed by name
    entries: HashMap<String, PluginEntry>,
    /// 插件目录(用于动态插件发现)/ Plugin directory (for dynamic discovery)
    plugin_dir: PathBuf,
}

impl PluginManager {
    /// 创建插件管理器
    /// Create a plugin manager.
    ///
    /// # Arguments
    /// * `plugin_dir` - 插件目录路径,用于动态插件发现 / Plugin dir, used for dynamic discovery
    pub fn new<P: AsRef<Path>>(plugin_dir: P) -> Self {
        Self {
            entries: HashMap::new(),
            plugin_dir: plugin_dir.as_ref().to_path_buf(),
        }
    }

    /// 创建无插件目录的管理器(仅支持静态注册)
    /// Create a manager without a plugin dir (static registration only).
    pub fn new_static_only() -> Self {
        Self {
            entries: HashMap::new(),
            plugin_dir: PathBuf::new(),
        }
    }

    // ── 静态注册 / Static registration ──

    /// 注册一个静态插件(编译时已知)。同名插件会被覆盖。
    /// Register a static plugin (known at compile time).
    /// Same-name plugins overwrite the previous registration.
    pub fn register(&mut self, plugin: Box<dyn Plugin>) {
        let name = plugin.name().to_string();
        self.entries.insert(
            name,
            PluginEntry {
                plugin,
                state: PluginState::Registered,
                source: PluginSource::Static,
            },
        );
    }

    // ── 动态发现与加载 / Dynamic discovery & loading ──

    /// 扫描插件目录,发现并加载所有动态插件
    /// Discover and load all dynamic plugins in the plugin dir.
    ///
    /// 遍历 `plugin_dir` 下的子目录,查找 `manifest.toml`,
    /// 加载对应的动态库并调用 on_load。
    /// 单个插件失败只记录状态,不影响其余插件。
    ///
    /// Scans subdirectories of `plugin_dir` for `manifest.toml`,
    /// loads the referenced library and calls on_load.
    /// A single plugin failing only affects itself.
    pub fn discover_and_load(&mut self) -> std::result::Result<(), PluginError> {
        let discoveries = manifest::discover_plugins(&self.plugin_dir);

        for result in discoveries {
            match result {
                Ok((dir, manifest)) => {
                    let lib_path = manifest.library_path(&dir);
                    let plugin_name = manifest.plugin.name.clone();

                    if self.entries.contains_key(&plugin_name) {
                        continue;
                    }

                    if !lib_path.exists() {
                        continue;
                    }

                    match dynamic::DynamicPlugin::load_from_file(&lib_path) {
                        Ok(dyn_plugin) => {
                            let config_json = manifest.config_json();
                            let name = dyn_plugin.name().to_string();

                            // 注册到 entries / Register
                            self.entries.insert(
                                name.clone(),
                                PluginEntry {
                                    plugin: Box::new(dyn_plugin),
                                    state: PluginState::Registered,
                                    source: PluginSource::Dynamic,
                                },
                            );

                            // 调用 on_load / Initialize
                            if let Some(entry) = self.entries.get_mut(&name) {
                                match entry.plugin.on_load(&config_json) {
                                    Ok(()) => {
                                        entry.state = PluginState::Loaded;
                                    }
                                    Err(e) => {
                                        entry.state = PluginState::Errored(e.to_string());
                                    }
                                }
                            }
                        }
                        Err(_) => {
                            // 加载失败的库跳过;详细错误可从 discover 结果获取
                            // Skip unloadable libraries; details available via the error
                        }
                    }
                }
                Err(_) => {
                    // 清单解析失败的目录跳过 / Skip dirs with unparseable manifests
                }
            }
        }

        Ok(())
    }

    // ── 生命周期管理 / Lifecycle ──

    /// 初始化所有已注册但未加载的插件(调用 on_load,配置为空 JSON)
    /// Initialize all registered-but-not-loaded plugins (on_load with empty config).
    pub fn load_all(&mut self) -> Result<()> {
        let names: Vec<String> = self
            .entries
            .iter()
            .filter(|(_, e)| matches!(e.state, PluginState::Registered))
            .map(|(name, _)| name.clone())
            .collect();

        for name in names {
            if let Some(entry) = self.entries.get_mut(&name) {
                match entry.plugin.on_load("{}") {
                    Ok(()) => {
                        entry.state = PluginState::Loaded;
                    }
                    Err(e) => {
                        entry.state = PluginState::Errored(e.to_string());
                    }
                }
            }
        }
        Ok(())
    }

    /// 卸载所有已加载的插件(调用 on_unload)
    /// Unload all loaded plugins (calls on_unload).
    pub fn unload_all(&mut self) -> Result<()> {
        let names: Vec<String> = self
            .entries
            .iter()
            .filter(|(_, e)| matches!(e.state, PluginState::Loaded))
            .map(|(name, _)| name.clone())
            .collect();

        for name in &names {
            if let Some(entry) = self.entries.get_mut(name) {
                match entry.plugin.on_unload() {
                    Ok(()) => {
                        entry.state = PluginState::Unloaded;
                    }
                    Err(e) => {
                        entry.state = PluginState::Errored(e.to_string());
                    }
                }
            }
        }
        Ok(())
    }

    /// 通知所有插件关机(调用 on_shutdown)
    /// Notify all plugins of shutdown (calls on_shutdown).
    pub fn shutdown_all(&self) -> Result<()> {
        for entry in self.entries.values() {
            // 关机路径尽力而为:单个失败不阻断其余插件
            // Best effort: one failure never blocks the others
            let _ = entry.plugin.on_shutdown();
        }
        Ok(())
    }

    // ── 消息广播 / Broadcast ──

    /// 向所有已加载插件广播消息,收集插件响应
    /// Broadcast a message to all loaded plugins and collect responses.
    ///
    /// 返回所有非 None 的插件响应,顺序不保证。
    /// Returns every non-None response; order is unspecified.
    pub fn on_message(&self, msg: &str) -> Vec<(String, String)> {
        let mut responses = Vec::new();
        for (name, entry) in &self.entries {
            if !matches!(entry.state, PluginState::Loaded) {
                continue;
            }
            if let Some(response) = entry.plugin.on_message(msg) {
                responses.push((name.clone(), response));
            }
        }
        responses
    }

    /// 周期 tick 所有已加载插件。单个失败不影响其余。
    /// Tick all loaded plugins. A single failure never blocks the rest.
    pub fn on_tick(&self) -> Result<()> {
        for entry in self.entries.values() {
            if !matches!(entry.state, PluginState::Loaded) {
                continue;
            }
            let _ = entry.plugin.on_tick();
        }
        Ok(())
    }

    // ── 查询 / Queries ──

    /// 获取插件总数 / Total plugin count.
    pub fn len(&self) -> usize {
        self.entries.len()
    }

    /// 是否无插件 / Whether there are no plugins.
    pub fn is_empty(&self) -> bool {
        self.entries.is_empty()
    }

    /// 获取指定插件的状态 / State of a specific plugin.
    pub fn state(&self, name: &str) -> Option<&PluginState> {
        self.entries.get(name).map(|e| &e.state)
    }

    /// 获取所有插件名称 / Names of all plugins.
    pub fn plugin_names(&self) -> Vec<&str> {
        self.entries.keys().map(|s| s.as_str()).collect()
    }

    /// 获取所有已加载插件的名称 / Names of loaded plugins.
    pub fn loaded_names(&self) -> Vec<&str> {
        self.entries
            .iter()
            .filter(|(_, e)| matches!(e.state, PluginState::Loaded))
            .map(|(name, _)| name.as_str())
            .collect()
    }

    /// 生成健康状态报告 / Produce a health status report.
    ///
    /// 格式:`name v{version} [{source}] {state}`
    /// Format: `name v{version} [{source}] {state}`
    pub fn health_status(&self) -> HashMap<String, String> {
        self.entries
            .iter()
            .map(|(name, entry)| {
                let status = format!(
                    "{} v{} [{:?}] {}",
                    name,
                    entry.plugin.version(),
                    entry.source,
                    entry.state
                );
                (name.clone(), status)
            })
            .collect()
    }
}

impl Default for PluginManager {
    fn default() -> Self {
        Self::new_static_only()
    }
}

// ── 测试 / Tests ──

#[cfg(test)]
mod tests {
    use super::*;

    /// 测试用静态插件 / Static test plugin
    struct TestPlugin {
        name_str: String,
    }

    impl TestPlugin {
        fn new(name: &str) -> Self {
            Self {
                name_str: name.to_string(),
            }
        }
    }

    impl Plugin for TestPlugin {
        fn name(&self) -> &str {
            &self.name_str
        }
        fn version(&self) -> &str {
            "1.0.0"
        }
        fn on_load(&self, _config: &str) -> Result<()> {
            Ok(())
        }
        fn on_unload(&self) -> Result<()> {
            Ok(())
        }
        fn on_message(&self, msg: &str) -> Option<String> {
            Some(format!("[echo] {}", msg))
        }
        fn on_tick(&self) -> Result<()> {
            Ok(())
        }
        fn on_shutdown(&self) -> Result<()> {
            Ok(())
        }
    }

    #[test]
    fn test_register_and_load() {
        let mut mgr = PluginManager::new_static_only();
        assert!(mgr.is_empty());

        mgr.register(Box::new(TestPlugin::new("echo")));
        assert_eq!(mgr.len(), 1);
        assert!(mgr.state("echo").is_some());

        assert!(mgr.load_all().is_ok());
        assert!(matches!(mgr.state("echo"), Some(PluginState::Loaded)));
    }

    #[test]
    fn test_unload_all() {
        let mut mgr = PluginManager::new_static_only();
        mgr.register(Box::new(TestPlugin::new("echo")));
        assert!(mgr.load_all().is_ok());
        assert!(mgr.unload_all().is_ok());
        assert!(matches!(mgr.state("echo"), Some(PluginState::Unloaded)));
    }

    #[test]
    fn test_on_message() {
        let mut mgr = PluginManager::new_static_only();
        mgr.register(Box::new(TestPlugin::new("echo")));
        assert!(mgr.load_all().is_ok());

        let responses = mgr.on_message("hello");
        assert_eq!(responses.len(), 1);
        assert_eq!(responses[0].0, "echo");
        assert_eq!(responses[0].1, "[echo] hello");
    }

    #[test]
    fn test_on_tick() {
        let mut mgr = PluginManager::new_static_only();
        mgr.register(Box::new(TestPlugin::new("echo")));
        assert!(mgr.load_all().is_ok());
        assert!(mgr.on_tick().is_ok());
    }

    #[test]
    fn test_multiple_plugins() {
        let mut mgr = PluginManager::new_static_only();
        mgr.register(Box::new(TestPlugin::new("echo")));
        mgr.register(Box::new(TestPlugin::new("sentiment")));
        assert_eq!(mgr.len(), 2);

        assert!(mgr.load_all().is_ok());
        let loaded = mgr.loaded_names();
        assert_eq!(loaded.len(), 2);

        let responses = mgr.on_message("test");
        assert_eq!(responses.len(), 2);
    }

    #[test]
    fn test_health_status() {
        let mut mgr = PluginManager::new_static_only();
        mgr.register(Box::new(TestPlugin::new("echo")));
        assert!(mgr.load_all().is_ok());

        let status = mgr.health_status();
        assert!(status.contains_key("echo"));
        assert!(status["echo"].contains("loaded"));
    }

    #[test]
    fn test_default() {
        let mgr = PluginManager::default();
        assert!(mgr.is_empty());
    }

    #[test]
    fn test_plugin_state_display() {
        assert_eq!(format!("{}", PluginState::Registered), "registered");
        assert_eq!(format!("{}", PluginState::Loaded), "loaded");
        assert_eq!(format!("{}", PluginState::Unloaded), "unloaded");
        assert_eq!(
            format!("{}", PluginState::Errored("fail".into())),
            "error: fail"
        );
    }

    #[test]
    fn test_discover_on_missing_dir() {
        // 目录不存在时 discover_and_load 不应失败(静态插件场景)
        // discover_and_load must not fail on a missing dir (static-only setups)
        let mut mgr = PluginManager::new("/nonexistent/path/xyz");
        assert!(mgr.discover_and_load().is_ok());
        assert!(mgr.is_empty());
    }

    #[test]
    fn test_register_overwrites_same_name() {
        let mut mgr = PluginManager::new_static_only();
        mgr.register(Box::new(TestPlugin::new("echo")));
        mgr.register(Box::new(TestPlugin::new("echo")));
        assert_eq!(mgr.len(), 1);
    }
}