safe_vk/
keyboard.rs

1use crate::ButtonAbstraction;
2use serde::{Deserialize, Serialize};
3
4#[derive(Deserialize, Serialize, Debug)]
5#[serde(untagged)]
6pub enum KeyboardAction<T> {
7    Text {
8        #[serde(rename = "type")]
9        button_type: String,
10        label: String,
11        payload: T,
12    },
13    OpenLink {
14        #[serde(rename = "type")]
15        button_type: String,
16        link: String,
17        label: String,
18        payload: T,
19    },
20    Location {
21        #[serde(rename = "type")]
22        button_type: String,
23        payload: T,
24    },
25    VkPay {
26        #[serde(rename = "type")]
27        button_type: String,
28        payload: T,
29        hash: String,
30    },
31    OpenApp {
32        #[serde(rename = "type")]
33        button_type: String,
34        app_id: u32,
35        owner_id: u32,
36        payload: T,
37        label: String,
38        hash: String,
39    },
40    Callback {
41        #[serde(rename = "type")]
42        button_type: String,
43        label: String,
44        payload: T,
45    },
46}
47
48#[derive(Deserialize, Serialize, Debug)]
49#[serde(rename_all = "lowercase")]
50pub enum KeyboardColor {
51    Primary,
52    Secondary,
53    Negative,
54    Positive,
55}
56
57#[derive(Deserialize, Serialize, Debug)]
58pub struct Keyboard<T> {
59    one_time: bool,
60    inline: bool,
61    buttons: Vec<Button<T>>,
62}
63
64#[derive(Deserialize, Serialize, Debug)]
65pub struct Button<T> {
66    action: KeyboardAction<T>,
67    color: Option<KeyboardColor>,
68}
69
70impl<T> ButtonAbstraction<T> for Button<T> {
71    fn new(action: KeyboardAction<T>, color: Option<KeyboardColor>) -> Self {
72        Button { action, color }
73    }
74
75    fn text(label: &str, payload: T, color: KeyboardColor) -> Self {
76        let button_type = String::from("text");
77        Button::new(
78            KeyboardAction::Text {
79                button_type,
80                label: label.to_string(),
81                payload,
82            },
83            Some(color),
84        )
85    }
86
87    fn open_link(link: &str, label: &str, payload: T) -> Self {
88        let button_type = String::from("open_link");
89        Button::new(
90            KeyboardAction::OpenLink {
91                button_type,
92                link: link.to_string(),
93                label: label.to_string(),
94                payload,
95            },
96            None,
97        )
98    }
99
100    fn location(payload: T) -> Self {
101        let button_type = String::from("location");
102        Button::new(
103            KeyboardAction::Location {
104                button_type,
105                payload,
106            },
107            None,
108        )
109    }
110
111    fn vkpay(payload: T, hash: &str) -> Self {
112        let button_type = String::from("vkpay");
113        Button::new(
114            KeyboardAction::VkPay {
115                button_type,
116                payload,
117                hash: hash.to_string(),
118            },
119            None,
120        )
121    }
122
123    fn open_app(app_id: u32, owner_id: u32, payload: T, label: &str, hash: &str) -> Self {
124        let button_type = String::from("open_app");
125        Button::new(
126            KeyboardAction::OpenApp {
127                button_type,
128                app_id,
129                owner_id,
130                payload,
131                label: label.to_string(),
132                hash: hash.to_string(),
133            },
134            None,
135        )
136    }
137
138    fn callback(label: &str, payload: T, color: KeyboardColor) -> Self {
139        let button_type = String::from("callback");
140        Button::new(
141            KeyboardAction::Callback {
142                button_type,
143                label: label.to_string(),
144                payload,
145            },
146            Some(color),
147        )
148    }
149}