rust_tdlib/types/
keyboard_button.rs

1use crate::errors::Result;
2use crate::types::*;
3use uuid::Uuid;
4
5/// Represents a single button in a bot keyboard
6#[derive(Debug, Clone, Default, Serialize, Deserialize)]
7pub struct KeyboardButton {
8    #[doc(hidden)]
9    #[serde(rename(serialize = "@extra", deserialize = "@extra"))]
10    extra: Option<String>,
11    #[serde(rename(serialize = "@client_id", deserialize = "@client_id"))]
12    client_id: Option<i32>,
13    /// Text of the button
14
15    #[serde(default)]
16    text: String,
17    /// Type of the button
18
19    #[serde(rename(serialize = "type", deserialize = "type"))]
20    #[serde(skip_serializing_if = "KeyboardButtonType::_is_default")]
21    type_: KeyboardButtonType,
22}
23
24impl RObject for KeyboardButton {
25    #[doc(hidden)]
26    fn extra(&self) -> Option<&str> {
27        self.extra.as_deref()
28    }
29    #[doc(hidden)]
30    fn client_id(&self) -> Option<i32> {
31        self.client_id
32    }
33}
34
35impl KeyboardButton {
36    pub fn from_json<S: AsRef<str>>(json: S) -> Result<Self> {
37        Ok(serde_json::from_str(json.as_ref())?)
38    }
39    pub fn builder() -> KeyboardButtonBuilder {
40        let mut inner = KeyboardButton::default();
41        inner.extra = Some(Uuid::new_v4().to_string());
42
43        KeyboardButtonBuilder { inner }
44    }
45
46    pub fn text(&self) -> &String {
47        &self.text
48    }
49
50    pub fn type_(&self) -> &KeyboardButtonType {
51        &self.type_
52    }
53}
54
55#[doc(hidden)]
56pub struct KeyboardButtonBuilder {
57    inner: KeyboardButton,
58}
59
60#[deprecated]
61pub type RTDKeyboardButtonBuilder = KeyboardButtonBuilder;
62
63impl KeyboardButtonBuilder {
64    pub fn build(&self) -> KeyboardButton {
65        self.inner.clone()
66    }
67
68    pub fn text<T: AsRef<str>>(&mut self, text: T) -> &mut Self {
69        self.inner.text = text.as_ref().to_string();
70        self
71    }
72
73    pub fn type_<T: AsRef<KeyboardButtonType>>(&mut self, type_: T) -> &mut Self {
74        self.inner.type_ = type_.as_ref().clone();
75        self
76    }
77}
78
79impl AsRef<KeyboardButton> for KeyboardButton {
80    fn as_ref(&self) -> &KeyboardButton {
81        self
82    }
83}
84
85impl AsRef<KeyboardButton> for KeyboardButtonBuilder {
86    fn as_ref(&self) -> &KeyboardButton {
87        &self.inner
88    }
89}