telegram_rich/types/
button.rs1use serde::{Deserialize, Serialize};
4
5use super::common::{
6 CopyTextButton, DisabledButton, LoginUrl, SwitchInlineQueryChosenChat, WebAppInfo,
7};
8use super::rich_text::RichText;
9
10#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
12#[serde(rename_all = "snake_case")]
13pub enum ButtonStyle {
14 Danger,
16 Success,
18 Primary,
20 Link,
22}
23
24#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
28pub struct RichMessageButton {
29 pub text: RichText,
31 #[serde(default, skip_serializing_if = "Option::is_none")]
33 pub style: Option<ButtonStyle>,
34 #[serde(default, skip_serializing_if = "Option::is_none")]
36 pub url: Option<String>,
37 #[serde(default, skip_serializing_if = "Option::is_none")]
39 pub callback_data: Option<String>,
40 #[serde(default, skip_serializing_if = "Option::is_none")]
42 pub web_app: Option<WebAppInfo>,
43 #[serde(default, skip_serializing_if = "Option::is_none")]
45 pub login_url: Option<LoginUrl>,
46 #[serde(default, skip_serializing_if = "Option::is_none")]
48 pub switch_inline_query: Option<String>,
49 #[serde(default, skip_serializing_if = "Option::is_none")]
51 pub switch_inline_query_current_chat: Option<String>,
52 #[serde(default, skip_serializing_if = "Option::is_none")]
54 pub switch_inline_query_chosen_chat: Option<SwitchInlineQueryChosenChat>,
55 #[serde(default, skip_serializing_if = "Option::is_none")]
57 pub copy_text: Option<CopyTextButton>,
58 #[serde(default, skip_serializing_if = "Option::is_none")]
60 pub disabled: Option<DisabledButton>,
61}
62
63impl RichMessageButton {
64 fn empty(text: RichText) -> Self {
65 Self {
66 text,
67 style: None,
68 url: None,
69 callback_data: None,
70 web_app: None,
71 login_url: None,
72 switch_inline_query: None,
73 switch_inline_query_current_chat: None,
74 switch_inline_query_chosen_chat: None,
75 copy_text: None,
76 disabled: None,
77 }
78 }
79
80 pub fn url(text: impl Into<RichText>, url: impl Into<String>) -> Self {
82 Self {
83 url: Some(url.into()),
84 ..Self::empty(text.into())
85 }
86 }
87
88 pub fn callback(text: impl Into<RichText>, data: impl Into<String>) -> Self {
90 Self {
91 callback_data: Some(data.into()),
92 ..Self::empty(text.into())
93 }
94 }
95
96 pub fn copy_text(text: impl Into<RichText>, copy: impl Into<String>) -> Self {
98 Self {
99 copy_text: Some(CopyTextButton { text: copy.into() }),
100 ..Self::empty(text.into())
101 }
102 }
103
104 pub fn with_style(mut self, style: ButtonStyle) -> Self {
106 self.style = Some(style);
107 self
108 }
109}