Skip to main content

rain_metadata/cli/
schema.rs

1pub mod ls;
2pub mod show;
3
4use clap::Subcommand;
5use schemars::schema::RootSchema;
6use schemars::schema_for;
7use show::Show;
8use crate::meta::KnownMeta;
9
10/// command related to meta json schema
11#[derive(Subcommand)]
12pub enum Schema {
13    /// Print all known schemas.
14    Ls,
15    /// Print a given known schema.
16    Show(Show),
17}
18
19pub fn dispatch(schema: Schema) -> anyhow::Result<()> {
20    match schema {
21        Schema::Ls => ls::ls(),
22        Schema::Show(s) => show::show(s),
23    }
24}
25
26/// Single source of truth for which metas `ls` advertises and `show` can
27/// produce. Matched exhaustively so a new [`KnownMeta`] cannot be silently
28/// absent from both.
29pub fn json_schema(meta: KnownMeta) -> Option<RootSchema> {
30    match meta {
31        KnownMeta::AuthoringMetaV1 => Some(schema_for!(
32            crate::meta::types::authoring::v1::AuthoringMeta
33        )),
34        KnownMeta::SolidityAbiV2 => Some(schema_for!(
35            crate::meta::types::solidity_abi::v2::SolidityAbiMeta
36        )),
37        KnownMeta::InterpreterCallerMetaV1 => Some(schema_for!(
38            crate::meta::types::interpreter_caller::v1::InterpreterCallerMeta
39        )),
40        // OpV1 is here rather than absent: rainlanguage/rain.metadata#304
41        // removed the model without removing the meta, so it stays a magic
42        // number this crate can name and `magic ls` still lists it. It has no
43        // type left to derive a schema from, which is exactly what None says.
44        KnownMeta::OpV1
45        | KnownMeta::DotrainV1
46        | KnownMeta::RainlangV1
47        | KnownMeta::AuthoringMetaV2
48        | KnownMeta::ExpressionDeployerV2BytecodeV1
49        | KnownMeta::RainlangSourceV1
50        | KnownMeta::AddressList
51        | KnownMeta::DotrainSourceV1
52        | KnownMeta::OrderBuilderStateV1
53        | KnownMeta::RaindexSignedContextOracleV1 => None,
54    }
55}