1mod derive_path_str;
2
3use proc_macro::TokenStream;
4
5/// Derives the `path` method for an enum.
6///
7/// This macro generates a `path` method for each variant of the enum, which returns a string
8/// representation of the enum variant's path. The path is constructed using a prefix and
9/// optionally a suffix, which are specified using attributes.
10///
11/// # Attributes
12///
13/// - `#[path_str(prefix = "...")]`: Required. Specifies the prefix for all paths.
14/// - `#[path_str(suffix = "...")]`: Optional. Specifies a suffix for all paths.
15/// - `#[strum(serialize_all = "...")]`: Optional. Specifies the case conversion for variant names.
16///
17/// # Example
18///
19/// ```
20/// use strum::EnumString;
21/// use ui_macros::{path_str, DerivePathStr};
22///
23/// #[derive(EnumString, DerivePathStr)]
24/// #[path_str(prefix = "my_prefix", suffix = ".txt")]
25/// #[strum(serialize_all = "snake_case")]
26/// enum MyEnum {
27/// VariantOne,
28/// VariantTwo,
29/// }
30///
31/// // These assertions would work if we could instantiate the enum
32/// // assert_eq!(MyEnum::VariantOne.path(), "my_prefix/variant_one.txt");
33/// // assert_eq!(MyEnum::VariantTwo.path(), "my_prefix/variant_two.txt");
34/// ```
35///
36/// # Panics
37///
38/// This macro will panic if used on anything other than an enum.
39#[proc_macro_derive(DerivePathStr, attributes(path_str))]
40pub fn derive_path_str(input: TokenStream) -> TokenStream {
41 derive_path_str::derive_path_str(input)
42}
43
44/// A marker attribute for use with `DerivePathStr`.
45///
46/// This attribute is used to specify the prefix and suffix for the `path` method
47/// generated by `DerivePathStr`. It doesn't modify the input and is only used as a
48/// marker for the derive macro.
49#[proc_macro_attribute]
50pub fn path_str(_args: TokenStream, input: TokenStream) -> TokenStream {
51 // This attribute doesn't modify the input, it's just a marker
52 input
53}