Skip to content

Commit 5b7caf0

Browse files
committed
Add some TypeScript snapshots
1 parent 08d06c4 commit 5b7caf0

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

0 commit comments

Comments
 (0)