Gunnar

nordisk/heimdall

guests/verify-bundle/src/wasm.rs

// SPDX-License-Identifier: AGPL-3.0-only
//! The wasm export: a thin shim over [`crate::verify_bundle_pairs`], bound to
//! heimdall-wasm's WIT world (`crates/heimdall-wasm/wit/heimdall.wit`):
//! `run(input, params) -> result<output, refusal>` plus `emit-file`.
//!
//! `input` is the raw `.holger.znippy`; `params` are the job's string pairs.
//! Outputs are emitted only on Accepted. The refusal `reason` starts with the
//! stable code in brackets (`[holger-update-<n>]` or `[heimdall-bundle-<n>]`),
//! because the WIT refusal record carries one string.

#![allow(unsafe_code)] // the generated canonical-ABI glue only

wit_bindgen::generate!({ path: "../../crates/heimdall-wasm/wit/heimdall.wit", world: "job" });

struct VerifyBundle;

impl Guest for VerifyBundle {
    fn run(input: Vec<u8>, params: Vec<Param>) -> Result<Output, Refusal> {
        let o = crate::verify_bundle_pairs(
            &input,
            params.iter().map(|p| (p.key.as_str(), p.value.as_str())),
        );
        match o.verdict {
            crate::Verdict::Accepted => {
                for e in &o.emitted {
                    emit_file(&e.name, &e.bytes);
                }
                Ok(Output {
                    note: format!(
                        "accepted: {} file(s) emitted, report in {}",
                        o.emitted.len(),
                        crate::REPORT_NAME
                    ),
                })
            }
            crate::Verdict::Refused { reason, .. } => Err(Refusal { reason }),
        }
    }
}

export!(VerifyBundle);