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 deployment;
12mod error;
13#[cfg(feature = "wasm")]
14mod lazy_wasm;
15#[cfg(feature = "process-tools")]
16mod process;
17mod verifier;
18#[cfg(feature = "wasm")]
19mod wasm;
20
21pub use deployment::{Deployment, HotDeployment};
22pub use error::HostError;
23#[cfg(feature = "wasm")]
24pub use lazy_wasm::{ValidationChannel, WasmPluginSlot, WasmPluginTable};
25#[cfg(feature = "process-tools")]
26pub use process::{ProcessBackend, ProcessInstance, ProcessLimits, ProcessProgram};
27pub use verifier::{Ed25519Verifier, TrustedPublicKey};
28#[cfg(feature = "wasm")]
29pub use wasm::{WasmBackend, WasmInstance, WasmLimits};
30
31use nichlink_run_method::PluginAdapter;
32
33/// One callable plugin implementation.
34/// 一个可调用的插件实现。
35pub trait PluginInstance: Send + Sync + 'static {
36    /// The execution adapter that backs this instance.
37    /// 该实例所依托的执行适配器。
38    fn adapter(&self) -> PluginAdapter;
39
40    /// Run one operation against the plugin and return its raw response bytes.
41    /// The adapter enforces its own limits and reports every failure as `HostError`.
42    /// 对插件执行一次操作并返回原始响应字节;适配器自行实施限制,并把所有失败报告为
43    /// `HostError`。
44    fn call(&self, operation: &str, input: &[u8]) -> Result<Vec<u8>, HostError>;
45
46    /// Confirm the instance answers a `health` call with exactly `ok`.
47    /// 确认实例对 `health` 调用给出的回答正好是 `ok`。
48    fn health_check(&self) -> Result<(), HostError> {
49        let response = self.call("health", &[])?;
50        if response == b"ok" {
51            Ok(())
52        } else {
53            Err(HostError::Health(format!(
54                "expected `ok`, received {:?}",
55                String::from_utf8_lossy(&response)
56            )))
57        }
58    }
59}