mirror of
https://github.com/teloxide/teloxide.git
synced 2024-12-22 22:46:39 +01:00
Add UpdateId
This commit is contained in:
parent
848fc14c0b
commit
13424c3fdc
4 changed files with 34 additions and 12 deletions
|
@ -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
|
||||
|
||||
|
|
|
@ -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::<Vec<Update>>(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(),
|
||||
]
|
||||
);
|
||||
}
|
||||
|
|
|
@ -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<D>(deserializer: D) -> Result<Self, D::Error>
|
||||
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),
|
||||
|
|
|
@ -354,7 +354,7 @@ impl<B: Requester> 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 {
|
||||
|
|
Loading…
Reference in a new issue