Skip to main content

tocat_plugins/
lib.rs

1//! Native plugins: implementations compiled into the tocat binary, as opposed
2//! to WASM modules loaded at runtime.
3//!
4//! Both kinds implement [`tocat_api::Plugin`] and are looked up through the
5//! same [`Registry`], so the relay cannot tell them apart.
6//!
7//! Every plugin is a module here, behind a cargo feature, and a feature enables
8//! both the module and whatever optional dependencies it needs: a build without
9//! `compress` never compiles zstd, and one without `wasm` never compiles
10//! wasmtime. A crate boundary would buy nothing that does not already buy.
11//!
12//! [`register_native`] is the only thing this crate exports. No module can
13//! reach another and the binary cannot reach any of them, which is what lets a
14//! plugin change shape without anything above noticing.
15
16#[cfg(feature = "block")]
17mod block;
18
19#[cfg(feature = "compress")]
20mod compress;
21
22#[cfg(feature = "limit")]
23mod limit;
24
25#[cfg(feature = "process")]
26mod process;
27
28#[cfg(feature = "rate")]
29mod rate;
30
31#[cfg(feature = "tee")]
32mod tee;
33
34#[cfg(feature = "throttle")]
35mod throttle;
36
37#[cfg(feature = "timeout")]
38mod timeout;
39
40#[cfg(feature = "wasm")]
41mod wasm;
42
43use tocat_api::Registry;
44
45/// A registry containing every plugin compiled into this binary.
46#[must_use]
47pub fn native_registry() -> Registry {
48    let mut registry = Registry::new();
49    register_native(&mut registry);
50    registry
51}
52
53/// Add the compiled-in plugins to an existing registry.
54///
55/// Separate from [`native_registry`] so a host that also loads WASM modules can
56/// populate one registry from both sources.
57pub fn register_native(registry: &mut Registry) {
58    #[cfg(feature = "block")]
59    registry.register(block::BlockFactory);
60
61    #[cfg(feature = "compress")]
62    {
63        registry.register(compress::CompressFactory);
64        registry.register(compress::DecompressFactory);
65    }
66
67    #[cfg(feature = "limit")]
68    registry.register(limit::LimitFactory);
69
70    #[cfg(feature = "process")]
71    registry.register(process::ProcessFactory);
72
73    #[cfg(feature = "rate")]
74    registry.register(rate::RateFactory);
75
76    #[cfg(feature = "tee")]
77    registry.register(tee::TeeFactory);
78
79    #[cfg(feature = "throttle")]
80    registry.register(throttle::ThrottleFactory);
81
82    #[cfg(feature = "timeout")]
83    registry.register(timeout::TimeoutFactory);
84
85    #[cfg(feature = "wasm")]
86    registry.register(wasm::WasmFactory);
87
88    // So clippy doesn't get mad if no features are enabled
89    let _ = registry;
90}