Skip to content

Commit

Permalink
reuse descriptor methods for all listings
Browse files Browse the repository at this point in the history
  • Loading branch information
teh-cmc committed Jan 8, 2025
1 parent 8c335be commit 67852da
Show file tree
Hide file tree
Showing 63 changed files with 1,682 additions and 5,423 deletions.
55 changes: 28 additions & 27 deletions crates/build/re_types_builder/src/codegen/rust/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1042,7 +1042,6 @@ fn quote_trait_impls_for_archetype(obj: &Object) -> TokenStream {
assert_eq!(kind, &ObjectKind::Archetype);

let display_name = re_case::to_human_case(name);
let archetype_name = &obj.fqname;
let name = format_ident!("{name}");

fn compute_component_descriptors(
Expand All @@ -1056,18 +1055,11 @@ fn quote_trait_impls_for_archetype(obj: &Object) -> TokenStream {
field
.try_get_attr::<String>(requirement_attr_value)
.map(|_| {
let Some(component_name) = field.typ.fqname() else {
panic!("Archetype field must be an object/union or an array/vector of such")
};

let archetype_name = &obj.fqname;
let archetype_name = format_ident!("{}", obj.name);
let archetype_field_name = field.snake_case_name();
let fn_name = format_ident!("descriptor_{archetype_field_name}");

quote!(ComponentDescriptor {
archetype_name: Some(#archetype_name.into()),
component_name: #component_name.into(),
archetype_field_name: Some(#archetype_field_name.into()),
})
quote!(#archetype_name::#fn_name())
})
})
.collect_vec();
Expand Down Expand Up @@ -1106,15 +1098,35 @@ fn quote_trait_impls_for_archetype(obj: &Object) -> TokenStream {
}
}
})
.chain(std::iter::once({
let archetype_name = &obj.fqname;
let indicator_component_name = format!(
"{}Indicator",
obj.fqname.replace("archetypes", "components")
);

let doc = "Returns the [`ComponentDescriptor`] for the associated indicator component.";

quote! {
#[doc = #doc]
#[inline]
pub fn descriptor_indicator() -> ComponentDescriptor {
ComponentDescriptor {
archetype_name: Some(#archetype_name.into()),
component_name: #indicator_component_name.into(),
archetype_field_name: None,
}
}
}
}))
.collect_vec();

let archetype_name = format_ident!("{}", obj.name);
let indicator_name = format!("{}Indicator", obj.name);

let quoted_indicator_name = format_ident!("{indicator_name}");
let quoted_indicator_doc =
format!("Indicator component for the [`{name}`] [`::re_types_core::Archetype`]");
let indicator_component_name =
format!("{}Indicator", fqname.replace("archetypes", "components"));

let (num_required_descriptors, required_descriptors) =
compute_component_descriptors(obj, ATTR_RERUN_COMPONENT_REQUIRED);
Expand All @@ -1126,11 +1138,7 @@ fn quote_trait_impls_for_archetype(obj: &Object) -> TokenStream {
num_recommended_descriptors += 1;
recommended_descriptors = quote! {
#recommended_descriptors
ComponentDescriptor {
archetype_name: Some(#archetype_name.into()),
component_name: #indicator_component_name.into(),
archetype_field_name: None,
},
#archetype_name::descriptor_indicator(),
};

let num_components_docstring = quote_doc_line(&format!(
Expand Down Expand Up @@ -1190,21 +1198,14 @@ fn quote_trait_impls_for_archetype(obj: &Object) -> TokenStream {
quote!{ Some(&self.#field_name as &dyn ComponentBatch) }
};

let Some(component_name) = obj_field.typ.fqname() else {
panic!("Archetype field must be an object/union or an array/vector of such")
};
let archetype_name = &obj.fqname;
let archetype_field_name = obj_field.snake_case_name();
let descr_fn_name = format_ident!("descriptor_{archetype_field_name}");

quote! {
(#batch).map(|batch| {
::re_types_core::ComponentBatchCowWithDescriptor {
batch: batch.into(),
descriptor_override: Some(ComponentDescriptor {
archetype_name: Some(#archetype_name.into()),
archetype_field_name: Some((#archetype_field_name).into()),
component_name: (#component_name).into(),
}),
descriptor_override: Some(Self::#descr_fn_name()),
}
})

Expand Down
44 changes: 15 additions & 29 deletions crates/store/re_types/src/archetypes/annotation_context.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 67852da

Please sign in to comment.