1mod derive_path_str;
2mod derive_register_component;
3mod dynamic_spacing;
4
5use proc_macro::TokenStream;
6
7/// Derives the `path` method for an enum.
8///
9/// This macro generates a `path` method for each variant of the enum, which returns a string
10/// representation of the enum variant's path. The path is constructed using a prefix and
11/// optionally a suffix, which are specified using attributes.
12///
13/// # Attributes
14///
15/// - `#[path_str(prefix = "...")]`: Required. Specifies the prefix for all paths.
16/// - `#[path_str(suffix = "...")]`: Optional. Specifies a suffix for all paths.
17/// - `#[strum(serialize_all = "...")]`: Optional. Specifies the case conversion for variant names.
18///
19/// # Example
20///
21/// ```
22/// use strum::EnumString;
23/// use ui_macros::{path_str, DerivePathStr};
24///
25/// #[derive(EnumString, DerivePathStr)]
26/// #[path_str(prefix = "my_prefix", suffix = ".txt")]
27/// #[strum(serialize_all = "snake_case")]
28/// enum MyEnum {
29/// VariantOne,
30/// VariantTwo,
31/// }
32///
33/// // These assertions would work if we could instantiate the enum
34/// // assert_eq!(MyEnum::VariantOne.path(), "my_prefix/variant_one.txt");
35/// // assert_eq!(MyEnum::VariantTwo.path(), "my_prefix/variant_two.txt");
36/// ```
37///
38/// # Panics
39///
40/// This macro will panic if used on anything other than an enum.
41#[proc_macro_derive(DerivePathStr, attributes(path_str))]
42pub fn derive_path_str(input: TokenStream) -> TokenStream {
43 derive_path_str::derive_path_str(input)
44}
45
46/// A marker attribute for use with `DerivePathStr`.
47///
48/// This attribute is used to specify the prefix and suffix for the `path` method
49/// generated by `DerivePathStr`. It doesn't modify the input and is only used as a
50/// marker for the derive macro.
51#[proc_macro_attribute]
52pub fn path_str(_args: TokenStream, input: TokenStream) -> TokenStream {
53 // This attribute doesn't modify the input, it's just a marker
54 input
55}
56
57/// Generates the DynamicSpacing enum used for density-aware spacing in the UI.
58#[proc_macro]
59pub fn derive_dynamic_spacing(input: TokenStream) -> TokenStream {
60 dynamic_spacing::derive_spacing(input)
61}
62
63/// Registers components that implement the `Component` trait.
64///
65/// This proc macro is used to automatically register structs that implement
66/// the `Component` trait with the [`component::ComponentRegistry`].
67///
68/// If the component trait is not implemented, it will generate a compile-time error.
69///
70/// # Example
71///
72/// ```
73/// use ui_macros::RegisterComponent;
74///
75/// #[derive(RegisterComponent)]
76/// struct MyComponent;
77///
78/// impl Component for MyComponent {
79/// // Component implementation
80/// }
81/// ```
82///
83/// This example will add MyComponent to the ComponentRegistry.
84#[proc_macro_derive(RegisterComponent)]
85pub fn derive_register_component(input: TokenStream) -> TokenStream {
86 derive_register_component::derive_register_component(input)
87}