dear_imgui_reflect_derive/lib.rs
1use proc_macro::TokenStream;
2use syn::{Data, DeriveInput, parse_macro_input};
3
4mod attrs;
5mod diagnostics;
6mod enum_codegen;
7mod field_codegen;
8mod internal;
9mod settings_codegen;
10mod struct_codegen;
11#[cfg(test)]
12mod tests;
13
14/// Derive macro for [`dear_imgui_reflect::ImGuiReflect`].
15///
16/// Currently supports:
17///
18/// - Structs with named fields, tuple fields, and unit structs. Each field must implement
19/// [`dear_imgui_reflect::ImGuiValue`], either directly or via a blanket
20/// implementation (for example, another type that implements
21/// [`dear_imgui_reflect::ImGuiReflect`]).
22/// - Enums with unit, tuple, or named payload variants. Variants are edited via a combo box
23/// (default) or radio buttons via `#[imgui(enum_style = "radio")]`.
24/// Switching to a payload variant constructs its payload using `Default`, so payload field types
25/// must implement `Default` to allow variant switching.
26///
27/// Supported field attributes:
28///
29/// - `#[imgui(skip)]` — do not generate any UI for this field.
30/// - `#[imgui(name = "Custom Label")]` — override the label used for this field.
31/// - `#[imgui(slider, min = ..., max = ..., format = "...")]` — use a slider
32/// with the given range/format for numeric fields.
33/// - `#[imgui(multiline, hint = "...", read_only)]` — use multiline text
34/// widgets for String/ImString fields.
35#[proc_macro_derive(ImGuiReflect, attributes(imgui))]
36pub fn derive_imgui_reflect(input: TokenStream) -> TokenStream {
37 let input = parse_macro_input!(input as DeriveInput);
38 let ident = input.ident;
39 let generics = input.generics;
40 let attrs = input.attrs;
41
42 match input.data {
43 Data::Struct(data) => struct_codegen::derive_for_struct(ident, generics, data),
44 Data::Enum(data) => enum_codegen::derive_for_enum(ident, generics, attrs, data),
45 Data::Union(data) => diagnostics::union_not_supported(data),
46 }
47}