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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
use derive_more::From;
use serde::{Deserialize, Serialize};

use crate::types::{
    ForceReply, InlineKeyboardButton, InlineKeyboardMarkup, KeyboardButton, KeyboardMarkup,
    KeyboardRemove,
};

#[derive(Clone, Debug, Eq, Hash, PartialEq, Serialize, Deserialize, From)]
#[serde(untagged)]
pub enum ReplyMarkup {
    InlineKeyboard(InlineKeyboardMarkup),
    Keyboard(KeyboardMarkup),
    KeyboardRemove(KeyboardRemove),
    ForceReply(ForceReply),
}

impl ReplyMarkup {
    /// Constructor for [`InlineKeyboard`] variant.
    ///
    /// This is a shortcut to
    /// `ReplyMarkup::InlineKeyboard(InlineKeyboardMarkup::new(_))`.
    ///
    /// [`InlineKeyboard`]: ReplyMarkup::InlineKeyboard
    pub fn inline_kb<I>(inline_keyboard: I) -> Self
    where
        I: IntoIterator,
        I::Item: IntoIterator<Item = InlineKeyboardButton>,
    {
        Self::InlineKeyboard(InlineKeyboardMarkup::new(inline_keyboard))
    }

    /// Constructor for [`Keyboard`] variant.
    ///
    /// This is a shortcut to
    /// `ReplyMarkup::Keyboard(KeyboardMarkup::new(_))`.
    ///
    /// [`Keyboard`]: ReplyMarkup::Keyboard
    pub fn keyboard<K>(keyboard: K) -> Self
    where
        K: IntoIterator,
        K::Item: IntoIterator<Item = KeyboardButton>,
    {
        Self::Keyboard(KeyboardMarkup::new(keyboard))
    }

    /// Constructor for [`KeyboardRemove`] variant.
    ///
    /// This is a shortcut to
    /// `ReplyMarkup::KeyboardRemove(ReplyKeyboardRemove::new()))`.
    ///
    /// [`KeyboardRemove`]: ReplyMarkup::KeyboardRemove
    #[must_use]
    pub fn kb_remove() -> Self {
        Self::KeyboardRemove(KeyboardRemove::new())
    }

    /// Constructor for [`ForceReply`] variant.
    ///
    /// This is a shortcut to `ReplyMarkup::ForceReply(ForceReply::new())`.
    ///
    /// [`ForceReply`]: ReplyMarkup::KeyboardRemove
    #[must_use]
    pub fn force_reply() -> Self {
        Self::ForceReply(ForceReply::new())
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn inline_keyboard_markup() {
        let data = InlineKeyboardMarkup::default();
        let expected = ReplyMarkup::InlineKeyboard(data.clone());
        let actual: ReplyMarkup = data.into();
        assert_eq!(actual, expected)
    }
}