Merge pull request #216 from teloxide/chat_member_status_is

Add `ChatMemberStatus::is_*` methods
This commit is contained in:
Hirrolot 2022-05-26 22:45:23 +06:00 committed by GitHub
commit 31935b4fd3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 75 additions and 0 deletions

View file

@ -8,8 +8,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## unreleased
- Add `ChatId` and `UserId` to the prelude ([#212][pr212])
- Add `is_*` methods to `ChatMemberStatus` analogous to the `ChatMember{,Kind}` methods ([#216][pr216])
[pr212]: https://github.com/teloxide/teloxide-core/pull/212
[pr216]: https://github.com/teloxide/teloxide-core/pull/216
## 0.6.0 - 2022-04-25

View file

@ -586,14 +586,87 @@ impl ChatMemberKind {
#[derive(Copy, Clone, Debug, Eq, Hash, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ChatMemberStatus {
#[serde(rename = "creator")]
Owner,
Administrator,
Member,
Restricted,
Left,
#[serde(rename = "kicked")]
Banned,
}
/// Simple methods for checking a user status.
impl ChatMemberStatus {
/// Returns `true` if the user is the [owner] of the given chat.
///
/// [owner]: ChatMemberKind::Owner
pub fn is_owner(&self) -> bool {
matches!(self, Self::Owner { .. })
}
/// Returns `true` if the user is an [administrator] of the given chat.
///
/// [administrator]: ChatMemberKind::Administrator
///
/// **Note**: this function **doesn't** return `true` if the user is the
/// owner of the given chat. See also: [`is_privileged`].
///
/// [`is_privileged`]: ChatMemberKind::is_privileged
pub fn is_administrator(&self) -> bool {
matches!(self, Self::Administrator { .. })
}
/// Returns `true` if the user is a common [member] of the given chat.
///
/// [member]: ChatMemberKind::Member
pub fn is_member(&self) -> bool {
matches!(self, Self::Member { .. })
}
/// Returns `true` if the user is [restricted] in the given chat.
///
/// [restricted]: ChatMemberKind::Restricted
pub fn is_restricted(&self) -> bool {
matches!(self, Self::Restricted { .. })
}
/// Returns `true` if the user [left] the given chat.
///
/// [left]: ChatMemberKind::Left
pub fn is_left(&self) -> bool {
matches!(self, Self::Left { .. })
}
/// Returns `true` if the user is [banned] in the given chat.
///
/// [banned]: ChatMemberKind::Banned
pub fn is_banned(&self) -> bool {
matches!(self, Self::Banned { .. })
}
}
/// Compound methods for checking a user status.
impl ChatMemberStatus {
/// Returns `true` if the user is privileged in the given chat. i.e. if the
/// user is either the [owner] or an [administrator] in the given chat.
///
/// [owner]: ChatMemberKind::Owner
/// [administrator]: ChatMemberKind::Administrator
pub fn is_privileged(&self) -> bool {
self.is_administrator() || self.is_owner()
}
/// Returns `true` if the user is currently present in the chat. i.e. if the
/// user **hasn't** [left] or been [banned].
///
/// [left]: ChatMemberKind::Left
/// [banned]: ChatMemberKind::Banned
pub fn is_present(&self) -> bool {
!(self.is_left() || self.is_banned())
}
}
#[cfg(test)]
mod tests {
use crate::types::UserId;