use crate::types::{callback::Game, LoginUrl};
use serde::{ser::SerializeMap, Serialize};
pub type Markup<'a> = &'a [&'a [Button<'a>]];
#[derive(Debug, PartialEq, Eq, Clone, Copy, Hash)]
pub enum ButtonKind<'a> {
Url(&'a str),
LoginUrl(LoginUrl<'a>),
CallbackData(&'a str),
SwitchInlineQuery(&'a str),
SwitchInlineQueryCurrentChat(&'a str),
CallbackGame(Game),
Pay(bool),
}
impl ButtonKind<'_> {
pub fn is_url(&self) -> bool {
match self {
ButtonKind::Url(..) => true,
_ => false,
}
}
pub fn is_login_url(&self) -> bool {
match self {
ButtonKind::LoginUrl(..) => true,
_ => false,
}
}
pub fn is_callback_data(&self) -> bool {
match self {
ButtonKind::CallbackData(..) => true,
_ => false,
}
}
pub fn is_switch_inline_query(&self) -> bool {
match self {
ButtonKind::SwitchInlineQuery(..) => true,
_ => false,
}
}
pub fn is_switch_inline_query_current_chat(&self) -> bool {
match self {
ButtonKind::SwitchInlineQueryCurrentChat(..) => true,
_ => false,
}
}
pub fn is_callback_game(&self) -> bool {
match self {
ButtonKind::CallbackGame(..) => true,
_ => false,
}
}
pub fn is_pay(&self) -> bool {
match self {
ButtonKind::Pay(..) => true,
_ => false,
}
}
}
#[derive(Debug, PartialEq, Eq, Clone, Copy, Hash)]
#[must_use]
pub struct Button<'a> {
text: &'a str,
kind: ButtonKind<'a>,
}
#[derive(Debug, PartialEq, Eq, Clone, Copy, Hash, Serialize)]
pub struct Keyboard<'a> {
inline_keyboard: Markup<'a>,
}
impl<'a> Button<'a> {
pub const fn new(text: &'a str, kind: ButtonKind<'a>) -> Self {
Self { text, kind }
}
}
impl Serialize for Button<'_> {
fn serialize<S: serde::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
let mut map = s.serialize_map(Some(2))?;
map.serialize_entry("text", self.text)?;
match self.kind {
ButtonKind::Url(url) => map.serialize_entry("url", url),
ButtonKind::LoginUrl(login_url) => {
map.serialize_entry("login_url", &login_url)
}
ButtonKind::CallbackData(callback_data) => {
map.serialize_entry("callback_data", callback_data)
}
ButtonKind::SwitchInlineQuery(query) => {
map.serialize_entry("switch_inline_query", query)
}
ButtonKind::SwitchInlineQueryCurrentChat(query) => {
map.serialize_entry("switch_inline_query_current_chat", query)
}
ButtonKind::CallbackGame(game) => {
map.serialize_entry("callback_game", &game)
}
ButtonKind::Pay(pay) => map.serialize_entry("pay", &pay),
}?;
map.end()
}
}
impl<'a> Keyboard<'a> {
pub const fn new(buttons: Markup<'a>) -> Self {
Self {
inline_keyboard: buttons,
}
}
}
impl<'a> From<Markup<'a>> for Keyboard<'a> {
fn from(markup: Markup<'a>) -> Self {
Self::new(markup)
}
}