Skip to content

Commit

Permalink
Deserialize the full CosmWasm API definition
Browse files Browse the repository at this point in the history
  • Loading branch information
aumetra committed Nov 4, 2024
1 parent 386a467 commit 08d06c4
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 47 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/cw-schema-codegen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ publish = false
anyhow = "1.0.89"
askama = { version = "0.12.1", default-features = false }
clap = { version = "4.5.18", features = ["derive"] }
cosmwasm-schema = { version = "=2.2.0-rc.1", path = "../schema" }
cw-schema = { version = "=2.2.0-rc.1", path = "../cw-schema" }
either = "1.13.0"
frunk = "0.4.3"
Expand Down
72 changes: 59 additions & 13 deletions packages/cw-schema-codegen/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ extern crate log;

use clap::{Parser, ValueEnum};
use std::{
collections::HashSet,
fs::{self, File},
io::{self, Write},
path::PathBuf,
Expand Down Expand Up @@ -49,9 +50,38 @@ impl Opts {
}
}

fn generate_defs<W>(
output: &mut W,
language: Language,
schema: &cw_schema::Schema,
) -> anyhow::Result<()>
where
W: io::Write,
{
let cw_schema::Schema::V1(schema) = schema else {
bail!("Only schema version 1 is supported")
};

schema.definitions.iter().try_for_each(|node| {
debug!("Processing node: {node:?}");

match language {
Language::Rust => cw_schema_codegen::rust::process_node(output, schema, node),
Language::Typescript => {
cw_schema_codegen::typescript::process_node(output, schema, node)
}
Language::Go | Language::Python => todo!(),
}
})?;

Ok(())
}

fn main() -> anyhow::Result<()> {
simple_logger::SimpleLogger::new()
.without_timestamps()
.with_level(log::LevelFilter::Info)
.env()
.init()?;

let opts: Opts = Opts::parse();
Expand All @@ -70,24 +100,40 @@ fn main() -> anyhow::Result<()> {
);

let schema = fs::read_to_string(&opts.file)?;
let schema: cw_schema::Schema = serde_json::from_str(&schema)?;
let cw_schema::Schema::V1(schema) = schema else {
bail!("Unsupported schema version");
};
let schema: cosmwasm_schema::JsonCwApi = serde_json::from_str(&schema)?;

let mut output = opts.output()?;

schema.definitions.iter().try_for_each(|node| {
debug!("Processing node: {node:?}");
if let Some(ref instantiate) = schema.instantiate {
generate_defs(&mut output, opts.language, instantiate)?;
}

match opts.language {
Language::Rust => cw_schema_codegen::rust::process_node(&mut output, &schema, node),
Language::Typescript => {
cw_schema_codegen::typescript::process_node(&mut output, &schema, node)
}
Language::Go | Language::Python => todo!(),
if let Some(ref execute) = schema.execute {
generate_defs(&mut output, opts.language, execute)?;
}

if let Some(ref query) = schema.query {
generate_defs(&mut output, opts.language, query)?;
}

if let Some(ref migrate) = schema.migrate {
generate_defs(&mut output, opts.language, migrate)?;
}

if let Some(ref sudo) = schema.sudo {
generate_defs(&mut output, opts.language, sudo)?;
}

if let Some(ref responses) = schema.responses {
let responses = responses
.iter()
.map(|(_name, response)| response)
.collect::<HashSet<_>>();

for response in responses {
generate_defs(&mut output, opts.language, response)?;
}
})?;
}

Ok(())
}
6 changes: 3 additions & 3 deletions packages/cw-schema-codegen/src/rust/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ fn expand_node_name<'a>(
cw_schema::NodeType::Enum { .. } => node.name.as_ref().into(),

cw_schema::NodeType::Decimal { precision, signed } => todo!(),
cw_schema::NodeType::Address => todo!(),
cw_schema::NodeType::Checksum => todo!(),
cw_schema::NodeType::Address => "cosmrs::AccountId".into(),
cw_schema::NodeType::Checksum => "cosmrs::tendermint::Hash".into(),
cw_schema::NodeType::HexBinary => todo!(),
cw_schema::NodeType::Timestamp => todo!(),
cw_schema::NodeType::Timestamp => "cosmrs::tendermint::Time".into(),
cw_schema::NodeType::Unit => Cow::Borrowed("()"),
}
}
Expand Down
18 changes: 5 additions & 13 deletions packages/cw-schema-codegen/src/typescript/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,7 @@ fn expand_node_name<'a>(
cw_schema::NodeType::Double => "number".into(),
cw_schema::NodeType::Boolean => "boolean".into(),
cw_schema::NodeType::String => "string".into(),
cw_schema::NodeType::Integer { signed, precision } => {
"string".into()
/*let ty = if signed { "i" } else { "u" };
format!("{ty}{precision}").into()*/
}
cw_schema::NodeType::Integer { .. } => "string".into(),
cw_schema::NodeType::Binary => "Uint8Array".into(),
cw_schema::NodeType::Optional { inner } => {
let inner = &schema.definitions[inner];
Expand All @@ -41,8 +37,8 @@ fn expand_node_name<'a>(
}
cw_schema::NodeType::Enum { .. } => node.name.as_ref().into(),

cw_schema::NodeType::Decimal { precision, signed } => todo!(),
cw_schema::NodeType::Address => todo!(),
cw_schema::NodeType::Decimal { .. } => "string".into(),
cw_schema::NodeType::Address => "string".into(),
cw_schema::NodeType::Checksum => todo!(),
cw_schema::NodeType::HexBinary => todo!(),
cw_schema::NodeType::Timestamp => todo!(),
Expand All @@ -51,12 +47,8 @@ fn expand_node_name<'a>(
}

fn prepare_docs(desc: Option<&str>) -> Cow<'_, [Cow<'_, str>]> {
desc.map(|desc| {
desc.lines()
.map(|line| line.replace('"', "\\\"").into())
.collect()
})
.unwrap_or(Cow::Borrowed(&[]))
desc.map(|desc| desc.lines().map(Into::into).collect())
.unwrap_or(Cow::Borrowed(&[]))
}

pub fn process_node<O>(
Expand Down
16 changes: 8 additions & 8 deletions packages/cw-schema/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub type DefinitionReference = usize;
mod default_impls;

#[skip_serializing_none]
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
#[derive(Clone, Debug, Eq, Hash, PartialEq, Deserialize, Serialize)]
#[cfg_attr(feature = "std", derive(::schemars::JsonSchema))]
#[serde(rename_all = "camelCase")]
pub struct StructProperty {
Expand All @@ -28,7 +28,7 @@ pub struct StructProperty {
}

#[skip_serializing_none]
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
#[derive(Clone, Debug, Eq, Hash, PartialEq, Deserialize, Serialize)]
#[cfg_attr(feature = "std", derive(::schemars::JsonSchema))]
#[serde(rename_all = "camelCase", untagged)]
pub enum StructType {
Expand All @@ -42,7 +42,7 @@ pub enum StructType {
}

#[skip_serializing_none]
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
#[derive(Clone, Debug, Eq, Hash, PartialEq, Deserialize, Serialize)]
#[cfg_attr(feature = "std", derive(::schemars::JsonSchema))]
#[serde(rename_all = "camelCase")]
pub struct EnumCase {
Expand All @@ -52,7 +52,7 @@ pub struct EnumCase {
}

#[skip_serializing_none]
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
#[derive(Clone, Debug, Eq, Hash, PartialEq, Deserialize, Serialize)]
#[cfg_attr(feature = "std", derive(::schemars::JsonSchema))]
#[serde(rename_all = "camelCase", tag = "type")]
pub enum EnumValue {
Expand All @@ -66,7 +66,7 @@ pub enum EnumValue {
}

#[skip_serializing_none]
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
#[derive(Clone, Debug, Eq, Hash, PartialEq, Deserialize, Serialize)]
#[cfg_attr(feature = "std", derive(::schemars::JsonSchema))]
#[serde(rename_all = "camelCase", tag = "type")]
pub enum NodeType {
Expand Down Expand Up @@ -113,7 +113,7 @@ pub enum NodeType {
}

#[skip_serializing_none]
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
#[derive(Clone, Debug, Eq, Hash, PartialEq, Deserialize, Serialize)]
#[cfg_attr(feature = "std", derive(::schemars::JsonSchema))]
#[serde(rename_all = "camelCase")]
pub struct Node {
Expand All @@ -124,7 +124,7 @@ pub struct Node {
}

#[skip_serializing_none]
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
#[derive(Clone, Debug, Eq, Hash, PartialEq, Deserialize, Serialize)]
#[cfg_attr(feature = "std", derive(::schemars::JsonSchema))]
#[serde(rename_all = "camelCase")]
pub struct SchemaV1 {
Expand All @@ -133,7 +133,7 @@ pub struct SchemaV1 {
}

#[skip_serializing_none]
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
#[derive(Clone, Debug, Eq, Hash, PartialEq, Deserialize, Serialize)]
#[cfg_attr(feature = "std", derive(::schemars::JsonSchema))]
#[serde(rename_all = "camelCase", tag = "type")]
#[non_exhaustive]
Expand Down
20 changes: 10 additions & 10 deletions packages/schema/src/idl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,17 +98,17 @@ impl Api {
}

/// A JSON representation of a contract's API.
#[derive(serde::Serialize)]
#[derive(serde::Deserialize, serde::Serialize)]
pub struct JsonCwApi {
contract_name: String,
contract_version: String,
idl_version: String,
instantiate: Option<cw_schema::Schema>,
execute: Option<cw_schema::Schema>,
query: Option<cw_schema::Schema>,
migrate: Option<cw_schema::Schema>,
sudo: Option<cw_schema::Schema>,
responses: Option<BTreeMap<String, cw_schema::Schema>>,
pub contract_name: String,
pub contract_version: String,
pub idl_version: String,
pub instantiate: Option<cw_schema::Schema>,
pub execute: Option<cw_schema::Schema>,
pub query: Option<cw_schema::Schema>,
pub migrate: Option<cw_schema::Schema>,
pub sudo: Option<cw_schema::Schema>,
pub responses: Option<BTreeMap<String, cw_schema::Schema>>,
}

impl JsonCwApi {
Expand Down
3 changes: 3 additions & 0 deletions packages/schema/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,6 @@ pub use cosmwasm_schema_derive::write_api;
pub use cw_schema;
pub use schemars;
pub use serde;

#[doc(hidden)]
pub use self::idl::JsonCwApi;

0 comments on commit 08d06c4

Please sign in to comment.