quick_macros/lib.rs
1//! # Quick Macros
2//!
3//! This crate provides simple macros:
4//! - **Derive**
5//! - FieldNames - macro used for generating functions returning a name of a field
6//!
7
8#[cfg(feature = "derive")]
9use proc_macro::TokenStream;
10
11#[cfg(feature = "derive")]
12use syn::{parse_macro_input, DeriveInput};
13
14#[cfg(feature = "derive")]
15mod field_names;
16
17/// Generates methods for retrieving field names of a struct as string literals.
18///
19/// This function is used as part of a procedural macro. It takes a struct definition,
20/// extracts its named fields, and generates a method for each field. Each generated method
21/// follows the naming pattern `nameof_<field_name>()`, which returns the field name as a static string.
22///
23/// # Example
24/// ```
25/// use quick_macros::FieldNames;
26///
27/// #[derive(FieldNames)]
28/// struct Person {
29/// name: String,
30/// age: u32,
31/// }
32///
33/// assert_eq!(Person::nameof_name(), "name");
34/// assert_eq!(Person::nameof_age(), "age");
35/// ```
36///
37/// # Panics
38/// - If the macro is applied to a non-struct type (e.g., an enum or union), it will panic.
39#[cfg(feature = "derive")]
40#[proc_macro_derive(FieldNames)]
41pub fn field_names(input: TokenStream) -> TokenStream {
42 let input = parse_macro_input!(input as DeriveInput);
43 field_names::field_names(input).into()
44}