Skip to main content

nichlink_plugin_host/
lib.rs

1//! Isolated execution and atomic deployment for NichLink plugins.
2//! NichLink 插件的隔离执行与原子部署。
3
4// The published surface must be readable on docs.rs without leaving the page,
5// so the lint is on for the whole crate; `clippy -D warnings` makes a new
6// undocumented public item a failure.
7// 发布表面必须能在 docs.rs 上不跳页读懂,因此 lint 开在整个 crate 上;
8// `clippy -D warnings` 会让新增的、没有文档的公开项变成失败。
9#![warn(missing_docs)]
10
11mod admission;
12mod deployment;
13mod error;
14#[cfg(feature = "wasm")]
15mod lazy_wasm;
16#[cfg(feature = "process-tools")]
17mod process;
18mod verifier;
19#[cfg(feature = "wasm")]
20mod wasm;
21
22pub use admission::{
23    OFFICIAL_LOCK, PLUGIN_LOCK_DIRECTORY, PluginAdmission, USER_LOCK, lane_for, plugin_catalog,
24};
25pub use deployment::{Deployment, HotDeployment};
26pub use error::HostError;
27#[cfg(feature = "wasm")]
28pub use lazy_wasm::{ValidationChannel, WasmPluginSlot, WasmPluginTable};
29#[cfg(feature = "process-tools")]
30pub use process::{ProcessBackend, ProcessInstance, ProcessLimits, ProcessProgram};
31pub use verifier::{Ed25519Verifier, TrustedPublicKey};
32#[cfg(feature = "wasm")]
33pub use wasm::{WasmBackend, WasmInstance, WasmLimits};
34
35use nichlink_run_method::PluginAdapter;
36
37/// One callable plugin implementation.
38/// 一个可调用的插件实现。
39pub trait PluginInstance: Send + Sync + 'static {
40    /// The execution adapter that backs this instance.
41    /// 该实例所依托的执行适配器。
42    fn adapter(&self) -> PluginAdapter;
43
44    /// Run one operation against the plugin and return its raw response bytes.
45    /// The adapter enforces its own limits and reports every failure as `HostError`.
46    /// 对插件执行一次操作并返回原始响应字节;适配器自行实施限制,并把所有失败报告为
47    /// `HostError`。
48    fn call(&self, operation: &str, input: &[u8]) -> Result<Vec<u8>, HostError>;
49
50    /// Confirm the instance answers a `health` call with exactly `ok`.
51    /// 确认实例对 `health` 调用给出的回答正好是 `ok`。
52    fn health_check(&self) -> Result<(), HostError> {
53        let response = self.call("health", &[])?;
54        if response == b"ok" {
55            Ok(())
56        } else {
57            Err(HostError::Health(format!(
58                "expected `ok`, received {:?}",
59                String::from_utf8_lossy(&response)
60            )))
61        }
62    }
63}