-
Notifications
You must be signed in to change notification settings - Fork 344
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
278 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
fn main() { | ||
println!("cargo:rerun-if-changed=templates"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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>, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
// This code is @generated by cw-schema-codegen. Do not modify this manually. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 %} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
packages/cw-schema-codegen/tests/snapshots/typescript__codegen-2.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 }; |
17 changes: 17 additions & 0 deletions
17
packages/cw-schema-codegen/tests/snapshots/typescript__codegen-3.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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ó }; |
19 changes: 19 additions & 0 deletions
19
packages/cw-schema-codegen/tests/snapshots/typescript__codegen-4.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 }; |
55 changes: 55 additions & 0 deletions
55
packages/cw-schema-codegen/tests/snapshots/typescript__codegen-5.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 }; |
31 changes: 31 additions & 0 deletions
31
packages/cw-schema-codegen/tests/snapshots/typescript__codegen.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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::<Owo>(), | ||
cw_schema::schema_of::<Uwu>(), | ||
cw_schema::schema_of::<Òwó>(), | ||
cw_schema::schema_of::<Empty>(), | ||
cw_schema::schema_of::<Hehehe>(), | ||
]; | ||
|
||
// 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::<String>(); | ||
|
||
insta::assert_snapshot!(output); | ||
} | ||
} |