wasmer_cli/commands/
validate.rs

1use std::path::PathBuf;
2
3use anyhow::{Context, Result, bail};
4use clap::Parser;
5use wasmer::{Module, is_wasm};
6use wasmer_types::target::Target;
7
8use crate::backend::RuntimeOptions;
9#[derive(Debug, Parser)]
10/// The options for the `wasmer validate` subcommand
11pub struct Validate {
12    /// File to validate as WebAssembly
13    #[clap(name = "FILE")]
14    path: PathBuf,
15
16    #[clap(flatten)]
17    rt: RuntimeOptions,
18}
19
20impl Validate {
21    /// Runs logic for the `validate` subcommand
22    pub fn execute(&self) -> Result<()> {
23        self.inner_execute()
24            .context(format!("failed to validate `{}`", self.path.display()))
25    }
26    fn inner_execute(&self) -> Result<()> {
27        let module_contents = std::fs::read(&self.path)?;
28        if !is_wasm(&module_contents) {
29            bail!("`wasmer validate` only validates WebAssembly files");
30        }
31
32        let engine = self
33            .rt
34            .get_engine_for_module(&module_contents, &Target::default())?;
35        Module::validate(&engine, &module_contents)?;
36        eprintln!("Validation passed for `{}`.", self.path.display());
37        Ok(())
38    }
39}