diff --git a/crates/teloxide-core/CHANGELOG.md b/crates/teloxide-core/CHANGELOG.md index b6160207..48216103 100644 --- a/crates/teloxide-core/CHANGELOG.md +++ b/crates/teloxide-core/CHANGELOG.md @@ -17,6 +17,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - `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]) +- Implement `PartialEq for UserId` and `PartialEq for ChatId` ([#905][pr905]) [pr851]: https://github.com/teloxide/teloxide/pull/851 [pr887]: https://github.com/teloxide/teloxide/pull/887 diff --git a/crates/teloxide-core/src/types/chat_id.rs b/crates/teloxide-core/src/types/chat_id.rs index 71363115..f9cdb32c 100644 --- a/crates/teloxide-core/src/types/chat_id.rs +++ b/crates/teloxide-core/src/types/chat_id.rs @@ -82,6 +82,12 @@ impl From for ChatId { } } +impl PartialEq for ChatId { + fn eq(&self, other: &UserId) -> bool { + self.is_user() && *self == ChatId::from(*other) + } +} + impl BareChatId { /// Converts bare chat id back to normal bot API [`ChatId`]. #[allow(unused)] @@ -152,4 +158,16 @@ mod tests { fn display() { assert_eq!(ChatId(1).to_string(), "1"); } + + #[test] + fn user_id_eq() { + assert_eq!(ChatId(12), UserId(12)); + assert_eq!(ChatId(4652762), UserId(4652762)); + assert_ne!(ChatId(17), UserId(42)); + + // The user id is not well formed, so even though `-1 == max` is true, + // we don't want user id to match + assert_eq!(-1i64, u64::MAX as i64); + assert_ne!(ChatId(-1), UserId(u64::MAX)); + } } diff --git a/crates/teloxide-core/src/types/user_id.rs b/crates/teloxide-core/src/types/user_id.rs index 57c95d13..625a1691 100644 --- a/crates/teloxide-core/src/types/user_id.rs +++ b/crates/teloxide-core/src/types/user_id.rs @@ -1,5 +1,7 @@ use serde::{Deserialize, Serialize}; +use crate::types::ChatId; + /// Identifier of a user. #[derive(Clone, Copy)] #[derive(Debug, derive_more::Display)] @@ -52,6 +54,13 @@ impl UserId { } } +impl PartialEq for UserId { + fn eq(&self, other: &ChatId) -> bool { + // Reuse `PartialEq for ChatId` impl + other == self + } +} + #[cfg(test)] mod tests { use serde::{Deserialize, Serialize};