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        // OpV1, SolidityAbiV2 and InterpreterCallerMetaV1 are here rather
35        // than absent: rainlanguage/rain.metadata#304 and #317 removed the
36        // models without removing the metas, so they stay magic numbers this
37        // crate can name and `magic ls` still lists them. They have no type
38        // left to derive a schema from, which is exactly what None says.
39        KnownMeta::OpV1
40        | KnownMeta::SolidityAbiV2
41        | KnownMeta::InterpreterCallerMetaV1
42        | KnownMeta::DotrainV1
43        | KnownMeta::RainlangV1
44        | KnownMeta::AuthoringMetaV2
45        | KnownMeta::ExpressionDeployerV2BytecodeV1
46        | KnownMeta::RainlangSourceV1
47        | KnownMeta::AddressList
48        | KnownMeta::DotrainSourceV1
49        | KnownMeta::OrderBuilderStateV1
50        | KnownMeta::RaindexSignedContextOracleV1 => None,
51    }
52}