Skip to main content

shine_core/install/transforms/
mod.rs

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