lithos_sprig/
lib.rs

1// SPDX-License-Identifier: Apache-2.0 OR MIT
2//! Lithos Sprig provides a partial re-implementation of selected helpers from
3//! the `sprig` Go template library, tailored for use with Rust-based Go template
4//! interpreters such as `gitmpl`.
5
6use lithos_gotmpl_core::{
7    install_text_template_functions, FunctionRegistry, FunctionRegistryBuilder,
8};
9
10mod functions;
11
12/// Registers the sprig helpers into an existing function registry builder.
13pub fn install_sprig_functions(builder: &mut FunctionRegistryBuilder) {
14    functions::install_all(builder);
15}
16
17/// Returns a registry populated with the Go core helpers plus sprig extensions.
18pub fn sprig_functions() -> FunctionRegistry {
19    let mut builder = FunctionRegistryBuilder::new();
20    install_text_template_functions(&mut builder);
21    install_sprig_functions(&mut builder);
22    builder.build()
23}
24
25#[cfg(test)]
26mod tests {
27    use super::*;
28    use lithos_gotmpl_core::Template;
29    use serde_json::json;
30
31    #[test]
32    fn template_with_sprig_helpers() {
33        let registry = sprig_functions();
34        let template = Template::parse_with_functions(
35            "sprig",
36            "{{default \"friend\" .name | upper}}",
37            registry,
38        )
39        .unwrap();
40        let rendered = template.render(&json!({"name": "sprig"})).unwrap();
41        assert_eq!(rendered, "SPRIG");
42    }
43}