diff --git a/packages/cw-schema-codegen/Cargo.toml b/packages/cw-schema-codegen/Cargo.toml index 5acc60be6..afc6d6987 100644 --- a/packages/cw-schema-codegen/Cargo.toml +++ b/packages/cw-schema-codegen/Cargo.toml @@ -4,6 +4,7 @@ authors = ["Aumetra Weisman "] edition = "2021" version.workspace = true publish = false +build = "build.rs" [dependencies] anyhow = "1.0.89" diff --git a/packages/cw-schema-codegen/build.rs b/packages/cw-schema-codegen/build.rs new file mode 100644 index 000000000..73f98cda4 --- /dev/null +++ b/packages/cw-schema-codegen/build.rs @@ -0,0 +1,3 @@ +fn main() { + println!("cargo:rerun-if-changed=templates"); +} diff --git a/packages/cw-schema-codegen/src/go/template.rs b/packages/cw-schema-codegen/src/go/template.rs index ea807e3a2..90c02e1b2 100644 --- a/packages/cw-schema-codegen/src/go/template.rs +++ b/packages/cw-schema-codegen/src/go/template.rs @@ -1,9 +1,41 @@ use askama::Template; +use std::borrow::Cow; + +#[derive(Clone)] +pub struct EnumVariantTemplate<'a> { + pub name: Cow<'a, str>, + pub docs: Cow<'a, [Cow<'a, str>]>, + pub ty: TypeTemplate<'a>, +} #[derive(Template)] #[template(escape = "none", path = "go/enum.tpl.go")] -pub struct EnumTemplate {} +pub struct EnumTemplate<'a> { + pub name: Cow<'a, str>, + pub docs: Cow<'a, [Cow<'a, str>]>, + pub variants: Cow<'a, [EnumVariantTemplate<'a>]>, +} + +#[derive(Clone)] +pub struct FieldTemplate<'a> { + pub name: Cow<'a, str>, + pub docs: Cow<'a, [Cow<'a, str>]>, + pub ty: Cow<'a, str>, +} + +#[derive(Clone)] +pub enum TypeTemplate<'a> { + Unit, + Tuple(Cow<'a, [Cow<'a, str>]>), + Named { + fields: Cow<'a, [FieldTemplate<'a>]>, + }, +} #[derive(Template)] #[template(escape = "none", path = "go/struct.tpl.go")] -pub struct StructTemplate {} +pub struct StructTemplate<'a> { + pub name: Cow<'a, str>, + pub docs: Cow<'a, [Cow<'a, str>]>, + pub ty: TypeTemplate<'a>, +} diff --git a/packages/cw-schema-codegen/templates/go/enum.tpl.go b/packages/cw-schema-codegen/templates/go/enum.tpl.go index e69de29bb..ec0d54cc8 100644 --- a/packages/cw-schema-codegen/templates/go/enum.tpl.go +++ b/packages/cw-schema-codegen/templates/go/enum.tpl.go @@ -0,0 +1,2 @@ +// This code is @generated by cw-schema-codegen. Do not modify this manually. + diff --git a/packages/cw-schema-codegen/templates/go/struct.tpl.go b/packages/cw-schema-codegen/templates/go/struct.tpl.go index e69de29bb..48b420e7f 100644 --- a/packages/cw-schema-codegen/templates/go/struct.tpl.go +++ b/packages/cw-schema-codegen/templates/go/struct.tpl.go @@ -0,0 +1,21 @@ +// This code is @generated by cw-schema-codegen. Do not modify this manually. + +package cwcodegen + +{% for doc in docs %} + // {{ doc }} +{% endfor %} +type {{ name }} struct { + {% match ty %} + {% when TypeTemplate::Unit %} + {% when TypeTemplate::Tuple with (types) %} + {{ todo!() }} + {% when TypeTemplate::Named with { fields } %} + {% for field in fields %} + {% for doc in docs %} + // {{ doc }} + {% endfor %} + {{ field.name|capitalize }} {{ field.ty }} `json:"{{ field.name }}"` + {% endfor %} + {% endmatch %} +} diff --git a/packages/cw-schema-codegen/templates/typescript/enum.tpl.ts b/packages/cw-schema-codegen/templates/typescript/enum.tpl.ts index f2b897ccf..70c675385 100644 --- a/packages/cw-schema-codegen/templates/typescript/enum.tpl.ts +++ b/packages/cw-schema-codegen/templates/typescript/enum.tpl.ts @@ -35,6 +35,10 @@ type {{ name }} = } } {% endmatch %} {% endfor %} + +{% if variants.len() == 0 %} + never; +{% endif %} ; export { {{ name }} }; diff --git a/packages/cw-schema-codegen/tests/snapshots/rust_tpl__complex_enum.snap b/packages/cw-schema-codegen/tests/snapshots/rust_tpl__complex_enum.snap index d219e9437..00c3725ff 100644 --- a/packages/cw-schema-codegen/tests/snapshots/rust_tpl__complex_enum.snap +++ b/packages/cw-schema-codegen/tests/snapshots/rust_tpl__complex_enum.snap @@ -2,6 +2,9 @@ source: packages/cw-schema-codegen/tests/rust_tpl.rs expression: rendered --- +// This code is @generated by cw-schema-codegen. Do not modify this manually. + + #[doc = "Complex enum"] @@ -18,9 +21,7 @@ pub enum Complex { One ( - - u64, - + u64 ) , diff --git a/packages/cw-schema-codegen/tests/snapshots/rust_tpl__empty_enum.snap b/packages/cw-schema-codegen/tests/snapshots/rust_tpl__empty_enum.snap index 876c882b0..8e97ca0d4 100644 --- a/packages/cw-schema-codegen/tests/snapshots/rust_tpl__empty_enum.snap +++ b/packages/cw-schema-codegen/tests/snapshots/rust_tpl__empty_enum.snap @@ -2,6 +2,9 @@ source: packages/cw-schema-codegen/tests/rust_tpl.rs expression: rendered --- +// This code is @generated by cw-schema-codegen. Do not modify this manually. + + #[doc = "Empty enum"] diff --git a/packages/cw-schema-codegen/tests/snapshots/rust_tpl__empty_struct.snap b/packages/cw-schema-codegen/tests/snapshots/rust_tpl__empty_struct.snap index 6766ff832..be4cd9a49 100644 --- a/packages/cw-schema-codegen/tests/snapshots/rust_tpl__empty_struct.snap +++ b/packages/cw-schema-codegen/tests/snapshots/rust_tpl__empty_struct.snap @@ -2,6 +2,9 @@ source: packages/cw-schema-codegen/tests/rust_tpl.rs expression: rendered --- +// This code is @generated by cw-schema-codegen. Do not modify this manually. + + #[doc = "Empty struct"] diff --git a/packages/cw-schema-codegen/tests/snapshots/rust_tpl__named_struct.snap b/packages/cw-schema-codegen/tests/snapshots/rust_tpl__named_struct.snap index f37ab2f6b..b6d3ba954 100644 --- a/packages/cw-schema-codegen/tests/snapshots/rust_tpl__named_struct.snap +++ b/packages/cw-schema-codegen/tests/snapshots/rust_tpl__named_struct.snap @@ -2,6 +2,9 @@ source: packages/cw-schema-codegen/tests/rust_tpl.rs expression: rendered --- +// This code is @generated by cw-schema-codegen. Do not modify this manually. + + #[doc = "Named struct"] diff --git a/packages/cw-schema-codegen/tests/snapshots/rust_tpl__simple_enum.snap b/packages/cw-schema-codegen/tests/snapshots/rust_tpl__simple_enum.snap index c5d5dd0f9..7d5b52b2e 100644 --- a/packages/cw-schema-codegen/tests/snapshots/rust_tpl__simple_enum.snap +++ b/packages/cw-schema-codegen/tests/snapshots/rust_tpl__simple_enum.snap @@ -2,6 +2,9 @@ source: packages/cw-schema-codegen/tests/rust_tpl.rs expression: rendered --- +// This code is @generated by cw-schema-codegen. Do not modify this manually. + + #[doc = "Simple enum"] diff --git a/packages/cw-schema-codegen/tests/snapshots/rust_tpl__tuple_struct.snap b/packages/cw-schema-codegen/tests/snapshots/rust_tpl__tuple_struct.snap index 54d55450b..90eff5f1c 100644 --- a/packages/cw-schema-codegen/tests/snapshots/rust_tpl__tuple_struct.snap +++ b/packages/cw-schema-codegen/tests/snapshots/rust_tpl__tuple_struct.snap @@ -2,6 +2,9 @@ source: packages/cw-schema-codegen/tests/rust_tpl.rs expression: rendered --- +// This code is @generated by cw-schema-codegen. Do not modify this manually. + + #[doc = "Tuple struct"] @@ -10,9 +13,5 @@ pub struct Tuple ( - - u64, - - String, - + u64, String ); diff --git a/packages/cw-schema-codegen/tests/snapshots/typescript__codegen-2.snap b/packages/cw-schema-codegen/tests/snapshots/typescript__codegen-2.snap new file mode 100644 index 000000000..7cfb7b4a7 --- /dev/null +++ b/packages/cw-schema-codegen/tests/snapshots/typescript__codegen-2.snap @@ -0,0 +1,17 @@ +--- +source: packages/cw-schema-codegen/tests/typescript.rs +expression: output +--- +// This code is @generated by cw-schema-codegen. Do not modify this manually. + +/** + + */ + +type Uwu = + + [string, string] + +; + +export { Uwu }; diff --git a/packages/cw-schema-codegen/tests/snapshots/typescript__codegen-3.snap b/packages/cw-schema-codegen/tests/snapshots/typescript__codegen-3.snap new file mode 100644 index 000000000..c13999ac2 --- /dev/null +++ b/packages/cw-schema-codegen/tests/snapshots/typescript__codegen-3.snap @@ -0,0 +1,17 @@ +--- +source: packages/cw-schema-codegen/tests/typescript.rs +expression: output +--- +// This code is @generated by cw-schema-codegen. Do not modify this manually. + +/** + + */ + +type Òwó = + + void + +; + +export { Òwó }; diff --git a/packages/cw-schema-codegen/tests/snapshots/typescript__codegen-4.snap b/packages/cw-schema-codegen/tests/snapshots/typescript__codegen-4.snap new file mode 100644 index 000000000..9f0cb760f --- /dev/null +++ b/packages/cw-schema-codegen/tests/snapshots/typescript__codegen-4.snap @@ -0,0 +1,19 @@ +--- +source: packages/cw-schema-codegen/tests/typescript.rs +expression: output +--- +// This code is @generated by cw-schema-codegen. Do not modify this manually. + +/** + + */ + +type Empty = + + + + never; + +; + +export { Empty }; diff --git a/packages/cw-schema-codegen/tests/snapshots/typescript__codegen-5.snap b/packages/cw-schema-codegen/tests/snapshots/typescript__codegen-5.snap new file mode 100644 index 000000000..5bd7258ec --- /dev/null +++ b/packages/cw-schema-codegen/tests/snapshots/typescript__codegen-5.snap @@ -0,0 +1,55 @@ +--- +source: packages/cw-schema-codegen/tests/typescript.rs +expression: output +--- +// This code is @generated by cw-schema-codegen. Do not modify this manually. + +/** + + */ + +type Hehehe = + + | + + /** + + */ + + + { "A": {} } + + + | + + /** + + */ + + + { "B": [string] } + + + | + + /** + + */ + + + { "C": { + + /** + + */ + + field: string; + + } } + + + + +; + +export { Hehehe }; diff --git a/packages/cw-schema-codegen/tests/snapshots/typescript__codegen.snap b/packages/cw-schema-codegen/tests/snapshots/typescript__codegen.snap new file mode 100644 index 000000000..b36d30ea3 --- /dev/null +++ b/packages/cw-schema-codegen/tests/snapshots/typescript__codegen.snap @@ -0,0 +1,31 @@ +--- +source: packages/cw-schema-codegen/tests/typescript.rs +expression: output +--- +// This code is @generated by cw-schema-codegen. Do not modify this manually. + +/** + + */ + +type Owo = + + { + + /** + + */ + + field_1: string; + + /** + + */ + + field_2: string; + + } + +; + +export { Owo }; diff --git a/packages/cw-schema-codegen/tests/typescript.rs b/packages/cw-schema-codegen/tests/typescript.rs new file mode 100644 index 000000000..82ac6133a --- /dev/null +++ b/packages/cw-schema-codegen/tests/typescript.rs @@ -0,0 +1,54 @@ +use cw_schema::Schemaifier; + +#[derive(Schemaifier)] +struct Owo { + field_1: u32, + field_2: String, +} + +#[derive(Schemaifier)] +struct Uwu(String, u32); + +#[derive(Schemaifier)] +struct Òwó; + +#[derive(Schemaifier)] +enum Empty {} + +#[derive(Schemaifier)] +enum Hehehe { + A, + B(u32), + C { field: String }, +} + +#[test] +fn codegen() { + // generate the schemas for each of the above types + let schemas = [ + cw_schema::schema_of::(), + cw_schema::schema_of::(), + cw_schema::schema_of::<Òwó>(), + cw_schema::schema_of::(), + cw_schema::schema_of::(), + ]; + + // run the codegen to typescript + for schema in schemas { + let cw_schema::Schema::V1(schema) = schema else { + panic!(); + }; + + let output = schema + .definitions + .iter() + .map(|node| { + let mut buf = Vec::new(); + cw_schema_codegen::typescript::process_node(&mut buf, &schema, node).unwrap(); + String::from_utf8(buf).unwrap() + }) + .collect::(); + + insta::assert_snapshot!(output); + } +}