ottl/editors.rs
1//! Standard OTTL editor functions.
2//!
3//! Editors are callback functions invoked as top-level OTTL statements (for example, `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//! <!-- vale off -->
13//! ```ignore
14//! // Start with the standard editors…
15//! let mut editors = ottl::editors::standard();
16//!
17//! // …and extend with project-specific editors if needed.
18//! editors.insert("my_editor".to_string(), Arc::new(|args: &mut dyn ottl::Args| {
19//! todo!()
20//! }));
21//! ```
22//! <!-- vale on -->
23
24use std::sync::Arc;
25
26use crate::{Args, CallbackMap, Value};
27
28/// Returns a [`CallbackMap`] pre-populated with the standard OTTL editor functions.
29///
30/// Currently includes:
31///
32/// | Editor | Signature | Description |
33/// |--------|-----------|-------------|
34/// | `set` | `set(target, value)` | Sets `target` to `value`. |
35pub fn standard() -> CallbackMap {
36 let mut map = CallbackMap::new();
37
38 map.insert(
39 "set".to_string(),
40 Arc::new(|args: &mut dyn Args| {
41 if args.len() != 2 {
42 return Err(format!("set() requires exactly 2 arguments, got {}", args.len()).into());
43 }
44 let value = args.get(1)?;
45 args.set(0, &value)?;
46 Ok(Value::Nil)
47 }),
48 );
49
50 map
51}
52
53#[cfg(test)]
54mod tests {
55 use super::*;
56
57 #[test]
58 fn standard_contains_set() {
59 let editors = standard();
60 assert!(editors.contains_key("set"), "standard editors must include `set`");
61 }
62}