weavetui_core/macros.rs
1/// Creates an array of keybindings.
2///
3/// Each action will be converted into an `ActionKind`.
4///
5/// This macro accepts two syntaxes:
6///
7/// 1. `<key> => <action>` syntax:
8///
9/// ```rust
10/// # use weavetui::{kb, event::Action};
11/// let keybindings = kb![
12/// "<q>" => Action::Quit,
13/// "d" => "app:drink-mate"
14/// ];
15/// ```
16///
17/// 2. `(<key>, <action>)` syntax:
18///
19/// ```rust
20/// # use weavetui::{kb, event::Action};
21/// let keybindings = kb![
22/// ("<q>", Action::Quit),
23/// ("d", "app:drink-mate")
24/// ];
25/// ```
26#[macro_export]
27macro_rules! kb {
28 // Accepts "<key>" => <action> syntax
29 ($($key:expr => $action:expr),* $(,)?) => {
30 [
31 $(($key, $crate::event::ActionKind::from($action))),*
32 ]
33 };
34
35 // Accepts ("<key>", <action>) syntax
36 ($(($key:expr, $action:expr)),* $(,)?) => {
37 [
38 $(($key, $crate::event::ActionKind::from($action))),*
39 ]
40 };
41}
42
43/// Creates a vector of components from a list of component instances.
44///
45/// Each component will be boxed as a `Box<dyn Component>`.
46///
47/// ## Usage
48///
49/// ```rust,ignore
50/// use weavetui::{components, Component};
51///
52/// #[derive(Default)]
53/// struct MyComponent1;
54/// #[derive(Default)]
55/// struct MyComponent2;
56///
57/// // impl Component for MyComponent1 ...
58/// // impl Component for MyComponent2 ...
59///
60/// let my_components: Vec<Box<dyn Component>> = components![
61/// MyComponent1::default(),
62/// MyComponent2::default()
63/// ];
64/// ```
65#[macro_export]
66macro_rules! components {
67 ( $( $x:expr $( => $t:ty )* ),* ) => {
68 {
69 let mut temp_vec = Vec::new();
70 $(
71 temp_vec.push(
72 Box::new($x)
73 as Box<dyn $crate::Component $( $t + )* >
74 );
75 )*
76 temp_vec
77 }
78 };
79}