refactor ChatOrInlineMessage

- Rename `ChatOrInlineMessage` => `TargetMessage`
- Change type of `TargetMessage::inline_message_id` `i32` => `String`
- Remove `#[non_exhaustive]` annotation as this enum is unlikely to be changed in future
- Implement `From<String>`
- Use `From<String>` impl in `get_game_high_scores` and `set_game_score` methods
- Leave a deprecated pub use as `ChatOrInlineMessage` for a bit of backward compat
This commit is contained in:
Waffle 2020-08-07 00:49:47 +03:00
parent 503f406edd
commit 851e4c6bc6
6 changed files with 65 additions and 53 deletions

View file

@ -1563,18 +1563,18 @@ impl Bot {
/// [The official docs](https://core.telegram.org/bots/api#setgamescore).
///
/// # Params
/// - `target`: Target message, either chat id and message id or inline
/// message id.
/// - `user_id`: User identifier.
/// - `score`: New score, must be non-negative.
///
/// [`Message`]: crate::types::Message
/// [`True`]: crate::types::True
pub fn set_game_score(
&self,
chat_or_inline_message: ChatOrInlineMessage,
user_id: i32,
score: i32,
) -> SetGameScore {
SetGameScore::new(self.clone(), chat_or_inline_message, user_id, score)
pub fn set_game_score<T>(&self, target: T, user_id: i32, score: i32) -> SetGameScore
where
T: Into<TargetMessage>,
{
SetGameScore::new(self.clone(), target, user_id, score)
}
/// Use this method to get data for high score tables.
@ -1591,13 +1591,14 @@ impl Bot {
/// [The official docs](https://core.telegram.org/bots/api#getgamehighscores).
///
/// # Params
/// - `target`: Target message, either chat id and message id or inline
/// message id.
/// - `user_id`: Target user id.
pub fn get_game_high_scores(
&self,
chat_or_inline_message: ChatOrInlineMessage,
user_id: i32,
) -> GetGameHighScores {
GetGameHighScores::new(self.clone(), chat_or_inline_message, user_id)
pub fn get_game_high_scores<T>(&self, target: T, user_id: i32) -> GetGameHighScores
where
T: Into<TargetMessage>,
{
GetGameHighScores::new(self.clone(), target, user_id)
}
/// Use this method to set a custom title for an administrator in a

View file

@ -3,7 +3,7 @@ use serde::Serialize;
use crate::{
net,
requests::{Request, ResponseResult},
types::{ChatOrInlineMessage, GameHighScore},
types::{GameHighScore, TargetMessage},
Bot,
};
@ -18,14 +18,13 @@ use crate::{
/// the user and his neighbors are not among them. Please note that this
/// behavior is subject to change.
///
/// [The official docs](https://core.telegram.org/bots/api#getgamehighscores).
#[serde_with_macros::skip_serializing_none]
/// [The official docs](https://core.telegram.org/bots/api#getgamehighscores)
#[derive(Debug, Clone, Serialize)]
pub struct GetGameHighScores {
#[serde(skip_serializing)]
bot: Bot,
#[serde(flatten)]
chat_or_inline_message: ChatOrInlineMessage,
target: TargetMessage,
user_id: i32,
}
@ -39,12 +38,20 @@ impl Request for GetGameHighScores {
}
impl GetGameHighScores {
pub(crate) fn new(bot: Bot, chat_or_inline_message: ChatOrInlineMessage, user_id: i32) -> Self {
Self { bot, chat_or_inline_message, user_id }
pub(crate) fn new<T>(bot: Bot, target: T, user_id: i32) -> Self
where
T: Into<TargetMessage>,
{
let target = target.into();
Self { bot, target, user_id }
}
pub fn chat_or_inline_message(mut self, val: ChatOrInlineMessage) -> Self {
self.chat_or_inline_message = val;
/// Target message, either chat id and message id or inline message id.
pub fn target<T>(mut self, val: T) -> Self
where
T: Into<TargetMessage>,
{
self.target = val.into();
self
}

View file

@ -3,7 +3,7 @@ use serde::Serialize;
use crate::{
net,
requests::{Request, ResponseResult},
types::{ChatOrInlineMessage, Message},
types::{Message, TargetMessage},
Bot,
};
@ -24,7 +24,7 @@ pub struct SetGameScore {
#[serde(skip_serializing)]
bot: Bot,
#[serde(flatten)]
chat_or_inline_message: ChatOrInlineMessage,
target: TargetMessage,
user_id: i32,
score: i32,
force: Option<bool>,
@ -41,24 +41,20 @@ impl Request for SetGameScore {
}
impl SetGameScore {
pub(crate) fn new(
bot: Bot,
chat_or_inline_message: ChatOrInlineMessage,
user_id: i32,
score: i32,
) -> Self {
Self {
bot,
chat_or_inline_message,
user_id,
score,
force: None,
disable_edit_message: None,
}
pub(crate) fn new<T>(bot: Bot, target: T, user_id: i32, score: i32) -> Self
where
T: Into<TargetMessage>,
{
let target = target.into();
Self { bot, target, user_id, score, force: None, disable_edit_message: None }
}
pub fn chat_or_inline_message(mut self, val: ChatOrInlineMessage) -> Self {
self.chat_or_inline_message = val;
/// Target message, either chat id and message id or inline message id.
pub fn target<T>(mut self, val: T) -> Self
where
T: Into<TargetMessage>,
{
self.target = val.into();
self
}

View file

@ -1,12 +0,0 @@
use crate::types::ChatId;
use serde::{Deserialize, Serialize};
/// A chat message or inline message.
#[derive(Clone, Debug, Eq, Hash, PartialEq, Serialize, Deserialize)]
#[serde(untagged)]
#[non_exhaustive]
pub enum ChatOrInlineMessage {
Chat { chat_id: ChatId, message_id: i32 },
Inline { inline_message_id: i32 },
}

View file

@ -10,7 +10,6 @@ pub use chat::*;
pub use chat_action::*;
pub use chat_id::*;
pub use chat_member::*;
pub use chat_or_inline_message::*;
pub use chat_permissions::*;
pub use chat_photo::*;
pub use chosen_inline_result::*;
@ -83,6 +82,7 @@ pub use sticker::*;
pub use sticker_set::*;
pub use sticker_type::*;
pub use successful_payment::*;
pub use target_message::*;
pub use unit_false::*;
pub use unit_true::*;
pub use update::*;
@ -104,7 +104,6 @@ mod chat;
mod chat_action;
mod chat_id;
mod chat_member;
mod chat_or_inline_message;
mod chat_permissions;
mod chat_photo;
mod chosen_inline_result;
@ -150,6 +149,7 @@ mod sticker;
mod sticker_set;
mod sticker_type;
mod successful_payment;
mod target_message;
mod unit_false;
mod unit_true;
mod update;

View file

@ -0,0 +1,20 @@
use crate::types::ChatId;
use serde::{Deserialize, Serialize};
/// A message in chat or inline message.
#[derive(Clone, Debug, Eq, Hash, PartialEq, Serialize, Deserialize)]
#[serde(untagged)]
pub enum TargetMessage {
Chat { chat_id: ChatId, message_id: i32 },
Inline { inline_message_id: String },
}
#[deprecated = "Was renamed to `TargetMessage`, please use renamed version"]
pub use TargetMessage as ChatOrInlineMessage;
impl From<String> for TargetMessage {
fn from(inline_message_id: String) -> Self {
Self::Inline { inline_message_id }
}
}