Skip to content

Commit

Permalink
[shape.bzl] generate thrift impl as long as __thrift is set at all
Browse files Browse the repository at this point in the history
Summary:
It can be useful to make empty structs as sentinel types which generate unit
structs in Rust.
Update the Rust code generator to generate thrift impls as long as `__thrift`
is non-`null`

Test Plan:
On D68505354
```
arc rust-check fbcode//metalos/bzl/service:service.shape-rust
```

Reviewed By: sergeyfd

Differential Revision: D68506178

fbshipit-source-id: 855e700ea36f4a1a2b63bd90d3b619e36c1db28d
  • Loading branch information
vmagro authored and facebook-github-bot committed Jan 22, 2025
1 parent 370cf44 commit 05176e0
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 2 deletions.
2 changes: 2 additions & 0 deletions antlir/bzl/shape2/ir2code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ where
handlebars.register_helper("json", Box::new(json_helper));
handlebars.register_helper("upper", Box::new(upper_helper));
handlebars.register_helper("ident", Box::new(ident_helper));
handlebars.register_helper("is-null", Box::new(is_null_helper));

let handlebars = <Module as RegisterTemplates<T>>::register_templates(handlebars, templates)
.context("When setting up handlebars for Module")?;
Expand All @@ -180,6 +181,7 @@ where
handlebars_helper!(upper_helper: |x: String| x.to_uppercase());
handlebars_helper!(ident_helper: |x: String| x.replace('-', "_"));
handlebars_helper!(can_default: |x: BTreeMap<FieldName, Arc<Field>>| x.values().all(|v| !v.required || v.default_value.is_some()));
handlebars_helper!(is_null_helper: |x: Value| x.is_null());

trait ModuleExt<T>
where
Expand Down
4 changes: 2 additions & 2 deletions antlir/bzl/shape2/templates/struct.rs.handlebars
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ impl {{name}} {
{{/each}}
}

{{#if thrift_fields}}
{{#unless (is-null thrift_fields)}}
impl ::fbthrift::ttype::GetTType for {{name}} {
const TTYPE: ::fbthrift::ttype::TType = ::fbthrift::ttype::TType::Struct;
}
Expand Down Expand Up @@ -115,7 +115,7 @@ where
})
}
}
{{/if}}
{{/unless}}

{{#if (can-default fields)}}
impl Default for {{name}} {
Expand Down

0 comments on commit 05176e0

Please sign in to comment.