Skip to content

Commit 460850a

Browse files
committed
Add some TypeScript snapshots
1 parent 08d06c4 commit 460850a

18 files changed

+278
-10
lines changed

packages/cw-schema-codegen/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ authors = ["Aumetra Weisman <[email protected]>"]
44
edition = "2021"
55
version.workspace = true
66
publish = false
7+
build = "build.rs"
78

89
[dependencies]
910
anyhow = "1.0.89"

packages/cw-schema-codegen/build.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
fn main() {
2+
println!("cargo:rerun-if-changed=templates");
3+
}
Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,41 @@
11
use askama::Template;
2+
use std::borrow::Cow;
3+
4+
#[derive(Clone)]
5+
pub struct EnumVariantTemplate<'a> {
6+
pub name: Cow<'a, str>,
7+
pub docs: Cow<'a, [Cow<'a, str>]>,
8+
pub ty: TypeTemplate<'a>,
9+
}
210

311
#[derive(Template)]
412
#[template(escape = "none", path = "go/enum.tpl.go")]
5-
pub struct EnumTemplate {}
13+
pub struct EnumTemplate<'a> {
14+
pub name: Cow<'a, str>,
15+
pub docs: Cow<'a, [Cow<'a, str>]>,
16+
pub variants: Cow<'a, [EnumVariantTemplate<'a>]>,
17+
}
18+
19+
#[derive(Clone)]
20+
pub struct FieldTemplate<'a> {
21+
pub name: Cow<'a, str>,
22+
pub docs: Cow<'a, [Cow<'a, str>]>,
23+
pub ty: Cow<'a, str>,
24+
}
25+
26+
#[derive(Clone)]
27+
pub enum TypeTemplate<'a> {
28+
Unit,
29+
Tuple(Cow<'a, [Cow<'a, str>]>),
30+
Named {
31+
fields: Cow<'a, [FieldTemplate<'a>]>,
32+
},
33+
}
634

735
#[derive(Template)]
836
#[template(escape = "none", path = "go/struct.tpl.go")]
9-
pub struct StructTemplate {}
37+
pub struct StructTemplate<'a> {
38+
pub name: Cow<'a, str>,
39+
pub docs: Cow<'a, [Cow<'a, str>]>,
40+
pub ty: TypeTemplate<'a>,
41+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// This code is @generated by cw-schema-codegen. Do not modify this manually.
2+
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// This code is @generated by cw-schema-codegen. Do not modify this manually.
2+
3+
package cwcodegen
4+
5+
{% for doc in docs %}
6+
// {{ doc }}
7+
{% endfor %}
8+
type {{ name }} struct {
9+
{% match ty %}
10+
{% when TypeTemplate::Unit %}
11+
{% when TypeTemplate::Tuple with (types) %}
12+
{{ todo!() }}
13+
{% when TypeTemplate::Named with { fields } %}
14+
{% for field in fields %}
15+
{% for doc in docs %}
16+
// {{ doc }}
17+
{% endfor %}
18+
{{ field.name|capitalize }} {{ field.ty }} `json:"{{ field.name }}"`
19+
{% endfor %}
20+
{% endmatch %}
21+
}

packages/cw-schema-codegen/templates/typescript/enum.tpl.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ type {{ name }} =
3535
} }
3636
{% endmatch %}
3737
{% endfor %}
38+
39+
{% if variants.len() == 0 %}
40+
never;
41+
{% endif %}
3842
;
3943

4044
export { {{ name }} };

packages/cw-schema-codegen/tests/snapshots/rust_tpl__complex_enum.snap

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
source: packages/cw-schema-codegen/tests/rust_tpl.rs
33
expression: rendered
44
---
5+
// This code is @generated by cw-schema-codegen. Do not modify this manually.
6+
7+
58
#[doc = "Complex enum"]
69

710

@@ -18,9 +21,7 @@ pub enum Complex {
1821
One
1922

2023
(
21-
22-
u64,
23-
24+
u64
2425
)
2526

2627
,

packages/cw-schema-codegen/tests/snapshots/rust_tpl__empty_enum.snap

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
source: packages/cw-schema-codegen/tests/rust_tpl.rs
33
expression: rendered
44
---
5+
// This code is @generated by cw-schema-codegen. Do not modify this manually.
6+
7+
58
#[doc = "Empty enum"]
69

710

packages/cw-schema-codegen/tests/snapshots/rust_tpl__empty_struct.snap

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
source: packages/cw-schema-codegen/tests/rust_tpl.rs
33
expression: rendered
44
---
5+
// This code is @generated by cw-schema-codegen. Do not modify this manually.
6+
7+
58
#[doc = "Empty struct"]
69

710

packages/cw-schema-codegen/tests/snapshots/rust_tpl__named_struct.snap

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
source: packages/cw-schema-codegen/tests/rust_tpl.rs
33
expression: rendered
44
---
5+
// This code is @generated by cw-schema-codegen. Do not modify this manually.
6+
7+
58
#[doc = "Named struct"]
69

710

packages/cw-schema-codegen/tests/snapshots/rust_tpl__simple_enum.snap

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
source: packages/cw-schema-codegen/tests/rust_tpl.rs
33
expression: rendered
44
---
5+
// This code is @generated by cw-schema-codegen. Do not modify this manually.
6+
7+
58
#[doc = "Simple enum"]
69

710

packages/cw-schema-codegen/tests/snapshots/rust_tpl__tuple_struct.snap

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
source: packages/cw-schema-codegen/tests/rust_tpl.rs
33
expression: rendered
44
---
5+
// This code is @generated by cw-schema-codegen. Do not modify this manually.
6+
7+
58
#[doc = "Tuple struct"]
69

710

@@ -10,9 +13,5 @@ pub struct Tuple
1013

1114

1215
(
13-
14-
u64,
15-
16-
String,
17-
16+
u64, String
1817
);
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
---
2+
source: packages/cw-schema-codegen/tests/typescript.rs
3+
expression: output
4+
---
5+
// This code is @generated by cw-schema-codegen. Do not modify this manually.
6+
7+
/**
8+
9+
*/
10+
11+
type Uwu =
12+
13+
[string, string]
14+
15+
;
16+
17+
export { Uwu };
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
---
2+
source: packages/cw-schema-codegen/tests/typescript.rs
3+
expression: output
4+
---
5+
// This code is @generated by cw-schema-codegen. Do not modify this manually.
6+
7+
/**
8+
9+
*/
10+
11+
type Òwó =
12+
13+
void
14+
15+
;
16+
17+
export { Òwó };
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
---
2+
source: packages/cw-schema-codegen/tests/typescript.rs
3+
expression: output
4+
---
5+
// This code is @generated by cw-schema-codegen. Do not modify this manually.
6+
7+
/**
8+
9+
*/
10+
11+
type Empty =
12+
13+
14+
15+
never;
16+
17+
;
18+
19+
export { Empty };
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
---
2+
source: packages/cw-schema-codegen/tests/typescript.rs
3+
expression: output
4+
---
5+
// This code is @generated by cw-schema-codegen. Do not modify this manually.
6+
7+
/**
8+
9+
*/
10+
11+
type Hehehe =
12+
13+
|
14+
15+
/**
16+
17+
*/
18+
19+
20+
{ "A": {} }
21+
22+
23+
|
24+
25+
/**
26+
27+
*/
28+
29+
30+
{ "B": [string] }
31+
32+
33+
|
34+
35+
/**
36+
37+
*/
38+
39+
40+
{ "C": {
41+
42+
/**
43+
44+
*/
45+
46+
field: string;
47+
48+
} }
49+
50+
51+
52+
53+
;
54+
55+
export { Hehehe };
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
---
2+
source: packages/cw-schema-codegen/tests/typescript.rs
3+
expression: output
4+
---
5+
// This code is @generated by cw-schema-codegen. Do not modify this manually.
6+
7+
/**
8+
9+
*/
10+
11+
type Owo =
12+
13+
{
14+
15+
/**
16+
17+
*/
18+
19+
field_1: string;
20+
21+
/**
22+
23+
*/
24+
25+
field_2: string;
26+
27+
}
28+
29+
;
30+
31+
export { Owo };
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
use cw_schema::Schemaifier;
2+
3+
#[derive(Schemaifier)]
4+
struct Owo {
5+
field_1: u32,
6+
field_2: String,
7+
}
8+
9+
#[derive(Schemaifier)]
10+
struct Uwu(String, u32);
11+
12+
#[derive(Schemaifier)]
13+
struct Òwó;
14+
15+
#[derive(Schemaifier)]
16+
enum Empty {}
17+
18+
#[derive(Schemaifier)]
19+
enum Hehehe {
20+
A,
21+
B(u32),
22+
C { field: String },
23+
}
24+
25+
#[test]
26+
fn codegen() {
27+
// generate the schemas for each of the above types
28+
let schemas = [
29+
cw_schema::schema_of::<Owo>(),
30+
cw_schema::schema_of::<Uwu>(),
31+
cw_schema::schema_of::<Òwó>(),
32+
cw_schema::schema_of::<Empty>(),
33+
cw_schema::schema_of::<Hehehe>(),
34+
];
35+
36+
// run the codegen to typescript
37+
for schema in schemas {
38+
let cw_schema::Schema::V1(schema) = schema else {
39+
panic!();
40+
};
41+
42+
let output = schema
43+
.definitions
44+
.iter()
45+
.map(|node| {
46+
let mut buf = Vec::new();
47+
cw_schema_codegen::typescript::process_node(&mut buf, &schema, node).unwrap();
48+
String::from_utf8(buf).unwrap()
49+
})
50+
.collect::<String>();
51+
52+
insta::assert_snapshot!(output);
53+
}
54+
}

0 commit comments

Comments
 (0)