Merge pull request #242 from teloxide/MUST_USE

Add some `#[must_use]`s
This commit is contained in:
Waffle Maybe 2022-08-19 06:14:39 +04:00 committed by GitHub
commit c9c3c7d03b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
47 changed files with 331 additions and 1 deletions

View file

@ -19,6 +19,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
[pr241]: https://github.com/teloxide/teloxide-core/pull/241
### Changed
- More functions are now marked with `#[must_use]` ([#242][PR242])
[pr242]: https://github.com/teloxide/teloxide-core/pull/242
## 0.7.0 - 2022-07-19
### Added

View file

@ -21,6 +21,7 @@ type BoxedFuture = Pin<Box<dyn Future<Output = ()> + Send>>;
/// ```
///
/// [`Throttle`]: crate::adaptors::throttle::Throttle
#[must_use]
#[non_exhaustive]
pub struct Settings {
pub limits: Limits,

View file

@ -51,6 +51,7 @@ const TELOXIDE_TOKEN: &str = "TELOXIDE_TOKEN";
///
/// [`Arc`]: std::sync::Arc
/// [Telegram Bot API]: https://core.telegram.org/bots/api
#[must_use]
#[derive(Debug, Clone)]
pub struct Bot {
token: Arc<str>,
@ -186,16 +187,19 @@ impl Bot {
/// Getters
impl Bot {
/// Returns currently used token.
#[must_use]
pub fn token(&self) -> &str {
&self.token
}
/// Returns currently used http-client.
#[must_use]
pub fn client(&self) -> &Client {
&self.client
}
/// Returns currently used token API url.
#[must_use]
pub fn api_url(&self) -> reqwest::Url {
reqwest::Url::clone(&*self.api_url)
}

View file

@ -34,6 +34,7 @@ pub const TELEGRAM_API_URL: &str = "https://api.telegram.org";
/// ## Panics
///
/// If `TELOXIDE_PROXY` exists, but isn't correct url.
#[must_use]
pub fn client_from_env() -> reqwest::Client {
use reqwest::Proxy;

View file

@ -191,10 +191,12 @@ pub struct PublicChatSupergroup {
}
impl Chat {
#[must_use]
pub fn is_private(&self) -> bool {
matches!(self.kind, ChatKind::Private(_))
}
#[must_use]
pub fn is_group(&self) -> bool {
matches!(
self.kind,
@ -205,6 +207,7 @@ impl Chat {
)
}
#[must_use]
pub fn is_supergroup(&self) -> bool {
matches!(
self.kind,
@ -215,6 +218,7 @@ impl Chat {
)
}
#[must_use]
pub fn is_channel(&self) -> bool {
matches!(
self.kind,
@ -225,6 +229,7 @@ impl Chat {
)
}
#[must_use]
pub fn is_chat(&self) -> bool {
self.is_private() || self.is_group() || self.is_supergroup()
}
@ -233,6 +238,7 @@ impl Chat {
/// Getters
impl Chat {
/// A title, for supergroups, channels and group chats.
#[must_use]
pub fn title(&self) -> Option<&str> {
match &self.kind {
ChatKind::Public(this) => this.title.as_deref(),
@ -241,6 +247,7 @@ impl Chat {
}
/// A username, for private chats, supergroups and channels if available.
#[must_use]
pub fn username(&self) -> Option<&str> {
match &self.kind {
ChatKind::Public(this) => match &this.kind {
@ -258,6 +265,7 @@ impl Chat {
/// identifier for a channel and vice versa. Returned only in [`GetChat`].
///
/// [`GetChat`]: crate::payloads::GetChat
#[must_use]
pub fn linked_chat_id(&self) -> Option<i64> {
match &self.kind {
ChatKind::Public(this) => match &this.kind {
@ -275,6 +283,7 @@ impl Chat {
/// only from [`GetChat`].
///
/// [`GetChat`]: crate::payloads::GetChat
#[must_use]
pub fn permissions(&self) -> Option<ChatPermissions> {
if let ChatKind::Public(this) = &self.kind {
if let PublicChatKind::Group(PublicChatGroup { permissions })
@ -291,6 +300,7 @@ impl Chat {
/// [`GetChat`].
///
/// [`GetChat`]: crate::payloads::GetChat
#[must_use]
pub fn sticker_set_name(&self) -> Option<&str> {
if let ChatKind::Public(this) = &self.kind {
if let PublicChatKind::Supergroup(this) = &this.kind {
@ -305,6 +315,7 @@ impl Chat {
/// from [`GetChat`].
///
/// [`GetChat`]: crate::payloads::GetChat
#[must_use]
pub fn can_set_sticker_set(&self) -> Option<bool> {
if let ChatKind::Public(this) = &self.kind {
if let PublicChatKind::Supergroup(this) = &this.kind {
@ -319,6 +330,7 @@ impl Chat {
/// unpriviledged user. Returned only from [`GetChat`].
///
/// [`GetChat`]: crate::payloads::GetChat
#[must_use]
pub fn slow_mode_delay(&self) -> Option<u32> {
if let ChatKind::Public(this) = &self.kind {
if let PublicChatKind::Supergroup(this) = &this.kind {
@ -333,6 +345,7 @@ impl Chat {
/// [`GetChat`].
///
/// [`GetChat`]: crate::payloads::GetChat
#[must_use]
pub fn location(&self) -> Option<&ChatLocation> {
if let ChatKind::Public(this) = &self.kind {
if let PublicChatKind::Supergroup(this) = &this.kind {
@ -347,6 +360,7 @@ impl Chat {
/// messages. Returned only in [`GetChat`].
///
/// [`GetChat`]: crate::payloads::GetChat
#[must_use]
pub fn join_to_send_messages(&self) -> Option<True> {
if let ChatKind::Public(this) = &self.kind {
if let PublicChatKind::Supergroup(this) = &this.kind {
@ -361,6 +375,7 @@ impl Chat {
/// by supergroup administrators. Returned only in [`GetChat`].
///
/// [`GetChat`]: crate::payloads::GetChat
#[must_use]
pub fn join_by_request(&self) -> Option<True> {
if let ChatKind::Public(this) = &self.kind {
if let PublicChatKind::Supergroup(this) = &this.kind {
@ -375,6 +390,7 @@ impl Chat {
/// only in [`GetChat`].
///
/// [`GetChat`]: crate::payloads::GetChat
#[must_use]
pub fn description(&self) -> Option<&str> {
match &self.kind {
ChatKind::Public(this) => this.description.as_deref(),
@ -392,6 +408,7 @@ impl Chat {
/// crate::payloads::ExportChatInviteLink
///
/// [`GetChat`]: crate::payloads::GetChat
#[must_use]
pub fn invite_link(&self) -> Option<&str> {
match &self.kind {
ChatKind::Public(this) => this.invite_link.as_deref(),
@ -403,6 +420,7 @@ impl Chat {
/// Returned only in [`GetChat`].
///
/// [`GetChat`]: crate::payloads::GetChat
#[must_use]
pub fn has_protected_content(&self) -> Option<True> {
match &self.kind {
ChatKind::Public(this) => this.has_protected_content,
@ -411,6 +429,7 @@ impl Chat {
}
/// A first name of the other party in a private chat.
#[must_use]
pub fn first_name(&self) -> Option<&str> {
match &self.kind {
ChatKind::Private(this) => this.first_name.as_deref(),
@ -419,6 +438,7 @@ impl Chat {
}
/// A last name of the other party in a private chat.
#[must_use]
pub fn last_name(&self) -> Option<&str> {
match &self.kind {
ChatKind::Private(this) => this.last_name.as_deref(),
@ -429,6 +449,7 @@ impl Chat {
/// Bio of the other party in a private chat. Returned only in [`GetChat`].
///
/// [`GetChat`]: crate::payloads::GetChat
#[must_use]
pub fn bio(&self) -> Option<&str> {
match &self.kind {
ChatKind::Private(this) => this.bio.as_deref(),
@ -441,6 +462,7 @@ impl Chat {
/// user. Returned only in [`GetChat`].
///
/// [`GetChat`]: crate::payloads::GetChat
#[must_use]
pub fn has_private_forwards(&self) -> Option<True> {
match &self.kind {
ChatKind::Private(this) => this.has_private_forwards,

View file

@ -31,6 +31,7 @@ pub(crate) enum BareChatId {
impl ChatId {
/// Returns `true` if this is an id of a user.
#[must_use]
pub fn is_user(self) -> bool {
matches!(self.to_bare(), BareChatId::User(_))
}
@ -38,11 +39,13 @@ impl ChatId {
/// Returns `true` if this is an id of a group.
///
/// Note: supergroup is **not** considered a group.
#[must_use]
pub fn is_group(self) -> bool {
matches!(self.to_bare(), BareChatId::Group(_))
}
/// Returns `true` if this is an id of a channel.
#[must_use]
pub fn is_channel_or_supergroup(self) -> bool {
matches!(self.to_bare(), BareChatId::Channel(_))
}

View file

@ -147,6 +147,7 @@ impl Deref for ChatMember {
/// Simple methods for checking a user status.
impl ChatMemberKind {
/// Returns chat member status.
#[must_use]
pub fn status(&self) -> ChatMemberStatus {
match self {
ChatMemberKind::Owner(_) => ChatMemberStatus::Owner,
@ -161,6 +162,7 @@ impl ChatMemberKind {
/// Returns `true` if the user is the [owner] of the given chat.
///
/// [owner]: ChatMemberKind::Owner
#[must_use]
pub fn is_owner(&self) -> bool {
matches!(self, Self::Owner { .. })
}
@ -173,6 +175,7 @@ impl ChatMemberKind {
/// owner of the given chat. See also: [`is_privileged`].
///
/// [`is_privileged`]: ChatMemberKind::is_privileged
#[must_use]
pub fn is_administrator(&self) -> bool {
matches!(self, Self::Administrator { .. })
}
@ -180,6 +183,7 @@ impl ChatMemberKind {
/// Returns `true` if the user is a common [member] of the given chat.
///
/// [member]: ChatMemberKind::Member
#[must_use]
pub fn is_member(&self) -> bool {
matches!(self, Self::Member { .. })
}
@ -187,6 +191,7 @@ impl ChatMemberKind {
/// Returns `true` if the user is [restricted] in the given chat.
///
/// [restricted]: ChatMemberKind::Restricted
#[must_use]
pub fn is_restricted(&self) -> bool {
matches!(self, Self::Restricted { .. })
}
@ -194,6 +199,7 @@ impl ChatMemberKind {
/// Returns `true` if the user [left] the given chat.
///
/// [left]: ChatMemberKind::Left
#[must_use]
pub fn is_left(&self) -> bool {
matches!(self, Self::Left { .. })
}
@ -201,6 +207,7 @@ impl ChatMemberKind {
/// Returns `true` if the user is [banned] in the given chat.
///
/// [banned]: ChatMemberKind::Banned
#[must_use]
pub fn is_banned(&self) -> bool {
matches!(self, Self::Banned { .. })
}
@ -209,6 +216,7 @@ impl ChatMemberKind {
///
/// [kicked]: ChatMemberKind::Banned
#[deprecated = "use `is_banned` instead"]
#[must_use]
pub fn is_kicked(&self) -> bool {
self.is_banned()
}
@ -217,6 +225,7 @@ impl ChatMemberKind {
///
/// [creator]: ChatMemberKind::Owner
#[deprecated = "use `is_owner` instead"]
#[must_use]
pub fn is_creator(&self) -> bool {
self.is_owner()
}
@ -229,6 +238,7 @@ impl ChatMemberKind {
///
/// [owner]: ChatMemberKind::Owner
/// [administrator]: ChatMemberKind::Administrator
#[must_use]
pub fn is_privileged(&self) -> bool {
self.is_administrator() || self.is_owner()
}
@ -238,6 +248,7 @@ impl ChatMemberKind {
///
/// [left]: ChatMemberKind::Left
/// [banned]: ChatMemberKind::Banned
#[must_use]
pub fn is_present(&self) -> bool {
!(self.is_left() || self.is_banned())
}
@ -246,6 +257,7 @@ impl ChatMemberKind {
impl ChatMemberKind {
/// Getter for [`Administrator::custom_title`] and [`Owner::custom_title`]
/// fields.
#[must_use]
pub fn custom_title(&self) -> Option<&str> {
match &self {
Self::Administrator(Administrator { custom_title, .. })
@ -261,6 +273,7 @@ impl ChatMemberKind {
/// Returns `false` otherwise.
///
/// [`can_manage_chat`]: Administrator::can_manage_chat
#[must_use]
pub fn is_anonymous(&self) -> bool {
match self {
Self::Owner(Owner { is_anonymous, .. })
@ -270,6 +283,7 @@ impl ChatMemberKind {
}
/// Getter for [`Restricted::until_date`] and [`Banned::until_date`] fields.
#[must_use]
pub fn until_date(&self) -> Option<UntilDate> {
match &self {
Self::Owner(_) | Self::Administrator(_) | Self::Member | Self::Left => None,
@ -283,6 +297,7 @@ impl ChatMemberKind {
impl ChatMemberKind {
/// Returns `true` if the user is an administrator in the given chat and the
/// bot is allowed to edit administrator privileges of that user.
#[must_use]
pub fn can_be_edited(&self) -> bool {
match self {
Self::Administrator(Administrator { can_be_edited, .. }) => *can_be_edited,
@ -305,6 +320,7 @@ impl ChatMemberKind {
/// Returns `false` otherwise.
///
/// [`can_manage_chat`]: Administrator::can_manage_chat
#[must_use]
pub fn can_manage_chat(&self) -> bool {
match self {
Self::Owner(_) => true,
@ -325,6 +341,7 @@ impl ChatMemberKind {
/// Returns `false` otherwise.
///
/// [`can_change_info`]: Administrator::can_change_info
#[must_use]
pub fn can_change_info(&self) -> bool {
match self {
Self::Owner(_) => true,
@ -344,6 +361,7 @@ impl ChatMemberKind {
/// Returns `false` otherwise.
///
/// [`can_post_messages`]: Administrator::can_post_messages
#[must_use]
pub fn can_post_messages(&self) -> bool {
match self {
Self::Owner(_) => true,
@ -364,6 +382,7 @@ impl ChatMemberKind {
/// Returns `false` otherwise.
///
/// [`can_edit_messages`]: Administrator::can_edit_messages
#[must_use]
pub fn can_edit_messages(&self) -> bool {
match self {
Self::Owner(_) => true,
@ -383,6 +402,7 @@ impl ChatMemberKind {
/// Returns `false` otherwise.
///
/// [`can_delete_messages`]: Administrator::can_delete_messages
#[must_use]
pub fn can_delete_messages(&self) -> bool {
match self {
Self::Owner(_) => true,
@ -403,6 +423,7 @@ impl ChatMemberKind {
/// Returns `false` otherwise.
///
/// [`can_manage_video_chats`]: Administrator::can_manage_video_chats
#[must_use]
pub fn can_manage_video_chats(&self) -> bool {
match self {
Self::Owner(_) => true,
@ -415,6 +436,7 @@ impl ChatMemberKind {
}
#[deprecated(since = "0.6.0", note = "renamed to `can_manage_video_chats`")]
#[must_use]
pub fn can_manage_voice_chats(&self) -> bool {
self.can_manage_video_chats()
}
@ -428,6 +450,7 @@ impl ChatMemberKind {
/// Returns `false` otherwise.
///
/// [`can_invite_users`]: Administrator::can_invite_users
#[must_use]
pub fn can_invite_users(&self) -> bool {
match &self {
Self::Owner(_) => true,
@ -447,6 +470,7 @@ impl ChatMemberKind {
/// Returns `false` otherwise.
///
/// [`can_restrict_members`]: Administrator::can_restrict_members
#[must_use]
pub fn can_restrict_members(&self) -> bool {
match self {
Self::Owner(_) => true,
@ -467,6 +491,7 @@ impl ChatMemberKind {
/// Returns `false` otherwise.
///
/// [`can_pin_messages`]: Administrator::can_pin_messages
#[must_use]
pub fn can_pin_messages(&self) -> bool {
match self {
Self::Owner(_) => true,
@ -489,6 +514,7 @@ impl ChatMemberKind {
/// Returns `false` otherwise.
///
/// [`can_promote_members`]: Administrator::can_promote_members
#[must_use]
pub fn can_promote_members(&self) -> bool {
match self {
Self::Owner(_) => true,
@ -512,6 +538,7 @@ impl ChatMemberKind {
/// Returns `true` otherwise.
///
/// [`can_send_messages`]: Restricted::can_send_messages
#[must_use]
pub fn can_send_messages(&self) -> bool {
match &self {
Self::Restricted(Restricted {
@ -531,6 +558,7 @@ impl ChatMemberKind {
/// Returns `true` otherwise.
///
/// [`can_send_media_messages`]: Restricted::can_send_media_messages
#[must_use]
pub fn can_send_media_messages(&self) -> bool {
match &self {
Self::Restricted(Restricted {
@ -551,6 +579,7 @@ impl ChatMemberKind {
/// Returns `true` otherwise.
///
/// [`can_send_media_messages`]: Restricted::can_send_media_messages
#[must_use]
pub fn can_send_other_messages(&self) -> bool {
match &self {
Self::Restricted(Restricted {
@ -571,6 +600,7 @@ impl ChatMemberKind {
/// Returns `true` otherwise.
///
/// [`can_send_media_messages`]: Restricted::can_send_media_messages
#[must_use]
pub fn can_add_web_page_previews(&self) -> bool {
match &self {
Self::Restricted(Restricted {
@ -601,6 +631,7 @@ impl ChatMemberStatus {
/// Returns `true` if the user is the [owner] of the given chat.
///
/// [owner]: ChatMemberKind::Owner
#[must_use]
pub fn is_owner(&self) -> bool {
matches!(self, Self::Owner { .. })
}
@ -613,6 +644,7 @@ impl ChatMemberStatus {
/// owner of the given chat. See also: [`is_privileged`].
///
/// [`is_privileged`]: ChatMemberKind::is_privileged
#[must_use]
pub fn is_administrator(&self) -> bool {
matches!(self, Self::Administrator { .. })
}
@ -620,6 +652,7 @@ impl ChatMemberStatus {
/// Returns `true` if the user is a common [member] of the given chat.
///
/// [member]: ChatMemberKind::Member
#[must_use]
pub fn is_member(&self) -> bool {
matches!(self, Self::Member { .. })
}
@ -627,6 +660,7 @@ impl ChatMemberStatus {
/// Returns `true` if the user is [restricted] in the given chat.
///
/// [restricted]: ChatMemberKind::Restricted
#[must_use]
pub fn is_restricted(&self) -> bool {
matches!(self, Self::Restricted { .. })
}
@ -634,6 +668,7 @@ impl ChatMemberStatus {
/// Returns `true` if the user [left] the given chat.
///
/// [left]: ChatMemberKind::Left
#[must_use]
pub fn is_left(&self) -> bool {
matches!(self, Self::Left { .. })
}
@ -641,6 +676,7 @@ impl ChatMemberStatus {
/// Returns `true` if the user is [banned] in the given chat.
///
/// [banned]: ChatMemberKind::Banned
#[must_use]
pub fn is_banned(&self) -> bool {
matches!(self, Self::Banned { .. })
}
@ -653,6 +689,7 @@ impl ChatMemberStatus {
///
/// [owner]: ChatMemberKind::Owner
/// [administrator]: ChatMemberKind::Administrator
#[must_use]
pub fn is_privileged(&self) -> bool {
self.is_administrator() || self.is_owner()
}
@ -662,6 +699,7 @@ impl ChatMemberStatus {
///
/// [left]: ChatMemberKind::Left
/// [banned]: ChatMemberKind::Banned
#[must_use]
pub fn is_present(&self) -> bool {
!(self.is_left() || self.is_banned())
}

View file

@ -33,6 +33,7 @@ pub struct ForceReply {
}
impl ForceReply {
#[must_use]
pub const fn new() -> Self {
Self {
force_reply: True,
@ -49,6 +50,7 @@ impl ForceReply {
self
}
#[must_use]
pub const fn selective(mut self, val: bool) -> Self {
self.selective = Some(val);
self

View file

@ -218,6 +218,7 @@ impl InlineKeyboardButton {
since = "0.7.0",
note = "set correct kind in the constructor or access the field directly"
)]
#[must_use]
pub fn kind(mut self, val: InlineKeyboardButtonKind) -> Self {
self.kind = val;
self

View file

@ -66,6 +66,7 @@ impl InlineKeyboardMarkup {
self
}
#[must_use]
pub fn append_to_row(mut self, index: usize, button: InlineKeyboardButton) -> Self {
match self.inline_keyboard.get_mut(index) {
Some(buttons) => buttons.push(button),

View file

@ -60,11 +60,13 @@ impl InlineQuery {
self
}
#[must_use]
pub fn from(mut self, val: User) -> Self {
self.from = val;
self
}
#[must_use]
pub fn location(mut self, val: Location) -> Self {
self.location = Some(val);
self

View file

@ -76,21 +76,25 @@ impl InlineQueryResultArticle {
self
}
#[must_use]
pub fn input_message_content(mut self, val: InputMessageContent) -> Self {
self.input_message_content = val;
self
}
#[must_use]
pub fn reply_markup(mut self, val: InlineKeyboardMarkup) -> Self {
self.reply_markup = Some(val);
self
}
#[must_use]
pub fn url(mut self, val: reqwest::Url) -> Self {
self.url = Some(val);
self
}
#[must_use]
pub fn hide_url(mut self, val: bool) -> Self {
self.hide_url = Some(val);
self
@ -104,16 +108,19 @@ impl InlineQueryResultArticle {
self
}
#[must_use]
pub fn thumb_url(mut self, val: reqwest::Url) -> Self {
self.thumb_url = Some(val);
self
}
#[must_use]
pub fn thumb_width(mut self, val: i32) -> Self {
self.thumb_width = Some(val);
self
}
#[must_use]
pub fn thumb_height(mut self, val: i32) -> Self {
self.thumb_height = Some(val);
self

View file

@ -79,6 +79,7 @@ impl InlineQueryResultAudio {
self
}
#[must_use]
pub fn audio_url(mut self, val: reqwest::Url) -> Self {
self.audio_url = val;
self
@ -100,6 +101,7 @@ impl InlineQueryResultAudio {
self
}
#[must_use]
pub fn parse_mode(mut self, val: ParseMode) -> Self {
self.parse_mode = Some(val);
self
@ -129,11 +131,13 @@ impl InlineQueryResultAudio {
self
}
#[must_use]
pub fn reply_markup(mut self, val: InlineKeyboardMarkup) -> Self {
self.reply_markup = Some(val);
self
}
#[must_use]
pub fn input_message_content(mut self, val: InputMessageContent) -> Self {
self.input_message_content = Some(val);
self

View file

@ -83,6 +83,7 @@ impl InlineQueryResultCachedAudio {
self
}
#[must_use]
pub fn parse_mode(mut self, val: ParseMode) -> Self {
self.parse_mode = Some(val);
self
@ -96,11 +97,13 @@ impl InlineQueryResultCachedAudio {
self
}
#[must_use]
pub fn reply_markup(mut self, val: InlineKeyboardMarkup) -> Self {
self.reply_markup = Some(val);
self
}
#[must_use]
pub fn input_message_content(mut self, val: InputMessageContent) -> Self {
self.input_message_content = Some(val);
self

View file

@ -108,6 +108,7 @@ impl InlineQueryResultCachedDocument {
self
}
#[must_use]
pub fn parse_mode(mut self, val: ParseMode) -> Self {
self.parse_mode = Some(val);
self
@ -121,11 +122,13 @@ impl InlineQueryResultCachedDocument {
self
}
#[must_use]
pub fn reply_markup(mut self, val: InlineKeyboardMarkup) -> Self {
self.reply_markup = Some(val);
self
}
#[must_use]
pub fn input_message_content(mut self, val: InputMessageContent) -> Self {
self.input_message_content = Some(val);
self

View file

@ -96,6 +96,7 @@ impl InlineQueryResultCachedGif {
self
}
#[must_use]
pub fn parse_mode(mut self, val: ParseMode) -> Self {
self.parse_mode = Some(val);
self
@ -109,11 +110,13 @@ impl InlineQueryResultCachedGif {
self
}
#[must_use]
pub fn reply_markup(mut self, val: InlineKeyboardMarkup) -> Self {
self.reply_markup = Some(val);
self
}
#[must_use]
pub fn input_message_content(mut self, val: InputMessageContent) -> Self {
self.input_message_content = Some(val);
self

View file

@ -88,6 +88,7 @@ impl InlineQueryResultCachedMpeg4Gif {
self
}
#[must_use]
pub fn parse_mode(mut self, val: ParseMode) -> Self {
self.parse_mode = Some(val);
self
@ -101,11 +102,13 @@ impl InlineQueryResultCachedMpeg4Gif {
self
}
#[must_use]
pub fn reply_markup(mut self, val: InlineKeyboardMarkup) -> Self {
self.reply_markup = Some(val);
self
}
#[must_use]
pub fn input_message_content(mut self, val: InputMessageContent) -> Self {
self.input_message_content = Some(val);
self

View file

@ -107,6 +107,7 @@ impl InlineQueryResultCachedPhoto {
self
}
#[must_use]
pub fn parse_mode(mut self, val: ParseMode) -> Self {
self.parse_mode = Some(val);
self
@ -120,11 +121,13 @@ impl InlineQueryResultCachedPhoto {
self
}
#[must_use]
pub fn reply_markup(mut self, val: InlineKeyboardMarkup) -> Self {
self.reply_markup = Some(val);
self
}
#[must_use]
pub fn input_message_content(mut self, val: InputMessageContent) -> Self {
self.input_message_content = Some(val);
self

View file

@ -57,11 +57,13 @@ impl InlineQueryResultCachedSticker {
self
}
#[must_use]
pub fn reply_markup(mut self, val: InlineKeyboardMarkup) -> Self {
self.reply_markup = Some(val);
self
}
#[must_use]
pub fn input_message_content(mut self, val: InputMessageContent) -> Self {
self.input_message_content = Some(val);
self

View file

@ -108,6 +108,7 @@ impl InlineQueryResultCachedVideo {
self
}
#[must_use]
pub fn parse_mode(mut self, val: ParseMode) -> Self {
self.parse_mode = Some(val);
self
@ -121,11 +122,13 @@ impl InlineQueryResultCachedVideo {
self
}
#[must_use]
pub fn reply_markup(mut self, val: InlineKeyboardMarkup) -> Self {
self.reply_markup = Some(val);
self
}
#[must_use]
pub fn input_message_content(mut self, val: InputMessageContent) -> Self {
self.input_message_content = Some(val);
self

View file

@ -96,6 +96,7 @@ impl InlineQueryResultCachedVoice {
self
}
#[must_use]
pub fn parse_mode(mut self, val: ParseMode) -> Self {
self.parse_mode = Some(val);
self
@ -109,11 +110,13 @@ impl InlineQueryResultCachedVoice {
self
}
#[must_use]
pub fn reply_markup(mut self, val: InlineKeyboardMarkup) -> Self {
self.reply_markup = Some(val);
self
}
#[must_use]
pub fn input_message_content(mut self, val: InputMessageContent) -> Self {
self.input_message_content = Some(val);
self

View file

@ -109,26 +109,31 @@ impl InlineQueryResultContact {
self
}
#[must_use]
pub fn reply_markup(mut self, val: InlineKeyboardMarkup) -> Self {
self.reply_markup = Some(val);
self
}
#[must_use]
pub fn input_message_content(mut self, val: InputMessageContent) -> Self {
self.input_message_content = Some(val);
self
}
#[must_use]
pub fn thumb_url(mut self, val: reqwest::Url) -> Self {
self.thumb_url = Some(val);
self
}
#[must_use]
pub fn thumb_width(mut self, val: i32) -> Self {
self.thumb_width = Some(val);
self
}
#[must_use]
pub fn thumb_height(mut self, val: i32) -> Self {
self.thumb_height = Some(val);
self

View file

@ -87,6 +87,7 @@ impl InlineQueryResultDocument {
self
}
#[must_use]
pub fn parse_mode(mut self, val: ParseMode) -> Self {
self.parse_mode = Some(val);
self
@ -100,11 +101,13 @@ impl InlineQueryResultDocument {
self
}
#[must_use]
pub fn document_url(mut self, val: reqwest::Url) -> Self {
self.document_url = val;
self
}
#[must_use]
pub fn mime_type(mut self, val: Mime) -> Self {
self.mime_type = val;
self
@ -118,26 +121,31 @@ impl InlineQueryResultDocument {
self
}
#[must_use]
pub fn reply_markup(mut self, val: InlineKeyboardMarkup) -> Self {
self.reply_markup = Some(val);
self
}
#[must_use]
pub fn input_message_content(mut self, val: InputMessageContent) -> Self {
self.input_message_content = Some(val);
self
}
#[must_use]
pub fn thumb_url(mut self, val: reqwest::Url) -> Self {
self.thumb_url = Some(val);
self
}
#[must_use]
pub fn thumb_width(mut self, val: i32) -> Self {
self.thumb_width = Some(val);
self
}
#[must_use]
pub fn thumb_height(mut self, val: i32) -> Self {
self.thumb_height = Some(val);
self

View file

@ -51,6 +51,7 @@ impl InlineQueryResultGame {
self
}
#[must_use]
pub fn reply_markup(mut self, val: InlineKeyboardMarkup) -> Self {
self.reply_markup = Some(val);
self

View file

@ -86,26 +86,31 @@ impl InlineQueryResultGif {
self
}
#[must_use]
pub fn gif_url(mut self, val: reqwest::Url) -> Self {
self.gif_url = val;
self
}
#[must_use]
pub fn gif_width(mut self, val: i32) -> Self {
self.gif_width = Some(val);
self
}
#[must_use]
pub fn gif_height(mut self, val: i32) -> Self {
self.gif_height = Some(val);
self
}
#[must_use]
pub fn gif_duration(mut self, val: i32) -> Self {
self.gif_duration = Some(val);
self
}
#[must_use]
pub fn thumb_url(mut self, val: reqwest::Url) -> Self {
self.thumb_url = val;
self
@ -127,6 +132,7 @@ impl InlineQueryResultGif {
self
}
#[must_use]
pub fn parse_mode(mut self, val: ParseMode) -> Self {
self.parse_mode = Some(val);
self
@ -140,11 +146,13 @@ impl InlineQueryResultGif {
self
}
#[must_use]
pub fn reply_markup(mut self, val: InlineKeyboardMarkup) -> Self {
self.reply_markup = Some(val);
self
}
#[must_use]
pub fn input_message_content(mut self, val: InputMessageContent) -> Self {
self.input_message_content = Some(val);
self

View file

@ -89,11 +89,13 @@ impl InlineQueryResultLocation {
self
}
#[must_use]
pub fn latitude(mut self, val: f64) -> Self {
self.latitude = val;
self
}
#[must_use]
pub fn longitude(mut self, val: f64) -> Self {
self.longitude = val;
self
@ -107,46 +109,55 @@ impl InlineQueryResultLocation {
self
}
#[must_use]
pub fn horizontal_accuracy<S>(mut self, val: f64) -> Self {
self.horizontal_accuracy = Some(val);
self
}
#[must_use]
pub fn live_period(mut self, val: i32) -> Self {
self.live_period = Some(val);
self
}
#[must_use]
pub fn heading(mut self, val: u16) -> Self {
self.heading = Some(val);
self
}
#[must_use]
pub fn proximity_alert_radius(mut self, val: u32) -> Self {
self.proximity_alert_radius = Some(val);
self
}
#[must_use]
pub fn reply_markup(mut self, val: InlineKeyboardMarkup) -> Self {
self.reply_markup = Some(val);
self
}
#[must_use]
pub fn input_message_content(mut self, val: InputMessageContent) -> Self {
self.input_message_content = Some(val);
self
}
#[must_use]
pub fn thumb_url(mut self, val: reqwest::Url) -> Self {
self.thumb_url = Some(val);
self
}
#[must_use]
pub fn thumb_width(mut self, val: u32) -> Self {
self.thumb_width = Some(val);
self
}
#[must_use]
pub fn thumb_height(mut self, val: u32) -> Self {
self.thumb_height = Some(val);
self

View file

@ -87,26 +87,31 @@ impl InlineQueryResultMpeg4Gif {
self
}
#[must_use]
pub fn mpeg4_url(mut self, val: reqwest::Url) -> Self {
self.mpeg4_url = val;
self
}
#[must_use]
pub fn mpeg4_width(mut self, val: i32) -> Self {
self.mpeg4_width = Some(val);
self
}
#[must_use]
pub fn mpeg4_height(mut self, val: i32) -> Self {
self.mpeg4_height = Some(val);
self
}
#[must_use]
pub fn mpeg4_duration(mut self, val: i32) -> Self {
self.mpeg4_duration = Some(val);
self
}
#[must_use]
pub fn thumb_url(mut self, val: reqwest::Url) -> Self {
self.thumb_url = val;
self
@ -128,6 +133,7 @@ impl InlineQueryResultMpeg4Gif {
self
}
#[must_use]
pub fn parse_mode(mut self, val: ParseMode) -> Self {
self.parse_mode = Some(val);
self
@ -141,11 +147,13 @@ impl InlineQueryResultMpeg4Gif {
self
}
#[must_use]
pub fn reply_markup(mut self, val: InlineKeyboardMarkup) -> Self {
self.reply_markup = Some(val);
self
}
#[must_use]
pub fn input_message_content(mut self, val: InputMessageContent) -> Self {
self.input_message_content = Some(val);
self

View file

@ -87,21 +87,25 @@ impl InlineQueryResultPhoto {
self
}
#[must_use]
pub fn photo_url(mut self, val: reqwest::Url) -> Self {
self.photo_url = val;
self
}
#[must_use]
pub fn thumb_url<S>(mut self, val: reqwest::Url) -> Self {
self.thumb_url = val;
self
}
#[must_use]
pub fn photo_width(mut self, val: i32) -> Self {
self.photo_width = Some(val);
self
}
#[must_use]
pub fn photo_height(mut self, val: i32) -> Self {
self.photo_height = Some(val);
self
@ -131,6 +135,7 @@ impl InlineQueryResultPhoto {
self
}
#[must_use]
pub fn parse_mode(mut self, val: ParseMode) -> Self {
self.parse_mode = Some(val);
self
@ -144,11 +149,13 @@ impl InlineQueryResultPhoto {
self
}
#[must_use]
pub fn reply_markup(mut self, val: InlineKeyboardMarkup) -> Self {
self.reply_markup = Some(val);
self
}
#[must_use]
pub fn input_message_content(mut self, val: InputMessageContent) -> Self {
self.input_message_content = Some(val);
self

View file

@ -94,11 +94,13 @@ impl InlineQueryResultVenue {
self
}
#[must_use]
pub fn latitude(mut self, val: f64) -> Self {
self.latitude = val;
self
}
#[must_use]
pub fn longitude(mut self, val: f64) -> Self {
self.longitude = val;
self
@ -152,26 +154,31 @@ impl InlineQueryResultVenue {
self
}
#[must_use]
pub fn reply_markup(mut self, val: InlineKeyboardMarkup) -> Self {
self.reply_markup = Some(val);
self
}
#[must_use]
pub fn input_message_content(mut self, val: InputMessageContent) -> Self {
self.input_message_content = Some(val);
self
}
#[must_use]
pub fn thumb_url(mut self, val: reqwest::Url) -> Self {
self.thumb_url = Some(val);
self
}
#[must_use]
pub fn thumb_width(mut self, val: i32) -> Self {
self.thumb_width = Some(val);
self
}
#[must_use]
pub fn thumb_height(mut self, val: i32) -> Self {
self.thumb_height = Some(val);
self

View file

@ -109,16 +109,19 @@ impl InlineQueryResultVideo {
self
}
#[must_use]
pub fn video_url(mut self, val: reqwest::Url) -> Self {
self.video_url = val;
self
}
#[must_use]
pub fn mime_type(mut self, val: Mime) -> Self {
self.mime_type = val;
self
}
#[must_use]
pub fn thumb_url(mut self, val: reqwest::Url) -> Self {
self.thumb_url = val;
self
@ -140,6 +143,7 @@ impl InlineQueryResultVideo {
self
}
#[must_use]
pub fn parse_mode(mut self, val: ParseMode) -> Self {
self.parse_mode = Some(val);
self
@ -153,16 +157,19 @@ impl InlineQueryResultVideo {
self
}
#[must_use]
pub fn video_width(mut self, val: i32) -> Self {
self.video_width = Some(val);
self
}
#[must_use]
pub fn video_height(mut self, val: i32) -> Self {
self.video_height = Some(val);
self
}
#[must_use]
pub fn video_duration(mut self, val: i32) -> Self {
self.video_duration = Some(val);
self
@ -176,11 +183,13 @@ impl InlineQueryResultVideo {
self
}
#[must_use]
pub fn reply_markup(mut self, val: InlineKeyboardMarkup) -> Self {
self.reply_markup = Some(val);
self
}
#[must_use]
pub fn input_message_content(mut self, val: InputMessageContent) -> Self {
self.input_message_content = Some(val);
self

View file

@ -76,6 +76,7 @@ impl InlineQueryResultVoice {
self
}
#[must_use]
pub fn voice_url(mut self, val: reqwest::Url) -> Self {
self.voice_url = val;
self
@ -97,6 +98,7 @@ impl InlineQueryResultVoice {
self
}
#[must_use]
pub fn parse_mode(mut self, val: ParseMode) -> Self {
self.parse_mode = Some(val);
self
@ -110,16 +112,19 @@ impl InlineQueryResultVoice {
self
}
#[must_use]
pub fn voice_duration(mut self, value: i32) -> Self {
self.voice_duration = Some(value);
self
}
#[must_use]
pub fn reply_markup(mut self, val: InlineKeyboardMarkup) -> Self {
self.reply_markup = Some(val);
self
}
#[must_use]
pub fn input_message_content(mut self, val: InputMessageContent) -> Self {
self.input_message_content = Some(val);
self

View file

@ -64,6 +64,7 @@ impl InputFile {
/// [`SendAudio`]: crate::payloads::SendAudio
/// [`SendDocument`]: crate::payloads::SendDocument
/// [`SendVoice`]: crate::payloads::SendVoice
#[must_use]
pub fn url(url: url::Url) -> Self {
Self::new(Url(url))
}

View file

@ -61,6 +61,7 @@ impl InputMessageContentText {
self
}
#[must_use]
pub fn parse_mode(mut self, val: ParseMode) -> Self {
self.parse_mode = Some(val);
self
@ -74,6 +75,7 @@ impl InputMessageContentText {
self
}
#[must_use]
pub fn disable_web_page_preview(mut self, val: bool) -> Self {
self.disable_web_page_preview = Some(val);
self
@ -109,6 +111,7 @@ pub struct InputMessageContentLocation {
}
impl InputMessageContentLocation {
#[must_use]
pub const fn new(latitude: f64, longitude: f64) -> Self {
Self {
latitude,
@ -120,16 +123,19 @@ impl InputMessageContentLocation {
}
}
#[must_use]
pub const fn latitude(mut self, val: f64) -> Self {
self.latitude = val;
self
}
#[must_use]
pub const fn longitude(mut self, val: f64) -> Self {
self.longitude = val;
self
}
#[must_use]
pub const fn live_period(mut self, val: u32) -> Self {
self.live_period = Some(val);
self
@ -188,11 +194,13 @@ impl InputMessageContentVenue {
}
}
#[must_use]
pub fn latitude(mut self, val: f64) -> Self {
self.latitude = val;
self
}
#[must_use]
pub fn longitude(mut self, val: f64) -> Self {
self.longitude = val;
self
@ -465,6 +473,7 @@ impl InputMessageContentInvoice {
self
}
#[must_use]
pub fn currency(mut self, val: Currency) -> Self {
self.currency = val;
self
@ -478,6 +487,7 @@ impl InputMessageContentInvoice {
self
}
#[must_use]
pub fn max_tip_amount(mut self, val: u32) -> Self {
self.max_tip_amount = Some(val);
self
@ -499,57 +509,68 @@ impl InputMessageContentInvoice {
self
}
#[must_use]
pub fn photo_url(mut self, val: Url) -> Self {
self.photo_url = Some(val);
self
}
#[must_use]
pub fn photo_size(mut self, val: u32) -> Self {
self.photo_size = Some(val);
self
}
#[must_use]
pub fn photo_width(mut self, val: u32) -> Self {
self.photo_width = Some(val);
self
}
#[must_use]
pub fn photo_height(mut self, val: u32) -> Self {
self.photo_height = Some(val);
self
}
#[must_use]
pub fn need_name(mut self, val: bool) -> Self {
self.need_name = Some(val);
self
}
#[must_use]
pub fn need_phone_number(mut self, val: bool) -> Self {
self.need_phone_number = Some(val);
self
}
#[must_use]
pub fn need_email(mut self, val: bool) -> Self {
self.need_email = Some(val);
self
}
#[must_use]
pub fn need_shipping_address(mut self, val: bool) -> Self {
self.need_shipping_address = Some(val);
self
}
#[must_use]
pub fn send_phone_number_to_provider(mut self, val: bool) -> Self {
self.send_phone_number_to_provider = Some(val);
self
}
#[must_use]
pub fn send_email_to_provider(mut self, val: bool) -> Self {
self.send_email_to_provider = Some(val);
self
}
#[allow(clippy::wrong_self_convention)]
#[must_use]
pub fn is_flexible(mut self, val: bool) -> Self {
self.is_flexible = Some(val);
self

View file

@ -38,6 +38,7 @@ impl LabeledPrice {
self
}
#[must_use]
pub fn amount(mut self, val: i32) -> Self {
self.amount = val;
self

View file

@ -46,6 +46,7 @@ pub struct LoginUrl {
}
impl LoginUrl {
#[must_use]
pub fn url(mut self, val: reqwest::Url) -> Self {
self.url = val;
self
@ -67,6 +68,7 @@ impl LoginUrl {
self
}
#[must_use]
pub fn request_write_access(mut self, val: bool) -> Self {
self.request_write_access = Some(val);
self

View file

@ -45,16 +45,19 @@ impl MaskPosition {
self
}
#[must_use]
pub fn x_shift(mut self, val: f64) -> Self {
self.x_shift = val;
self
}
#[must_use]
pub fn y_shift(mut self, val: f64) -> Self {
self.y_shift = val;
self
}
#[must_use]
pub fn scale(mut self, val: f64) -> Self {
self.scale = val;
self

View file

@ -26,6 +26,7 @@ pub struct Me {
impl Me {
/// Returns the username of the bot.
#[must_use]
pub fn username(&self) -> &str {
self.user
.username
@ -34,11 +35,13 @@ impl Me {
}
/// Returns a username mention of this bot.
#[must_use]
pub fn mention(&self) -> String {
format!("@{}", self.username())
}
/// Returns an URL that links to this bot in the form of `t.me/<...>`.
#[must_use]
pub fn tme_url(&self) -> reqwest::Url {
format!("https://t.me/{}", self.username()).parse().unwrap()
}

View file

@ -544,6 +544,7 @@ mod getters {
/// [Message]: crate::types::Message
/// [telegram docs]: https://core.telegram.org/bots/api#message
impl Message {
#[must_use]
pub fn from(&self) -> Option<&User> {
match &self.kind {
Common(MessageCommon { from, .. }) => from.as_ref(),
@ -551,6 +552,7 @@ mod getters {
}
}
#[must_use]
pub fn author_signature(&self) -> Option<&str> {
match &self.kind {
Common(MessageCommon {
@ -560,6 +562,7 @@ mod getters {
}
}
#[must_use]
pub fn sender_chat(&self) -> Option<&Chat> {
match &self.kind {
Common(MessageCommon { sender_chat, .. }) => sender_chat.as_ref(),
@ -568,22 +571,27 @@ mod getters {
}
#[deprecated(since = "0.4.2", note = "use `.chat.id` field instead")]
#[must_use]
pub fn chat_id(&self) -> ChatId {
self.chat.id
}
#[must_use]
pub fn forward(&self) -> Option<&Forward> {
self.common().and_then(|m| m.forward.as_ref())
}
#[must_use]
pub fn forward_date(&self) -> Option<DateTime<Utc>> {
self.forward().map(|f| f.date)
}
#[must_use]
pub fn forward_from(&self) -> Option<&ForwardedFrom> {
self.forward().map(|f| &f.from)
}
#[must_use]
pub fn forward_from_user(&self) -> Option<&User> {
self.forward_from().and_then(|from| match from {
ForwardedFrom::User(user) => Some(user),
@ -591,6 +599,7 @@ mod getters {
})
}
#[must_use]
pub fn forward_from_chat(&self) -> Option<&Chat> {
self.forward_from().and_then(|from| match from {
ForwardedFrom::Chat(chat) => Some(chat),
@ -598,6 +607,7 @@ mod getters {
})
}
#[must_use]
pub fn forward_from_sender_name(&self) -> Option<&str> {
self.forward_from().and_then(|from| match from {
ForwardedFrom::SenderName(sender_name) => Some(&**sender_name),
@ -605,18 +615,22 @@ mod getters {
})
}
#[must_use]
pub fn forward_from_message_id(&self) -> Option<i32> {
self.forward().and_then(|f| f.message_id)
}
#[must_use]
pub fn forward_signature(&self) -> Option<&str> {
self.forward().and_then(|f| f.signature.as_deref())
}
#[must_use]
pub fn reply_to_message(&self) -> Option<&Message> {
self.common().and_then(|m| m.reply_to_message.as_deref())
}
#[must_use]
pub fn edit_date(&self) -> Option<&DateTime<Utc>> {
match &self.kind {
Common(MessageCommon { edit_date, .. }) => edit_date.as_ref(),
@ -624,6 +638,7 @@ mod getters {
}
}
#[must_use]
pub fn media_group_id(&self) -> Option<&str> {
match &self.kind {
Common(MessageCommon {
@ -646,6 +661,7 @@ mod getters {
}
}
#[must_use]
pub fn text(&self) -> Option<&str> {
match &self.kind {
Common(MessageCommon {
@ -668,6 +684,7 @@ mod getters {
///
/// [`parse_entities`]: Message::parse_entities
/// [`caption_entities`]: Message::caption_entities
#[must_use]
pub fn entities(&self) -> Option<&[MessageEntity]> {
match &self.kind {
Common(MessageCommon {
@ -690,6 +707,7 @@ mod getters {
///
/// [`parse_caption_entities`]: Message::parse_caption_entities
/// [`entities`]: Message::entities
#[must_use]
pub fn caption_entities(&self) -> Option<&[MessageEntity]> {
match &self.kind {
Common(MessageCommon {
@ -738,6 +756,7 @@ mod getters {
}
}
#[must_use]
pub fn audio(&self) -> Option<&types::Audio> {
match &self.kind {
Common(MessageCommon {
@ -748,6 +767,7 @@ mod getters {
}
}
#[must_use]
pub fn document(&self) -> Option<&types::Document> {
match &self.kind {
Common(MessageCommon {
@ -758,6 +778,7 @@ mod getters {
}
}
#[must_use]
pub fn animation(&self) -> Option<&types::Animation> {
match &self.kind {
Common(MessageCommon {
@ -768,6 +789,7 @@ mod getters {
}
}
#[must_use]
pub fn game(&self) -> Option<&types::Game> {
match &self.kind {
Common(MessageCommon {
@ -778,6 +800,7 @@ mod getters {
}
}
#[must_use]
pub fn photo(&self) -> Option<&[PhotoSize]> {
match &self.kind {
Common(MessageCommon {
@ -788,6 +811,7 @@ mod getters {
}
}
#[must_use]
pub fn sticker(&self) -> Option<&types::Sticker> {
match &self.kind {
Common(MessageCommon {
@ -798,6 +822,7 @@ mod getters {
}
}
#[must_use]
pub fn video(&self) -> Option<&types::Video> {
match &self.kind {
Common(MessageCommon {
@ -808,6 +833,7 @@ mod getters {
}
}
#[must_use]
pub fn voice(&self) -> Option<&types::Voice> {
match &self.kind {
Common(MessageCommon {
@ -818,6 +844,7 @@ mod getters {
}
}
#[must_use]
pub fn video_note(&self) -> Option<&types::VideoNote> {
match &self.kind {
Common(MessageCommon {
@ -828,6 +855,7 @@ mod getters {
}
}
#[must_use]
pub fn caption(&self) -> Option<&str> {
match &self.kind {
Common(MessageCommon { media_kind, .. }) => match media_kind {
@ -845,6 +873,7 @@ mod getters {
}
}
#[must_use]
pub fn contact(&self) -> Option<&types::Contact> {
match &self.kind {
Common(MessageCommon {
@ -855,6 +884,7 @@ mod getters {
}
}
#[must_use]
pub fn location(&self) -> Option<&types::Location> {
match &self.kind {
Common(MessageCommon {
@ -865,6 +895,7 @@ mod getters {
}
}
#[must_use]
pub fn venue(&self) -> Option<&types::Venue> {
match &self.kind {
Common(MessageCommon {
@ -875,6 +906,7 @@ mod getters {
}
}
#[must_use]
pub fn poll(&self) -> Option<&types::Poll> {
match &self.kind {
Common(MessageCommon {
@ -885,6 +917,7 @@ mod getters {
}
}
#[must_use]
pub fn new_chat_members(&self) -> Option<&[User]> {
match &self.kind {
NewChatMembers(MessageNewChatMembers { new_chat_members }) => {
@ -894,6 +927,7 @@ mod getters {
}
}
#[must_use]
pub fn left_chat_member(&self) -> Option<&User> {
match &self.kind {
LeftChatMember(MessageLeftChatMember { left_chat_member }) => {
@ -903,6 +937,7 @@ mod getters {
}
}
#[must_use]
pub fn new_chat_title(&self) -> Option<&str> {
match &self.kind {
NewChatTitle(MessageNewChatTitle { new_chat_title }) => Some(new_chat_title),
@ -910,6 +945,7 @@ mod getters {
}
}
#[must_use]
pub fn new_chat_photo(&self) -> Option<&[PhotoSize]> {
match &self.kind {
NewChatPhoto(MessageNewChatPhoto { new_chat_photo }) => Some(new_chat_photo),
@ -919,6 +955,7 @@ mod getters {
// TODO: OK, `Option<True>` is weird, can we do something with it?
// mb smt like `is_delete_chat_photo(&self) -> bool`?
#[must_use]
pub fn delete_chat_photo(&self) -> Option<True> {
match &self.kind {
DeleteChatPhoto(MessageDeleteChatPhoto { delete_chat_photo }) => {
@ -928,6 +965,7 @@ mod getters {
}
}
#[must_use]
pub fn group_chat_created(&self) -> Option<True> {
match &self.kind {
GroupChatCreated(MessageGroupChatCreated { group_chat_created }) => {
@ -937,6 +975,7 @@ mod getters {
}
}
#[must_use]
pub fn super_group_chat_created(&self) -> Option<True> {
match &self.kind {
SupergroupChatCreated(MessageSupergroupChatCreated {
@ -946,6 +985,7 @@ mod getters {
}
}
#[must_use]
pub fn channel_chat_created(&self) -> Option<True> {
match &self.kind {
ChannelChatCreated(MessageChannelChatCreated {
@ -955,6 +995,7 @@ mod getters {
}
}
#[must_use]
pub fn chat_migration(&self) -> Option<ChatMigration> {
match &self.kind {
Common(MessageCommon {
@ -965,6 +1006,7 @@ mod getters {
}
}
#[must_use]
pub fn migrate_to_chat_id(&self) -> Option<ChatId> {
match &self.kind {
Common(MessageCommon {
@ -975,6 +1017,7 @@ mod getters {
}
}
#[must_use]
pub fn migrate_from_chat_id(&self) -> Option<ChatId> {
match &self.kind {
Common(MessageCommon {
@ -985,6 +1028,7 @@ mod getters {
}
}
#[must_use]
pub fn pinned_message(&self) -> Option<&Message> {
match &self.kind {
Pinned(MessagePinned { pinned }) => Some(pinned),
@ -992,6 +1036,7 @@ mod getters {
}
}
#[must_use]
pub fn invoice(&self) -> Option<&types::Invoice> {
match &self.kind {
Invoice(MessageInvoice { invoice }) => Some(invoice),
@ -999,6 +1044,7 @@ mod getters {
}
}
#[must_use]
pub fn successful_payment(&self) -> Option<&types::SuccessfulPayment> {
match &self.kind {
SuccessfulPayment(MessageSuccessfulPayment { successful_payment }) => {
@ -1008,6 +1054,7 @@ mod getters {
}
}
#[must_use]
pub fn connected_website(&self) -> Option<&str> {
match &self.kind {
ConnectedWebsite(MessageConnectedWebsite { connected_website }) => {
@ -1017,6 +1064,7 @@ mod getters {
}
}
#[must_use]
pub fn passport_data(&self) -> Option<&types::PassportData> {
match &self.kind {
PassportData(MessagePassportData { passport_data }) => Some(passport_data),
@ -1024,6 +1072,7 @@ mod getters {
}
}
#[must_use]
pub fn dice(&self) -> Option<&types::Dice> {
match &self.kind {
Dice(MessageDice { dice }) => Some(dice),
@ -1031,6 +1080,7 @@ mod getters {
}
}
#[must_use]
pub fn proximity_alert_triggered(&self) -> Option<&types::ProximityAlertTriggered> {
match &self.kind {
ProximityAlertTriggered(MessageProximityAlertTriggered {
@ -1040,6 +1090,7 @@ mod getters {
}
}
#[must_use]
pub fn reply_markup(&self) -> Option<&types::InlineKeyboardMarkup> {
match &self.kind {
Common(MessageCommon { reply_markup, .. }) => reply_markup.as_ref(),
@ -1047,6 +1098,7 @@ mod getters {
}
}
#[must_use]
pub fn is_automatic_forward(&self) -> bool {
match &self.kind {
Common(MessageCommon {
@ -1057,6 +1109,7 @@ mod getters {
}
}
#[must_use]
pub fn has_protected_content(&self) -> bool {
match &self.kind {
Common(MessageCommon {
@ -1085,6 +1138,7 @@ impl Message {
///
/// Returns `None` for private chats (i.e.: DMs) and private groups (not
/// supergroups).
#[must_use]
pub fn url(&self) -> Option<Url> {
Self::url_of(self.chat.id, self.chat.username(), self.id)
}
@ -1103,6 +1157,7 @@ impl Message {
///
/// [`url`]: Message::url
#[track_caller]
#[must_use]
pub fn url_of(chat_id: ChatId, chat_username: Option<&str>, message_id: i32) -> Option<Url> {
use BareChatId::*;
@ -1146,6 +1201,7 @@ impl Message {
///
/// Returns `None` for private chats (i.e.: DMs) and private groups (not
/// supergroups).
#[must_use]
pub fn comment_url(&self, comment_id: i32) -> Option<Url> {
Self::comment_url_of(self.chat.id, self.chat.username(), self.id, comment_id)
}
@ -1164,6 +1220,7 @@ impl Message {
/// supergroups).
///
/// [`comment_url`]: Message::comment_url
#[must_use]
pub fn comment_url_of(
channel_id: ChatId,
channel_username: Option<&str>,
@ -1187,6 +1244,7 @@ impl Message {
///
/// Returns `None` for private chats (i.e.: DMs) and private groups (not
/// supergroups).
#[must_use]
pub fn url_in_thread(&self, thread_starter_msg_id: i32) -> Option<Url> {
Self::url_in_thread_of(
self.chat.id,
@ -1214,6 +1272,7 @@ impl Message {
/// supergroups).
///
/// [`url_in_thread`]: Message::url_in_thread
#[must_use]
pub fn url_in_thread_of(
chat_id: ChatId,
chat_username: Option<&str>,
@ -1235,6 +1294,7 @@ impl Message {
/// See also: [`parse_caption_entities`].
///
/// [`parse_caption_entities`]: Message::parse_caption_entities
#[must_use]
pub fn parse_entities(&self) -> Option<Vec<MessageEntityRef<'_>>> {
self.text()
.zip(self.entities())
@ -1249,6 +1309,7 @@ impl Message {
/// See also: [`parse_entities`].
///
/// [`parse_entities`]: Message::parse_entities
#[must_use]
pub fn parse_caption_entities(&self) -> Option<Vec<MessageEntityRef<'_>>> {
self.caption()
.zip(self.caption_entities())

View file

@ -43,6 +43,7 @@ pub struct MessageEntityRef<'a> {
}
impl MessageEntity {
#[must_use]
pub const fn new(kind: MessageEntityKind, offset: usize, length: usize) -> Self {
Self {
kind,
@ -52,6 +53,7 @@ impl MessageEntity {
}
/// Create a message entity representing a bold text.
#[must_use]
pub const fn bold(offset: usize, length: usize) -> Self {
Self {
kind: MessageEntityKind::Bold,
@ -61,6 +63,7 @@ impl MessageEntity {
}
/// Create a message entity representing an italic text.
#[must_use]
pub const fn italic(offset: usize, length: usize) -> Self {
Self {
kind: MessageEntityKind::Italic,
@ -70,6 +73,7 @@ impl MessageEntity {
}
/// Create a message entity representing an underline text.
#[must_use]
pub const fn underline(offset: usize, length: usize) -> Self {
Self {
kind: MessageEntityKind::Underline,
@ -79,6 +83,7 @@ impl MessageEntity {
}
/// Create a message entity representing a strikethrough text.
#[must_use]
pub const fn strikethrough(offset: usize, length: usize) -> Self {
Self {
kind: MessageEntityKind::Strikethrough,
@ -88,6 +93,7 @@ impl MessageEntity {
}
/// Create a message entity representing a spoiler text.
#[must_use]
pub const fn spoiler(offset: usize, length: usize) -> Self {
Self {
kind: MessageEntityKind::Spoiler,
@ -97,6 +103,7 @@ impl MessageEntity {
}
/// Create a message entity representing a monowidth text.
#[must_use]
pub const fn code(offset: usize, length: usize) -> Self {
Self {
kind: MessageEntityKind::Code,
@ -106,6 +113,7 @@ impl MessageEntity {
}
/// Create a message entity representing a monowidth block.
#[must_use]
pub const fn pre(language: Option<String>, offset: usize, length: usize) -> Self {
Self {
kind: MessageEntityKind::Pre { language },
@ -115,6 +123,7 @@ impl MessageEntity {
}
/// Create a message entity representing a clickable text URL.
#[must_use]
pub const fn text_link(url: reqwest::Url, offset: usize, length: usize) -> Self {
Self {
kind: MessageEntityKind::TextLink { url },
@ -129,6 +138,7 @@ impl MessageEntity {
///
/// If you don't have a complete [`User`] value, please use
/// [`MessageEntity::text_mention_id`] instead.
#[must_use]
pub fn text_mention(user: User, offset: usize, length: usize) -> Self {
Self {
kind: MessageEntityKind::TextMention { user },
@ -139,6 +149,7 @@ impl MessageEntity {
/// Create a message entity representing a text link in the form of
/// `tg://user/?id=...` that mentions user with `user_id`.
#[must_use]
pub fn text_mention_id(user_id: UserId, offset: usize, length: usize) -> Self {
Self {
kind: MessageEntityKind::TextLink { url: user_id.url() },
@ -147,16 +158,19 @@ impl MessageEntity {
}
}
#[must_use]
pub fn kind(mut self, val: MessageEntityKind) -> Self {
self.kind = val;
self
}
#[must_use]
pub const fn offset(mut self, val: usize) -> Self {
self.offset = val;
self
}
#[must_use]
pub const fn length(mut self, val: usize) -> Self {
self.length = val;
self
@ -165,11 +179,13 @@ impl MessageEntity {
impl<'a> MessageEntityRef<'a> {
/// Returns kind of this entity.
#[must_use]
pub fn kind(&self) -> &'a MessageEntityKind {
self.kind
}
/// Returns the text that this entity is related to.
#[must_use]
pub fn text(&self) -> &'a str {
&self.message[self.range.clone()]
}
@ -178,34 +194,40 @@ impl<'a> MessageEntityRef<'a> {
///
/// The range is in bytes for UTF-8 encoding i.e. you can use it with common
/// Rust strings.
#[must_use]
pub fn range(&self) -> Range<usize> {
self.range.clone()
}
/// Returns the offset (in bytes, for UTF-8) to the start of this entity in
/// the original message.
#[must_use]
pub fn start(&self) -> usize {
self.range.start
}
/// Returns the offset (in bytes, for UTF-8) to the end of this entity in
/// the original message.
#[must_use]
pub fn end(&self) -> usize {
self.range.end
}
/// Returns the length of this entity in bytes for UTF-8 encoding.
#[allow(clippy::len_without_is_empty)]
#[must_use]
pub fn len(&self) -> usize {
self.range.len()
}
/// Returns the full text of the original message.
#[must_use]
pub fn message_text(&self) -> &'a str {
self.message
}
/// Parses telegram [`MessageEntity`]s converting offsets to UTF-8.
#[must_use]
pub fn parse(text: &'a str, entities: &'a [MessageEntity]) -> Vec<Self> {
// This creates entities with **wrong** offsets (UTF-16) that we later patch.
let mut entities: Vec<_> = entities

View file

@ -32,6 +32,7 @@ impl PassportElementError {
self
}
#[must_use]
pub fn kind(mut self, val: PassportElementErrorKind) -> Self {
self.kind = val;
self
@ -104,6 +105,7 @@ impl PassportElementErrorDataField {
}
}
#[must_use]
pub fn r#type(mut self, val: PassportElementErrorDataFieldType) -> Self {
self.r#type = val;
self
@ -153,6 +155,7 @@ impl PassportElementErrorFrontSide {
}
}
#[must_use]
pub fn r#type(mut self, val: PassportElementErrorFrontSideType) -> Self {
self.r#type = val;
self
@ -194,6 +197,7 @@ impl PassportElementErrorReverseSide {
}
}
#[must_use]
pub fn r#type(mut self, val: PassportElementErrorReverseSideType) -> Self {
self.r#type = val;
self
@ -233,6 +237,7 @@ impl PassportElementErrorSelfie {
}
}
#[must_use]
pub fn r#type(mut self, val: PassportElementErrorSelfieType) -> Self {
self.r#type = val;
self
@ -273,6 +278,7 @@ impl PassportElementErrorFile {
}
}
#[must_use]
pub fn r#type(mut self, val: PassportElementErrorFileType) -> Self {
self.r#type = val;
self
@ -313,6 +319,7 @@ impl PassportElementErrorFiles {
}
}
#[must_use]
pub fn r#type(mut self, val: PassportElementErrorFilesType) -> Self {
self.r#type = val;
self
@ -354,6 +361,7 @@ impl PassportElementErrorTranslationFile {
}
}
#[must_use]
pub fn r#type(mut self, val: PassportElementErrorTranslationFileType) -> Self {
self.r#type = val;
self
@ -394,6 +402,7 @@ impl PassportElementErrorTranslationFiles {
}
}
#[must_use]
pub fn r#type(mut self, val: PassportElementErrorTranslationFilesType) -> Self {
self.r#type = val;
self
@ -434,6 +443,7 @@ impl PassportElementErrorUnspecified {
}
}
#[must_use]
pub fn r#type(mut self, val: PassportElementErrorUnspecifiedType) -> Self {
self.r#type = val;
self

View file

@ -75,6 +75,7 @@ impl KeyboardMarkup {
self
}
#[must_use]
pub fn append_to_row(mut self, index: usize, button: KeyboardButton) -> Self {
match self.keyboard.get_mut(index) {
Some(buttons) => buttons.push(button),

View file

@ -38,6 +38,7 @@ pub struct KeyboardRemove {
}
impl KeyboardRemove {
#[must_use]
pub const fn new() -> Self {
Self {
remove_keyboard: True,
@ -45,6 +46,7 @@ impl KeyboardRemove {
}
}
#[must_use]
pub const fn selective(mut self, val: bool) -> Self {
self.selective = Some(val);
self

View file

@ -50,6 +50,7 @@ impl ReplyMarkup {
/// `ReplyMarkup::KeyboardRemove(ReplyKeyboardRemove::new()))`.
///
/// [`KeyboardRemove`]: ReplyMarkup::KeyboardRemove
#[must_use]
pub fn kb_remove() -> Self {
Self::KeyboardRemove(KeyboardRemove::new())
}
@ -59,6 +60,7 @@ impl ReplyMarkup {
/// This is a shortcut to `ReplyMarkup::ForceReply(ForceReply::new())`.
///
/// [`ForceReply`]: ReplyMarkup::KeyboardRemove
#[must_use]
pub fn force_reply() -> Self {
Self::ForceReply(ForceReply::new())
}

View file

@ -84,6 +84,7 @@ impl Deref for Sticker {
impl StickerKind {
/// Returns `true` is this is a "normal" raster sticker.
#[must_use]
pub fn is_webp(&self) -> bool {
matches!(self, Self::Webp)
}
@ -91,6 +92,7 @@ impl StickerKind {
/// Returns `true` is this is an [animated] sticker.
///
/// [animated]: https://telegram.org/blog/animated-stickers
#[must_use]
pub fn is_animated(&self) -> bool {
matches!(self, Self::Animated)
}
@ -98,6 +100,7 @@ impl StickerKind {
/// Returns `true` is this is a [video] sticker.
///
/// [video]: https://telegram.org/blog/video-stickers-better-reactions
#[must_use]
pub fn is_video(&self) -> bool {
matches!(self, Self::Video)
}

View file

@ -29,6 +29,7 @@ pub struct Update {
}
impl Update {
#[must_use]
pub fn user(&self) -> Option<&User> {
match &self.kind {
UpdateKind::Message(m) => m.from(),
@ -43,6 +44,7 @@ impl Update {
}
}
#[must_use]
pub fn chat(&self) -> Option<&Chat> {
match &self.kind {
UpdateKind::Message(m) => Some(&m.chat),

View file

@ -40,27 +40,31 @@ pub struct User {
impl User {
/// Returns full name of this user, ie first and last names joined with a
/// space.
#[must_use]
pub fn full_name(&self) -> String {
match &self.last_name {
Some(last_name) => (format!("{0} {1}", self.first_name, last_name)),
Some(last_name) => format!("{0} {1}", self.first_name, last_name),
None => self.first_name.clone(),
}
}
/// Returns a username mention of this user. Returns `None` if
/// `self.username.is_none()`.
#[must_use]
pub fn mention(&self) -> Option<String> {
Some(format!("@{}", self.username.as_ref()?))
}
/// Returns an URL that links to this user in the form of
/// `tg://user/?id=<...>`.
#[must_use]
pub fn url(&self) -> reqwest::Url {
self.id.url()
}
/// Returns an URL that links to this user in the form of `t.me/<...>`.
/// Returns `None` if `self.username.is_none()`.
#[must_use]
pub fn tme_url(&self) -> Option<reqwest::Url> {
Some(
format!("https://t.me/{}", self.username.as_ref()?)
@ -71,12 +75,14 @@ impl User {
/// Returns an URL that links to this user in the form of `t.me/<...>` or
/// `tg://user/?id=<...>`, preferring `t.me` one when possible.
#[must_use]
pub fn preferably_tme_url(&self) -> reqwest::Url {
self.tme_url().unwrap_or_else(|| self.url())
}
/// Returns `true` if this is the special user used by telegram bot API to
/// denote an anonymous user that sends messages on behalf of a group.
#[must_use]
pub fn is_anonymous(&self) -> bool {
// Sanity check
debug_assert!(
@ -92,6 +98,7 @@ impl User {
/// Returns `true` if this is the special user used by telegram bot API to
/// denote an anonymous user that sends messages on behalf of a channel.
#[must_use]
pub fn is_channel(&self) -> bool {
// Sanity check
debug_assert!(
@ -110,6 +117,7 @@ impl User {
/// It is sometimes also used as a fallback, for example when a channel post
/// is automatically forwarded to a group, bots in a group will get a
/// message where `from` is the Telegram user.
#[must_use]
pub fn is_telegram(&self) -> bool {
// Sanity check
debug_assert!(

View file

@ -11,6 +11,7 @@ pub struct UserId(pub u64);
impl UserId {
/// Returns an URL that links to the user with this id in the form of
/// `tg://user/?id=<...>`.
#[must_use]
pub fn url(self) -> reqwest::Url {
reqwest::Url::parse(&format!("tg://user/?id={}", self)).unwrap()
}
@ -18,6 +19,7 @@ impl UserId {
/// Returns `true` if this is the id of the special user used by telegram
/// bot API to denote an anonymous user that sends messages on behalf of
/// a group.
#[must_use]
pub fn is_anonymous(self) -> bool {
// https://github.com/tdlib/td/blob/4791fb6a2af0257f6cad8396e10424a79ee5f768/td/telegram/ContactsManager.cpp#L4941-L4943
const ANON_ID: UserId = UserId(1087968824);
@ -28,6 +30,7 @@ impl UserId {
/// Returns `true` if this is the id of the special user used by telegram
/// bot API to denote an anonymous user that sends messages on behalf of
/// a channel.
#[must_use]
pub fn is_channel(self) -> bool {
// https://github.com/tdlib/td/blob/4791fb6a2af0257f6cad8396e10424a79ee5f768/td/telegram/ContactsManager.cpp#L4945-L4947
const ANON_CHANNEL_ID: UserId = UserId(136817688);
@ -41,6 +44,7 @@ impl UserId {
/// It is sometimes also used as a fallback, for example when a channel post
/// is automatically forwarded to a group, bots in a group will get a
/// message where `from` is the Telegram user.
#[must_use]
pub fn is_telegram(self) -> bool {
const TELEGRAM_USER_ID: UserId = UserId(777000);