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 = "base64")]
17mod base64;
18
19#[cfg(feature = "block")]
20mod block;
21
22#[cfg(feature = "compress")]
23mod compress;
24
25#[cfg(feature = "frame")]
26mod frame;
27
28#[cfg(feature = "hash")]
29mod hash;
30
31#[cfg(feature = "hexify")]
32mod hexify;
33
34#[cfg(feature = "limit")]
35mod limit;
36
37#[cfg(feature = "process")]
38mod process;
39
40#[cfg(feature = "rate")]
41mod rate;
42
43#[cfg(feature = "tee")]
44mod tee;
45
46#[cfg(feature = "throttle")]
47mod throttle;
48
49#[cfg(feature = "timeout")]
50mod timeout;
51
52#[cfg(feature = "wasm")]
53mod wasm;
54
55use tocat_api::Registry;
56
57/// A registry containing every plugin compiled into this binary.
58#[must_use]
59pub fn native_registry() -> Registry {
60    let mut registry = Registry::new();
61    register_native(&mut registry);
62    registry
63}
64
65/// Add the compiled-in plugins to an existing registry.
66///
67/// Separate from [`native_registry`] so a host that also loads WASM modules can
68/// populate one registry from both sources.
69pub fn register_native(registry: &mut Registry) {
70    #[cfg(feature = "base64")]
71    {
72        registry.register(base64::Base64Factory);
73        registry.register(base64::Unbase64Factory);
74    }
75
76    #[cfg(feature = "block")]
77    registry.register(block::BlockFactory);
78
79    #[cfg(feature = "compress")]
80    {
81        registry.register(compress::CompressFactory);
82        registry.register(compress::DecompressFactory);
83    }
84
85    #[cfg(feature = "frame")]
86    {
87        registry.register(frame::FrameFactory);
88        registry.register(frame::UnframeFactory);
89    }
90
91    #[cfg(feature = "hash")]
92    registry.register(hash::HashFactory);
93
94    #[cfg(feature = "hexify")]
95    {
96        registry.register(hexify::HexifyFactory);
97        registry.register(hexify::UnhexifyFactory);
98    }
99
100    #[cfg(feature = "limit")]
101    registry.register(limit::LimitFactory);
102
103    #[cfg(feature = "process")]
104    registry.register(process::ProcessFactory);
105
106    #[cfg(feature = "rate")]
107    registry.register(rate::RateFactory);
108
109    #[cfg(feature = "tee")]
110    registry.register(tee::TeeFactory);
111
112    #[cfg(feature = "throttle")]
113    registry.register(throttle::ThrottleFactory);
114
115    #[cfg(feature = "timeout")]
116    registry.register(timeout::TimeoutFactory);
117
118    #[cfg(feature = "wasm")]
119    registry.register(wasm::WasmFactory);
120
121    // So clippy doesn't get mad if no features are enabled
122    let _ = registry;
123}