Skip to main content

shine_core/install/transforms/
mod.rs

1mod jsonc;
2mod template;
3
4use std::collections::BTreeMap;
5
6/// Validate transform spec names without applying them.
7pub fn validate(specs: &[String]) -> anyhow::Result<()> {
8    for spec in specs {
9        if !matches!(spec.as_str(), "jsonc-to-json" | "template") {
10            anyhow::bail!("unknown transform {spec:?} (known: jsonc-to-json, template)");
11        }
12    }
13    Ok(())
14}
15
16/// Apply a pipeline of transforms to `input`, returning the transformed bytes.
17///
18/// `env` is passed to the `template` transform; other transforms ignore it.
19pub fn apply(
20    specs: &[String],
21    input: &[u8],
22    env: &BTreeMap<String, String>,
23) -> anyhow::Result<Vec<u8>> {
24    let mut data = input.to_vec();
25    for spec in specs {
26        data = apply_one(spec, &data, env)?;
27    }
28    Ok(data)
29}
30
31fn apply_one(spec: &str, input: &[u8], env: &BTreeMap<String, String>) -> anyhow::Result<Vec<u8>> {
32    match spec {
33        "jsonc-to-json" => jsonc::apply(input),
34        "template" => template::apply(input, env),
35        _ => anyhow::bail!("unknown transform: {spec:?}"),
36    }
37}