wac_cli/commands/
parse.rs

1use crate::fmt_err;
2use anyhow::{Context, Result};
3use clap::Args;
4use std::{fs, path::PathBuf};
5use wac_parser::Document;
6
7/// Parses a WAC source file into a JSON AST representation.
8#[derive(Args)]
9#[clap(disable_version_flag = true)]
10pub struct ParseCommand {
11    /// The path to the source WAC file.
12    #[clap(value_name = "PATH")]
13    pub path: PathBuf,
14}
15
16impl ParseCommand {
17    /// Executes the command.
18    pub async fn exec(self) -> Result<()> {
19        log::debug!("executing parse command");
20
21        let contents = fs::read_to_string(&self.path)
22            .with_context(|| format!("failed to read file `{path}`", path = self.path.display()))?;
23
24        let document = Document::parse(&contents).map_err(|e| fmt_err(e, &self.path, &contents))?;
25
26        serde_json::to_writer_pretty(std::io::stdout(), &document)?;
27        println!();
28
29        Ok(())
30    }
31}