lean_ctx/core/output_shapers.rs
1// SPDX-License-Identifier: Apache-2.0
2//! The shapers behind `ctx_shape`. The shaping pipeline belongs to the proxy
3//! layer, which registers it at start-up (`proxy::register_shapers`, called
4//! by the CLI); the engine's `ctx_shape` tool runs whatever is registered.
5//! Without a registration (library use, tests) there is nothing to shape
6//! with, so `ctx_shape` hands the redacted output back unshaped.
7
8use std::sync::OnceLock;
9
10/// Shapes a native shell command's output: `(command, output, exit_code)`.
11pub type CommandShaper = fn(&str, &str, Option<i32>) -> String;
12/// Shapes any other native tool's output: `(output, tool_name)`.
13pub type ToolShaper = fn(&str, Option<&str>) -> String;
14
15/// The registered shaping entry points.
16#[derive(Clone, Copy)]
17pub struct OutputShapers {
18 pub command: CommandShaper,
19 pub tool: ToolShaper,
20}
21
22static SHAPERS: OnceLock<OutputShapers> = OnceLock::new();
23
24/// Register the output shapers. The first registration wins.
25pub fn register_output_shapers(shapers: OutputShapers) {
26 let _ = SHAPERS.set(shapers);
27}
28
29/// The registered output shapers, if any.
30pub fn output_shapers() -> Option<&'static OutputShapers> {
31 SHAPERS.get()
32}