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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
use crate::types::{callback::Game, LoginUrl};
use is_macro::Is;
use serde::{ser::SerializeMap, Serialize};
pub type Markup<'a> = &'a [&'a [Button<'a>]];
#[derive(Debug, PartialEq, Eq, Clone, Copy, Hash, Is)]
#[non_exhaustive]
#[must_use]
pub enum ButtonKind<'a> {
Url(&'a str),
LoginUrl(LoginUrl<'a>),
CallbackData(&'a str),
SwitchInlineQuery(&'a str),
SwitchInlineQueryCurrentChat(&'a str),
CallbackGame(Game),
Pay(bool),
}
#[derive(Debug, PartialEq, Eq, Clone, Copy, Hash)]
#[must_use]
#[must_use]
pub struct Button<'a> {
text: &'a str,
kind: ButtonKind<'a>,
}
#[derive(Debug, PartialEq, Eq, Clone, Copy, Hash, Serialize)]
#[must_use]
pub struct Keyboard<'a> {
inline_keyboard: Markup<'a>,
}
impl<'a> Button<'a> {
pub const fn new(text: &'a str, kind: ButtonKind<'a>) -> Self {
Self { text, kind }
}
}
impl Serialize for Button<'_> {
fn serialize<S: serde::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
let mut map = s.serialize_map(Some(2))?;
map.serialize_entry("text", self.text)?;
match self.kind {
ButtonKind::Url(url) => map.serialize_entry("url", url),
ButtonKind::LoginUrl(login_url) => {
map.serialize_entry("login_url", &login_url)
}
ButtonKind::CallbackData(callback_data) => {
map.serialize_entry("callback_data", callback_data)
}
ButtonKind::SwitchInlineQuery(query) => {
map.serialize_entry("switch_inline_query", query)
}
ButtonKind::SwitchInlineQueryCurrentChat(query) => {
map.serialize_entry("switch_inline_query_current_chat", query)
}
ButtonKind::CallbackGame(game) => {
map.serialize_entry("callback_game", &game)
}
ButtonKind::Pay(pay) => map.serialize_entry("pay", &pay),
}?;
map.end()
}
}
impl<'a> Keyboard<'a> {
pub const fn new(buttons: Markup<'a>) -> Self {
Self {
inline_keyboard: buttons,
}
}
}
impl<'a> From<Markup<'a>> for Keyboard<'a> {
fn from(markup: Markup<'a>) -> Self {
Self::new(markup)
}
}