From 529b38afee20bf855cbd48d566149b015f840f72 Mon Sep 17 00:00:00 2001 From: Maybe Waffle Date: Sat, 29 Jul 2023 14:03:10 +0400 Subject: [PATCH] Add a way to go `ChatId` -> `UserId` --- crates/teloxide-core/CHANGELOG.md | 2 ++ crates/teloxide-core/src/types/chat_id.rs | 9 +++++++++ 2 files changed, 11 insertions(+) diff --git a/crates/teloxide-core/CHANGELOG.md b/crates/teloxide-core/CHANGELOG.md index 72b70c74..b6160207 100644 --- a/crates/teloxide-core/CHANGELOG.md +++ b/crates/teloxide-core/CHANGELOG.md @@ -16,9 +16,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - `Seconds` type, which represents a duration is seconds ([#859][pr859]) - `VideoChatEnded::duration` field that was previously missed ([#859][pr859]) - `ThreadId` newtype over `MessageId`, used for identifying reply threads ([#887][pr887]) +- `ChatId::as_user` ([#905][pr905]) [pr851]: https://github.com/teloxide/teloxide/pull/851 [pr887]: https://github.com/teloxide/teloxide/pull/887 +[pr905]: https://github.com/teloxide/teloxide/pull/905 ### Fixed diff --git a/crates/teloxide-core/src/types/chat_id.rs b/crates/teloxide-core/src/types/chat_id.rs index cccbc4a3..71363115 100644 --- a/crates/teloxide-core/src/types/chat_id.rs +++ b/crates/teloxide-core/src/types/chat_id.rs @@ -50,6 +50,15 @@ impl ChatId { matches!(self.to_bare(), BareChatId::Channel(_)) } + /// Returns user id, if this is an id of a user. + #[must_use] + pub fn as_user(self) -> Option { + match self.to_bare() { + BareChatId::User(u) => Some(u), + BareChatId::Group(_) | BareChatId::Channel(_) => None, + } + } + /// Converts this id to "bare" MTProto peer id. /// /// See [`BareChatId`] for more.