ui_macros.rs

 1mod derive_component;
 2mod derive_path_str;
 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/// Derives the `Component` trait for a struct.
64///
65/// This macro generates implementations for the `Component` trait and associated
66/// registration functions for the component system.
67///
68/// # Attributes
69///
70/// - `#[component(scope = "...")]`: Required. Specifies the scope of the component.
71/// - `#[component(description = "...")]`: Optional. Provides a description for the component.
72///
73/// # Example
74///
75/// ```
76/// use ui_macros::Component;
77///
78/// #[derive(Component)]
79/// #[component(scope = "toggle", description = "A element that can be toggled on and off")]
80/// struct Checkbox;
81/// ```
82#[proc_macro_derive(IntoComponent, attributes(component))]
83pub fn derive_component(input: TokenStream) -> TokenStream {
84    derive_component::derive_into_component(input)
85}