Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rust SDK: no more reducer args structs #2036

Merged
merged 3 commits into from
Dec 4, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions crates/cli/src/subcommands/generate/rust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,9 @@ Requested namespace: {namespace}",
|out| {
write!(out, "Self::{enum_variant_name}");
if !reducer.params_for_generate.elements.is_empty() {
// We generate "struct variants" for reducers with arguments,
// but "unit variants" for reducers of no arguments.
// These use different constructor syntax.
out.delimited_block(
" {",
|out| {
Expand Down Expand Up @@ -905,9 +908,10 @@ fn print_reducer_enum_defn(module: &ModuleDef, out: &mut Indenter) {
for reducer in iter_reducers(module) {
write!(out, "{} ", reducer_variant_name(&reducer.name));
if !reducer.params_for_generate.elements.is_empty() {
// If the reducer has any arguments, generate a "struct variant."
// If it doesn't, generate a "unit variant" instead.
// For some reason, Rust prohibits empty struct variants.
// If the reducer has any arguments, generate a "struct variant,"
// like `Foo { bar: Baz, }`.
// If it doesn't, generate a "unit variant" instead,
// like `Foo,`.
write_struct_type_fields_in_braces(module, out, &reducer.params_for_generate.elements, false);
}
writeln!(out, ",");
Expand Down Expand Up @@ -937,7 +941,7 @@ impl __sdk::InModule for Reducer {{
for reducer in iter_reducers(module) {
write!(out, "Reducer::{}", reducer_variant_name(&reducer.name));
if !reducer.params_for_generate.elements.is_empty() {
// Because Rust prohibits empty struct variants,
// Because we're emitting unit variants when the payload is empty,
// we have to emit different patterns for empty vs non-empty variants.
write!(out, " {{ .. }}");
}
Expand Down
Loading