Skip to main content

inquire_derive/
lib.rs

1mod codegen;
2mod utils;
3
4use proc_macro::TokenStream;
5use syn::{parse_macro_input, DeriveInput};
6
7/// Derive macro for making enums selectable in inquire prompts.
8///
9/// This macro generates `select()` and `multi_select()` methods for enums,
10/// allowing them to be used directly with inquire's Select and MultiSelect prompts.
11/// The methods return the prompt builders, allowing for further customization.
12///
13/// The enum must implement `Display`, `Debug`, `Copy`, `Clone`, and be `'static`.
14///
15/// # Example
16///
17/// ```ignore
18/// use inquire_derive::Selectable;
19/// use std::fmt::{Display, Formatter};
20///
21/// #[derive(Debug, Copy, Clone, Selectable)]
22/// enum Color {
23///     Red,
24///     Green,
25///     Blue,
26/// }
27///
28/// impl Display for Color {
29///     fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
30///         write!(f, "{:?}", self)
31///     }
32/// }
33///
34/// // Usage:
35/// // let color = Color::select("Choose a color:").prompt()?;
36/// //
37/// // With customization:
38/// // let color = Color::select("Choose a color:")
39/// //     .with_help_message("Use arrow keys to navigate")
40/// //     .prompt()?;
41/// //
42/// // Multi-select:
43/// // let colors = Color::multi_select("Choose colors:").prompt()?;
44/// ```
45#[proc_macro_derive(Selectable, attributes(desc))]
46pub fn derive_selectable(input: TokenStream) -> TokenStream {
47    let input = parse_macro_input!(input as DeriveInput);
48    codegen::generate_selectable_impl(input)
49}
50
51#[cfg(test)]
52mod tests {
53    use super::*;
54
55    #[test]
56    fn test_selectable_derive_exists() {
57        // This test ensures the derive macro function exists and can be called
58        // We can't actually test the TokenStream output without a full compile,
59        // but we can test that the function signature is correct
60        let _derive_fn = derive_selectable;
61    }
62}