1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
use clap::{command, Parser};
use soroban_env_host::xdr;
use std::{fmt::Debug, path::PathBuf};
use tracing::debug;

use super::SpecOutput;
use crate::{commands::config::locator, wasm};

#[derive(Parser, Debug, Clone)]
#[group(skip)]
pub struct Cmd {
    #[command(flatten)]
    wasm: wasm::Args,
    /// Output just XDR in base64
    #[arg(long, default_value = "docs")]
    output: SpecOutput,

    #[clap(flatten)]
    locator: locator::Args,
}

#[derive(thiserror::Error, Debug)]
pub enum Error {
    #[error(transparent)]
    Wasm(#[from] wasm::Error),
    #[error("missing spec for {0:?}")]
    MissingSpec(PathBuf),
    #[error(transparent)]
    Xdr(#[from] xdr::Error),
    #[error(transparent)]
    Spec(#[from] crate::utils::contract_spec::Error),
}

impl Cmd {
    pub fn run(&self) -> Result<(), Error> {
        let wasm = self.wasm.parse()?;
        debug!("File: {}", self.wasm.wasm.to_string_lossy());
        let output = match self.output {
            SpecOutput::XdrBase64 => wasm
                .spec_base64
                .clone()
                .ok_or_else(|| Error::MissingSpec(self.wasm.wasm.clone()))?,
            SpecOutput::XdrBase64Array => wasm.spec_as_json_array()?,
            SpecOutput::Docs => wasm.to_string(),
        };
        println!("{output}");
        Ok(())
    }
}