Skip to content

Commit

Permalink
add custom clause support to Rust codegen
Browse files Browse the repository at this point in the history
  • Loading branch information
teh-cmc committed Jul 25, 2023
1 parent c185424 commit a09785e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
22 changes: 16 additions & 6 deletions crates/re_types_builder/src/codegen/rust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ use crate::{
codegen::{StringExt as _, AUTOGEN_WARNING},
ArrowRegistry, CodeGenerator, Docs, ElementType, Object, ObjectField, ObjectKind, Objects,
Type, ATTR_RERUN_COMPONENT_OPTIONAL, ATTR_RERUN_COMPONENT_RECOMMENDED,
ATTR_RERUN_COMPONENT_REQUIRED, ATTR_RERUN_LEGACY_FQNAME, ATTR_RUST_DERIVE, ATTR_RUST_REPR,
ATTR_RUST_TUPLE_STRUCT,
ATTR_RERUN_COMPONENT_REQUIRED, ATTR_RERUN_LEGACY_FQNAME, ATTR_RUST_CUSTOM_CLAUSE,
ATTR_RUST_DERIVE, ATTR_RUST_REPR, ATTR_RUST_TUPLE_STRUCT,
};

// TODO(cmc): it'd be nice to be able to generate vanilla comments (as opposed to doc-comments)
Expand Down Expand Up @@ -344,6 +344,7 @@ impl QuotedObject {
let quoted_derive_clone_debug = quote_derive_clone_debug();
let quoted_derive_clause = quote_meta_clause_from_obj(obj, ATTR_RUST_DERIVE, "derive");
let quoted_repr_clause = quote_meta_clause_from_obj(obj, ATTR_RUST_REPR, "repr");
let quoted_custom_clause = quote_meta_clause_from_obj(obj, ATTR_RUST_CUSTOM_CLAUSE, "");

let quoted_fields = fields
.iter()
Expand All @@ -367,6 +368,7 @@ impl QuotedObject {
#quoted_derive_clone_debug
#quoted_derive_clause
#quoted_repr_clause
#quoted_custom_clause
#quoted_struct

#quoted_from_impl
Expand Down Expand Up @@ -411,6 +413,7 @@ impl QuotedObject {
let quoted_derive_clone_debug = quote_derive_clone_debug();
let quoted_derive_clause = quote_meta_clause_from_obj(obj, ATTR_RUST_DERIVE, "derive");
let quoted_repr_clause = quote_meta_clause_from_obj(obj, ATTR_RUST_REPR, "repr");
let quoted_custom_clause = quote_meta_clause_from_obj(obj, ATTR_RUST_CUSTOM_CLAUSE, "");

let quoted_fields = fields.iter().map(|obj_field| {
let ObjectField {
Expand Down Expand Up @@ -451,6 +454,7 @@ impl QuotedObject {
#quoted_derive_clone_debug
#quoted_derive_clause
#quoted_repr_clause
#quoted_custom_clause
pub enum #name {
#(#quoted_fields,)*
}
Expand Down Expand Up @@ -618,10 +622,16 @@ fn quote_derive_clone_debug() -> TokenStream {
fn quote_meta_clause_from_obj(obj: &Object, attr: &str, clause: &str) -> TokenStream {
let quoted = obj
.try_get_attr::<String>(attr)
.map(|what| {
syn::parse_str::<syn::MetaList>(&format!("{clause}({what})"))
.with_context(|| format!("illegal meta clause: {what:?}"))
.unwrap()
.map(|contents| {
if clause.is_empty() {
syn::parse_str::<syn::Meta>(contents.as_str())
.with_context(|| format!("illegal meta clause: {clause:?}"))
.unwrap()
} else {
syn::parse_str::<syn::Meta>(&format!("{clause}({contents})"))
.with_context(|| format!("illegal meta clause: {clause}({contents})"))
.unwrap()
}
})
.map(|clause| quote!(#[#clause]));
quote!(#quoted)
Expand Down
1 change: 1 addition & 0 deletions crates/re_types_builder/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ pub const ATTR_RERUN_LEGACY_FQNAME: &str = "attr.rerun.legacy_fqname";
pub const ATTR_PYTHON_ALIASES: &str = "attr.python.aliases";
pub const ATTR_PYTHON_ARRAY_ALIASES: &str = "attr.python.array_aliases";

pub const ATTR_RUST_CUSTOM_CLAUSE: &str = "attr.rust.custom_clause";
pub const ATTR_RUST_DERIVE: &str = "attr.rust.derive";
pub const ATTR_RUST_REPR: &str = "attr.rust.repr";
pub const ATTR_RUST_TUPLE_STRUCT: &str = "attr.rust.tuple_struct";
Expand Down

0 comments on commit a09785e

Please sign in to comment.