From 13424c3fdc508c8543ecde58d136250fa2307ba3 Mon Sep 17 00:00:00 2001 From: Maybe Waffle Date: Thu, 15 Jun 2023 15:40:50 +0400 Subject: [PATCH] Add `UpdateId` --- crates/teloxide-core/CHANGELOG.md | 2 ++ crates/teloxide-core/src/net/request.rs | 14 ++++------ crates/teloxide-core/src/types/update.rs | 28 +++++++++++++++++-- .../teloxide/src/update_listeners/polling.rs | 2 +- 4 files changed, 34 insertions(+), 12 deletions(-) diff --git a/crates/teloxide-core/CHANGELOG.md b/crates/teloxide-core/CHANGELOG.md index 6d0f2d73..72b70c74 100644 --- a/crates/teloxide-core/CHANGELOG.md +++ b/crates/teloxide-core/CHANGELOG.md @@ -60,11 +60,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - `PollAnswer::option_ids` now use `u8` instead of `i32` ([#887][pr887]) - Use `u32` for sizes and `Seconds` for timespans in `InlineQueryResult*` ([#887][pr887]) - `SendGame::reply_to_message_id`, `SendSticker::reply_to_message_id` and `SendInvoice::reply_to_message_id` now use `MessageId` instead of `i32` ([#887][pr887]) +- Use `UpdateId` for `Update::id` ([#892][pr892]) [pr852]: https://github.com/teloxide/teloxide/pull/853 [pr859]: https://github.com/teloxide/teloxide/pull/859 [pr876]: https://github.com/teloxide/teloxide/pull/876 [pr885]: https://github.com/teloxide/teloxide/pull/885 +[pr892]: https://github.com/teloxide/teloxide/pull/892 ### Deprecated diff --git a/crates/teloxide-core/src/net/request.rs b/crates/teloxide-core/src/net/request.rs index dc9c5a88..74d12c47 100644 --- a/crates/teloxide-core/src/net/request.rs +++ b/crates/teloxide-core/src/net/request.rs @@ -162,13 +162,11 @@ where #[cfg(test)] mod tests { - use crate::types::{ChatId, Seconds}; - use cool_asserts::assert_matches; use crate::{ net::request::deserialize_response, - types::{True, Update, UpdateKind}, + types::{ChatId, Seconds, True, Update, UpdateId, UpdateKind}, ApiError, RequestError, }; @@ -223,7 +221,7 @@ mod tests { .to_owned(); let res = deserialize_response::>(json).unwrap(); - assert_matches!(res, [Update { id: 0, kind: UpdateKind::PollAnswer(_) }]); + assert_matches!(res, [Update { id: UpdateId(0), kind: UpdateKind::PollAnswer(_) }]); } /// Check that `get_updates` can work with malformed updates. @@ -264,10 +262,10 @@ mod tests { assert_matches!( res, [ - Update { id: 0, kind: UpdateKind::PollAnswer(_) }, - Update { id: 1, kind: UpdateKind::Error(v) } if v.is_object(), - Update { id: 2, kind: UpdateKind::PollAnswer(_) }, - Update { id: 3, kind: UpdateKind::Error(v) } if v.is_object(), + Update { id: UpdateId(0), kind: UpdateKind::PollAnswer(_) }, + Update { id: UpdateId(1), kind: UpdateKind::Error(v) } if v.is_object(), + Update { id: UpdateId(2), kind: UpdateKind::PollAnswer(_) }, + Update { id: UpdateId(3), kind: UpdateKind::Error(v) } if v.is_object(), ] ); } diff --git a/crates/teloxide-core/src/types/update.rs b/crates/teloxide-core/src/types/update.rs index 54b53fbf..4c5ff6f4 100644 --- a/crates/teloxide-core/src/types/update.rs +++ b/crates/teloxide-core/src/types/update.rs @@ -22,12 +22,22 @@ pub struct Update { /// week, then identifier of the next update will be chosen randomly /// instead of sequentially. #[serde(rename = "update_id")] - pub id: i32, + pub id: UpdateId, #[serde(flatten)] pub kind: UpdateKind, } +/// An identifier of a telegram update. +/// +/// See [`Update::id`] for more information. +#[derive(Clone, Copy)] +#[derive(Debug)] +#[derive(PartialEq, Eq, PartialOrd, Ord, Hash)] +#[derive(Serialize, Deserialize)] +#[serde(transparent)] +pub struct UpdateId(pub u32); + #[derive(Clone, Debug, PartialEq)] pub enum UpdateKind { // NB: When adding new variants, don't forget to update @@ -208,6 +218,18 @@ impl Update { } } +impl UpdateId { + /// Returns the offset for the **next** update that can be used for polling. + /// + /// I.e. `self.0 + 1`. + #[must_use] + pub fn as_offset(self) -> i32 { + debug_assert!(self.0 < i32::MAX as u32); + + self.0 as i32 + 1 + } +} + impl<'de> Deserialize<'de> for UpdateKind { fn deserialize(deserializer: D) -> Result where @@ -344,7 +366,7 @@ fn empty_error() -> UpdateKind { mod test { use crate::types::{ Chat, ChatId, ChatKind, ChatPrivate, MediaKind, MediaText, Message, MessageCommon, - MessageId, MessageKind, Update, UpdateKind, User, UserId, + MessageId, MessageKind, Update, UpdateId, UpdateKind, User, UserId, }; use chrono::{DateTime, NaiveDateTime, Utc}; @@ -379,7 +401,7 @@ mod test { }"#; let expected = Update { - id: 892_252_934, + id: UpdateId(892_252_934), kind: UpdateKind::Message(Message { via_bot: None, id: MessageId(6557), diff --git a/crates/teloxide/src/update_listeners/polling.rs b/crates/teloxide/src/update_listeners/polling.rs index 20dd95ba..3695a150 100644 --- a/crates/teloxide/src/update_listeners/polling.rs +++ b/crates/teloxide/src/update_listeners/polling.rs @@ -354,7 +354,7 @@ impl Stream for PollingStream<'_, B> { } Ok(updates) => { if let Some(upd) = updates.last() { - *this.offset = upd.id + 1; + *this.offset = upd.id.as_offset(); } match *this.drop_pending_updates {