Skip to content

Commit

Permalink
Fix enum templates
Browse files Browse the repository at this point in the history
  • Loading branch information
aumetra committed Oct 24, 2024
1 parent 63d647d commit 2780474
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 12 deletions.
4 changes: 2 additions & 2 deletions packages/cw-schema-codegen/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use std::{
path::PathBuf,
};

#[derive(Clone, Copy, Default, ValueEnum)]
#[derive(Clone, Copy, Debug, Default, ValueEnum)]
pub enum Language {
#[default]
Rust,
Expand Down Expand Up @@ -54,7 +54,7 @@ fn main() -> anyhow::Result<()> {
.init()?;

let opts: Opts = Opts::parse();
info!("Generating code for {} from {:?}", opts.language, opts.file);
info!("Generating code for {:?} from {:?}", opts.language, opts.file);

let schema = std::fs::read_to_string(&opts.file)?;
let schema: cw_schema::Schema = serde_json::from_str(&schema)?;
Expand Down
18 changes: 17 additions & 1 deletion packages/cw-schema-codegen/templates/rust/enum.tpl.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
pub enum {{ name }} {
{% for variant in variants %}
{{ variant.name }} {% if let Some(types) = variant.types %}({% for ty in types %} {{ ty }}, {% endfor %}) {% endif %},
{{ variant.name }}
{% match variant.ty %}
{% when TypeTemplate::Unit %}
{% when TypeTemplate::Tuple with (types) %}
(
{% for ty in types %}
{{ ty }},
{% endfor %}
)
{% when TypeTemplate::Named with { fields } %}
{
{% for field in fields %}
{{ field.name }}: {{ field.ty }},
{% endfor %}
}
{% endmatch %}
,
{% endfor %}
}
21 changes: 16 additions & 5 deletions packages/cw-schema-codegen/tests/rust_tpl.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use askama::Template;
use cw_schema_codegen::rust::{EnumTemplate, EnumVariantTemplate};
use cw_schema_codegen::rust::{EnumTemplate, EnumVariantTemplate, FieldTemplate, TypeTemplate};

#[test]
fn simple_enum() {
Expand All @@ -8,11 +8,11 @@ fn simple_enum() {
variants: &[
EnumVariantTemplate {
name: "One",
types: None,
ty: TypeTemplate::Unit,
},
EnumVariantTemplate {
name: "Two",
types: None,
ty: TypeTemplate::Unit,
},
],
};
Expand All @@ -28,11 +28,22 @@ fn complex_enum() {
variants: &[
EnumVariantTemplate {
name: "One",
types: Some(&["u64"]),
ty: TypeTemplate::Tuple(&["u64"]),
},
EnumVariantTemplate {
name: "Two",
types: Some(&["String", "u64"]),
ty: TypeTemplate::Named {
fields: &[
FieldTemplate {
name: "a",
ty: "u64",
},
FieldTemplate {
name: "b",
ty: "String",
},
],
},
},
],
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,26 @@ expression: rendered
---
pub enum Complex {

One ( u64, ) ,
One

(

u64,

)

,

Two ( String, u64, ) ,
Two

{

a: u64,

b: String,

}

,

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,14 @@ expression: rendered
---
pub enum Simple {

One ,
One


,

Two ,
Two


,

}

0 comments on commit 2780474

Please sign in to comment.