cartographer_rs/
menu_macros.rs

1/// Creates a [`MenuItem`](crate::MenuItem), filling in the defaults if values are not provided
2///
3/// ## Example
4/// ```
5/// menu_item!("A Menu Item", true, 2, ["alt search"])
6///
7/// // Is equal to
8///
9/// MenuItem {
10///     visible_name: "A Menu Item".to_string(),
11///     visible_at_rest: true,
12///     at_rest_position: Some(2),
13///     alternative_matches: Some(vec!["alt search"]),
14///     }
15/// ```
16#[macro_export]
17macro_rules! menu_item {
18    ($name:expr) => {
19        {
20            use cartographer_rs::MenuItem;
21            MenuItem::new($name.to_string())
22        }
23    };
24    ($name:expr, $visible_at_rest:expr) => {
25        {
26            use cartographer_rs::MenuItem;
27            MenuItem::new($name.to_string()).visible_at_rest($visible_at_rest)
28        }
29    };
30    ($name:expr, $visible_at_rest:expr, $default_position:expr) => {
31        {
32            use cartographer_rs::MenuItem;
33            MenuItem::new($name.to_string())
34                .visible_at_rest($visible_at_rest)
35                .at_rest_position($default_position)
36        }
37    };
38    ($name:expr, $visible_at_rest:expr, $default_position:expr, [$($alt_matches:expr),+]) => {
39        {
40            use cartographer_rs::MenuItem;
41            MenuItem::new($name.to_string())
42                .visible_at_rest($visible_at_rest)
43                .at_rest_position($default_position)
44                .add_alternative_match({
45                    let mut matches = Vec::<String>::new();
46                    $(
47                        matches.push($alt_matches.to_string());
48                    )+
49                    matches
50                })
51        }
52    };
53}
54
55/// Creates a [`Menu`](crate::Menu) in one line
56///
57/// For the best experience, pair it with the [`menu_item!`](crate::menu_item!) macro for simple menu declaration.
58///
59/// You can also configure the menu by passing a [`MenuOptions`](crate::MenuOptions). If this is not provided,
60/// the defaults are used instead
61///
62/// ## Example
63/// ```
64/// let configuration = MenuOptions::new();
65/// let menu = menu!(
66///       "Pick a number: ",
67///       configuration,
68///       [
69///           menu_item!("Item Number 1", true, 1),
70///           menu_item!("Item Number 2", true, 1),
71///       ]
72/// )
73///
74/// let usr_choice = menu.serve()?;
75/// println!("{}", usr_choice);
76/// ```
77#[macro_export]
78macro_rules! menu {
79    ( $prompt:expr, [$( $menu_item:expr ),*]) => {
80        {
81            use cartographer_rs::{MenuItem, Menu, MenuOptions};
82            let mut items = Vec::<MenuItem>::new();
83            $(
84                items.push($menu_item);
85            )*
86            Menu::new(
87                $prompt.to_string(),
88                items,
89                Some(MenuOptions::default())
90            )
91        }
92
93    };
94    ( $prompt:expr, $configuration:expr, [$( $menu_item:expr ),*]) => {
95        {
96            use cartographer_rs::{MenuItem, Menu};
97            let mut items = Vec::<MenuItem>::new();
98            $(
99                items.push($menu_item);
100            )*
101            Menu::new(
102                $prompt.to_string(),
103                items,
104                Some($configuration)
105            )
106        }
107
108    };
109}