1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
use yew::prelude::*;

/// Definition of an action.
#[derive(Clone, PartialEq, Default, Debug)]
pub struct Action {
    /// The label for the end user
    pub label: String,
    /// The callback to execute when the action is triggered
    pub callback: Callback<()>,
}

impl Action {
    pub fn new<S>(label: S, callback: Callback<()>) -> Self
    where
        S: ToString,
    {
        Self {
            label: label.to_string(),
            callback,
        }
    }
}

/// Allows converting something into an [`Action`] by providing a label.
pub trait IntoAction {
    fn into_action<S: ToString>(self, label: S) -> Action;
}

impl IntoAction for Callback<()> {
    fn into_action<S>(self, label: S) -> Action
    where
        S: ToString,
    {
        Action::new(label, self)
    }
}