Add more constructors to InlineKeyboardButton

This commit is contained in:
Maybe Waffle 2022-06-26 20:05:16 +04:00
parent 3a2c8a8ffe
commit 71555ba43e

View file

@ -13,31 +13,6 @@ pub struct InlineKeyboardButton {
pub kind: InlineKeyboardButtonKind,
}
impl InlineKeyboardButton {
pub fn new<S>(text: S, kind: InlineKeyboardButtonKind) -> Self
where
S: Into<String>,
{
Self {
text: text.into(),
kind,
}
}
pub fn text<S>(mut self, val: S) -> Self
where
S: Into<String>,
{
self.text = val.into();
self
}
pub fn kind(mut self, val: InlineKeyboardButtonKind) -> Self {
self.kind = val;
self
}
}
#[derive(Clone, Debug, Eq, Hash, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum InlineKeyboardButtonKind {
@ -113,16 +88,17 @@ pub enum InlineKeyboardButtonKind {
Pay(True),
}
/// Build buttons.
///
/// # Examples
/// ```
/// use teloxide_core::types::InlineKeyboardButton;
///
/// let url = url::Url::parse("https://example.com").unwrap();
/// let url_button = InlineKeyboardButton::url("Text".to_string(), url);
/// ```
impl InlineKeyboardButton {
pub fn new<S>(text: S, kind: InlineKeyboardButtonKind) -> Self
where
S: Into<String>,
{
Self {
text: text.into(),
kind,
}
}
pub fn url<T>(text: T, url: reqwest::Url) -> InlineKeyboardButton
where
T: Into<String>,
@ -133,6 +109,16 @@ impl InlineKeyboardButton {
}
}
pub fn login<T>(text: T, url: LoginUrl) -> InlineKeyboardButton
where
T: Into<String>,
{
InlineKeyboardButton {
text: text.into(),
kind: InlineKeyboardButtonKind::LoginUrl(url),
}
}
pub fn callback<T, C>(text: T, callback_data: C) -> InlineKeyboardButton
where
T: Into<String>,
@ -144,6 +130,16 @@ impl InlineKeyboardButton {
}
}
pub fn web_app<T>(text: T, info: WebAppInfo) -> InlineKeyboardButton
where
T: Into<String>,
{
InlineKeyboardButton {
text: text.into(),
kind: InlineKeyboardButtonKind::WebApp(info),
}
}
pub fn switch_inline_query<T, Q>(text: T, switch_inline_query: Q) -> InlineKeyboardButton
where
T: Into<String>,
@ -170,4 +166,37 @@ impl InlineKeyboardButton {
),
}
}
pub fn callback_game<T>(text: T, game: CallbackGame) -> InlineKeyboardButton
where
T: Into<String>,
{
InlineKeyboardButton {
text: text.into(),
kind: InlineKeyboardButtonKind::CallbackGame(game),
}
}
pub fn pay<T>(text: T) -> InlineKeyboardButton
where
T: Into<String>,
{
InlineKeyboardButton {
text: text.into(),
kind: InlineKeyboardButtonKind::Pay(True),
}
}
pub fn text<S>(mut self, val: S) -> Self
where
S: Into<String>,
{
self.text = val.into();
self
}
pub fn kind(mut self, val: InlineKeyboardButtonKind) -> Self {
self.kind = val;
self
}
}