Skip to content

Commit

Permalink
Add serde renames for prettier enums
Browse files Browse the repository at this point in the history
  • Loading branch information
aumetra committed Oct 28, 2024
1 parent 2b560a2 commit db942c0
Show file tree
Hide file tree
Showing 12 changed files with 74 additions and 37 deletions.
3 changes: 2 additions & 1 deletion packages/cw-schema-codegen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ clap = { version = "4.5.18", features = ["derive"] }
cw-schema = { version = "=2.2.0-rc.1", path = "../cw-schema" }
either = "1.13.0"
frunk = "0.4.3"
heck = "0.5.0"
log = "0.4.22"
serde_json = "1.0.128"
simple_logger = "5.0.0"
simple_logger = { version = "5.0.0", features = ["stderr"] }

[dev-dependencies]
insta = "1.40.0"
51 changes: 28 additions & 23 deletions packages/cw-schema-codegen/src/rust/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use self::template::{
EnumTemplate, EnumVariantTemplate, FieldTemplate, StructTemplate, TypeTemplate,
};
use heck::ToPascalCase;
use std::{borrow::Cow, io};

pub mod template;
Expand Down Expand Up @@ -38,7 +39,13 @@ fn expand_node_name<'a>(
format!("({})", items).into()
}
cw_schema::NodeType::Enum { .. } => node.name.as_ref().into(),
_ => todo!(),

cw_schema::NodeType::Decimal { precision, signed } => todo!(),
cw_schema::NodeType::Address => todo!(),
cw_schema::NodeType::Checksum => todo!(),
cw_schema::NodeType::HexBinary => todo!(),
cw_schema::NodeType::Timestamp => todo!(),
cw_schema::NodeType::Unit => Cow::Borrowed("()"),
}
}

Expand All @@ -62,46 +69,48 @@ where
match node.value {
cw_schema::NodeType::Struct(ref sty) => {
let structt = StructTemplate {
name: &node.name,
name: node.name.clone(),
docs: prepare_docs(node.description.as_deref()),
ty: match sty {
cw_schema::StructType::Unit => TypeTemplate::Unit,
cw_schema::StructType::Named { ref properties } => TypeTemplate::Named {
fields: properties
.iter()
.map(|(name, prop)| {
let ty = expand_node_name(schema, &schema.definitions[prop.value]);
FieldTemplate {
name: Cow::Borrowed(name),
docs: prepare_docs(prop.description.as_deref()),
ty,
}
.map(|(name, prop)| FieldTemplate {
name: Cow::Borrowed(name),
docs: prepare_docs(prop.description.as_deref()),
ty: expand_node_name(schema, &schema.definitions[prop.value]),
})
.collect(),
},
_ => unreachable!(),
cw_schema::StructType::Tuple { ref items } => TypeTemplate::Tuple(
items
.iter()
.map(|item| expand_node_name(schema, &schema.definitions[*item]))
.collect(),
),
},
};

writeln!(output, "{structt}")?;
}
cw_schema::NodeType::Enum { ref cases, .. } => {
let enumm = EnumTemplate {
name: &node.name,
name: node.name.clone(),
docs: prepare_docs(node.description.as_deref()),
variants: cases
.iter()
.map(|(name, case)| EnumVariantTemplate {
name,
name: name.to_pascal_case().into(),
docs: prepare_docs(case.description.as_deref()),
serde_rename: Some(name.clone()),
ty: match case.value {
cw_schema::EnumValue::Unit => TypeTemplate::Unit,
cw_schema::EnumValue::Tuple { ref items } => {
let items = items
.iter()
.map(|item| {
let node = &schema.definitions[*item];
expand_node_name(schema, node)
expand_node_name(schema, &schema.definitions[*item])
})
.collect();

Expand All @@ -111,21 +120,17 @@ where
TypeTemplate::Named {
fields: properties
.iter()
.map(|(name, prop)| {
let ty = expand_node_name(
.map(|(name, prop)| FieldTemplate {
name: Cow::Borrowed(name),
docs: prepare_docs(prop.description.as_deref()),
ty: expand_node_name(
schema,
&schema.definitions[prop.value],
);
FieldTemplate {
name: Cow::Borrowed(name),
docs: prepare_docs(prop.description.as_deref()),
ty,
}
),
})
.collect(),
}
}
_ => unreachable!(),
},
})
.collect(),
Expand Down
7 changes: 4 additions & 3 deletions packages/cw-schema-codegen/src/rust/template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@ use std::borrow::Cow;

#[derive(Clone)]
pub struct EnumVariantTemplate<'a> {
pub name: &'a str,
pub name: Cow<'a, str>,
pub docs: Cow<'a, [Cow<'a, str>]>,
pub serde_rename: Option<Cow<'a, str>>,
pub ty: TypeTemplate<'a>,
}

#[derive(Template)]
#[template(escape = "none", path = "rust/enum.tpl.rs")]
pub struct EnumTemplate<'a> {
pub name: &'a str,
pub name: Cow<'a, str>,
pub docs: Cow<'a, [Cow<'a, str>]>,
pub variants: Cow<'a, [EnumVariantTemplate<'a>]>,
}
Expand All @@ -35,7 +36,7 @@ pub enum TypeTemplate<'a> {
#[derive(Template)]
#[template(escape = "none", path = "rust/struct.tpl.rs")]
pub struct StructTemplate<'a> {
pub name: &'a str,
pub name: Cow<'a, str>,
pub docs: Cow<'a, [Cow<'a, str>]>,
pub ty: TypeTemplate<'a>,
}
7 changes: 7 additions & 0 deletions packages/cw-schema-codegen/templates/rust/enum.tpl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,19 @@
#[doc = "{{ doc }}"]
{% endfor %}

#[cosmwasm_schema::cw_serde]
pub enum {{ name }} {
{% for variant in variants %}
{% for doc in variant.docs %}
#[doc = "{{ doc }}"]
{% endfor %}

{% match variant.serde_rename %}
{% when Some with (rename) %}
#[serde(rename = "{{ rename }}")]
{% when None %}
{% endmatch %}

{{ variant.name }}
{% match variant.ty %}
{% when TypeTemplate::Unit %}
Expand Down
1 change: 1 addition & 0 deletions packages/cw-schema-codegen/templates/rust/struct.tpl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#[doc = "{{ doc }}"]
{% endfor %}

#[cosmwasm_schema::cw_serde]
pub struct {{ name }}

{% match ty %}
Expand Down
24 changes: 14 additions & 10 deletions packages/cw-schema-codegen/tests/rust_tpl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,19 @@ use cw_schema_codegen::rust::template::{
#[test]
fn simple_enum() {
let tpl = EnumTemplate {
name: "Simple",
name: Cow::Borrowed("Simple"),
docs: Cow::Borrowed(&[Cow::Borrowed("Simple enum")]),
variants: Cow::Borrowed(&[
EnumVariantTemplate {
name: "One",
name: Cow::Borrowed("One"),
docs: Cow::Borrowed(&[Cow::Borrowed("One variant")]),
serde_rename: None,
ty: TypeTemplate::Unit,
},
EnumVariantTemplate {
name: "Two",
name: Cow::Borrowed("Two"),
docs: Cow::Borrowed(&[Cow::Borrowed("Two variant")]),
serde_rename: None,
ty: TypeTemplate::Unit,
},
]),
Expand All @@ -31,17 +33,19 @@ fn simple_enum() {
#[test]
fn complex_enum() {
let tpl = EnumTemplate {
name: "Complex",
name: Cow::Borrowed("Complex"),
docs: Cow::Borrowed(&[Cow::Borrowed("Complex enum")]),
variants: Cow::Borrowed(&[
EnumVariantTemplate {
name: "One",
name: Cow::Borrowed("One"),
docs: Cow::Borrowed(&[Cow::Borrowed("One variant")]),
serde_rename: None,
ty: TypeTemplate::Tuple(Cow::Borrowed(&[Cow::Borrowed("u64")])),
},
EnumVariantTemplate {
name: "Two",
name: Cow::Borrowed("Two"),
docs: Cow::Borrowed(&[Cow::Borrowed("Two variant")]),
serde_rename: None,
ty: TypeTemplate::Named {
fields: Cow::Borrowed(&[
FieldTemplate {
Expand All @@ -67,7 +71,7 @@ fn complex_enum() {
#[test]
fn empty_enum() {
let tpl = EnumTemplate {
name: "Empty",
name: Cow::Borrowed("Empty"),
docs: Cow::Borrowed(&[Cow::Borrowed("Empty enum")]),
variants: Cow::Borrowed(&[]),
};
Expand All @@ -79,7 +83,7 @@ fn empty_enum() {
#[test]
fn empty_struct() {
let tpl = StructTemplate {
name: "Empty",
name: Cow::Borrowed("Empty"),
docs: Cow::Borrowed(&[Cow::Borrowed("Empty struct")]),
ty: TypeTemplate::Unit,
};
Expand All @@ -91,7 +95,7 @@ fn empty_struct() {
#[test]
fn tuple_struct() {
let tpl = StructTemplate {
name: "Tuple",
name: Cow::Borrowed("Tuple"),
docs: Cow::Borrowed(&[Cow::Borrowed("Tuple struct")]),
ty: TypeTemplate::Tuple(Cow::Borrowed(&[
Cow::Borrowed("u64"),
Expand All @@ -106,7 +110,7 @@ fn tuple_struct() {
#[test]
fn named_struct() {
let tpl = StructTemplate {
name: "Named",
name: Cow::Borrowed("Named"),
docs: Cow::Borrowed(&[Cow::Borrowed("Named struct")]),
ty: TypeTemplate::Named {
fields: Cow::Borrowed(&[
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,16 @@ expression: rendered
#[doc = "Complex enum"]


#[cosmwasm_schema::cw_serde]
pub enum Complex {


#[doc = "One variant"]





One

(
Expand All @@ -25,6 +29,9 @@ pub enum Complex {
#[doc = "Two variant"]





Two

{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ expression: rendered
#[doc = "Empty enum"]


#[cosmwasm_schema::cw_serde]
pub enum Empty {

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ expression: rendered
#[doc = "Empty struct"]


#[cosmwasm_schema::cw_serde]
pub struct Empty


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ expression: rendered
#[doc = "Named struct"]


#[cosmwasm_schema::cw_serde]
pub struct Named


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,16 @@ expression: rendered
#[doc = "Simple enum"]


#[cosmwasm_schema::cw_serde]
pub enum Simple {


#[doc = "One variant"]





One


Expand All @@ -20,6 +24,9 @@ pub enum Simple {
#[doc = "Two variant"]





Two


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ expression: rendered
#[doc = "Tuple struct"]


#[cosmwasm_schema::cw_serde]
pub struct Tuple


Expand Down

0 comments on commit db942c0

Please sign in to comment.