ottl/editors.rs
1//! Standard OTTL editor functions.
2//!
3//! Editors are callback functions invoked as top-level OTTL statements (e.g. `set(target, value)`).
4//! This module provides the set of editors defined by the
5//! [OpenTelemetry Transformation Language specification][ottl-funcs] so that integrators can
6//! bootstrap a [`CallbackMap`] without re-implementing common logic.
7//!
8//! [ottl-funcs]: https://github.com/open-telemetry/opentelemetry-collector-contrib/tree/release/v0.144.x/pkg/ottl/ottlfuncs
9//!
10//! # Example
11//!
12//! ```ignore
13//! // Start with the standard editors…
14//! let mut editors = ottl::editors::standard();
15//!
16//! // …and extend with project-specific editors if needed.
17//! editors.insert("my_editor".to_string(), Arc::new(|args: &mut dyn ottl::Args| {
18//! todo!()
19//! }));
20//! ```
21
22use std::sync::Arc;
23
24use crate::{Args, CallbackMap, Value};
25
26/// Returns a [`CallbackMap`] pre-populated with the standard OTTL editor functions.
27///
28/// Currently includes:
29///
30/// | Editor | Signature | Description |
31/// |--------|-----------|-------------|
32/// | `set` | `set(target, value)` | Sets `target` to `value`. |
33pub fn standard() -> CallbackMap {
34 let mut map = CallbackMap::new();
35
36 map.insert(
37 "set".to_string(),
38 Arc::new(|args: &mut dyn Args| {
39 if args.len() != 2 {
40 return Err(format!("set() requires exactly 2 arguments, got {}", args.len()).into());
41 }
42 let value = args.get(1)?;
43 args.set(0, &value)?;
44 Ok(Value::Nil)
45 }),
46 );
47
48 map
49}
50
51#[cfg(test)]
52mod tests {
53 use super::*;
54
55 #[test]
56 fn standard_contains_set() {
57 let editors = standard();
58 assert!(editors.contains_key("set"), "standard editors must include `set`");
59 }
60}