Skip to main content

rain_metadata/cli/schema/
show.rs

1use clap::Parser;
2use std::path::PathBuf;
3use schemars::schema_for;
4use crate::meta::KnownMeta;
5use crate::cli::output::SupportedOutputEncoding;
6
7#[derive(Parser)]
8pub struct Show {
9    /// One of a set of known JSON schemas that can be produced to match a subset
10    /// of the validation performed on known metas. Additional validation beyond
11    /// what can be expressed by JSON schema is performed when parsing and
12    /// validating metadata.
13    #[arg(value_parser = clap::value_parser!(KnownMeta))]
14    schema: KnownMeta,
15    /// If provided the schema will be written to the given path instead of
16    /// stdout.
17    #[arg(short, long)]
18    output_path: Option<PathBuf>,
19    /// If true the schema will be pretty printed. Defaults to false.
20    #[arg(short, long)]
21    pretty_print: bool,
22}
23
24pub fn show(s: Show) -> anyhow::Result<()> {
25    let schema_json = match s.schema {
26        KnownMeta::OpV1 => schema_for!(crate::meta::types::op::v1::OpMeta),
27        KnownMeta::AuthoringMetaV1 => schema_for!(crate::meta::types::authoring::v1::AuthoringMeta),
28        KnownMeta::SolidityAbiV2 => {
29            schema_for!(crate::meta::types::solidity_abi::v2::SolidityAbiMeta)
30        }
31        KnownMeta::InterpreterCallerMetaV1 => {
32            schema_for!(crate::meta::types::interpreter_caller::v1::InterpreterCallerMeta)
33        }
34        other => return Err(anyhow::anyhow!("Unsupported for {} meta", other)),
35    };
36    let schema_string = if s.pretty_print {
37        serde_json::to_string_pretty(&schema_json)?
38    } else {
39        serde_json::to_string(&schema_json)?
40    };
41
42    crate::cli::output::output(
43        &s.output_path,
44        SupportedOutputEncoding::Binary,
45        schema_string.as_bytes(),
46    )
47}