vk_keyboard/keyboard.rs
1//! Keyboard
2//! ```
3//! use vk_keyboard::button::{Button, ButtonColor, ButtonAction};
4//! use vk_keyboard::keyboard::Keyboard;
5//!
6//! let button = |text: &str| Button::new(ButtonColor::Negative, ButtonAction::Text {
7//! label: text.to_owned(),
8//! payload: String::new(),
9//! });
10//!
11//! let mut keyboard = Keyboard::new(true, vec![], false);
12//! keyboard.add_button(button("hello world"));
13//! keyboard.add_line();
14//! keyboard.add_button(button("another hello world"));
15//! ```
16
17use crate::button::Button;
18use serde::{Deserialize, Serialize};
19
20/// The keyboard type
21#[derive(Debug, Clone, Serialize, Deserialize)]
22pub struct Keyboard {
23 /// Will be it closed after click?
24 pub one_time: bool,
25 /// Buttons
26 pub buttons: Vec<Vec<Button>>,
27 /// Is it inline?
28 pub inline: bool,
29}
30
31/// Methods
32impl Keyboard {
33 /// Create a keyboard from `one_time`, `buttons` and `inline`.
34 pub fn new(one_time: bool, mut buttons: Vec<Vec<Button>>, inline: bool) -> Self {
35 if buttons.len() == 0 {
36 buttons.push(vec![]);
37 }
38 Self {
39 one_time,
40 buttons,
41 inline,
42 }
43 }
44
45 /// Add a button.
46 /// ```
47 /// use vk_keyboard::button::*;
48 /// use vk_keyboard::keyboard::Keyboard;
49 /// let button = Button::new(ButtonColor::Primary, ButtonAction::Text
50 /// {
51 /// label: "Hello world!".to_owned(),
52 /// payload: String::new()
53 /// });
54 /// let mut keyboard = Keyboard::new(true, vec![], false);
55 /// keyboard.add_button(button);
56 ///
57 /// ```
58 pub fn add_button(&mut self, button: Button) {
59 self.buttons.last_mut().unwrap().push(button);
60 }
61
62 /// Add a line.
63 /// ```
64 /// use vk_keyboard::keyboard::Keyboard;
65 /// let mut keyboard = Keyboard::new(true, vec![], false);
66 /// // add buttons..
67 /// keyboard.add_line();
68 /// // add another buttons..
69 /// ```
70 pub fn add_line(&mut self) {
71 self.buttons.push(vec![]);
72 }
73}