Use Seconds where it makes sense

This commit is contained in:
Maybe Waffle 2023-02-21 18:01:39 +04:00
parent 13032ac8dc
commit 65f693b31f
12 changed files with 42 additions and 34 deletions

View file

@ -1,7 +1,7 @@
use mime::Mime;
use serde::{Deserialize, Serialize};
use crate::types::{FileMeta, PhotoSize};
use crate::types::{FileMeta, PhotoSize, Seconds};
/// This object represents an animation file (GIF or H.264/MPEG-4 AVC video
/// without sound).
@ -21,7 +21,7 @@ pub struct Animation {
pub height: u32,
/// A duration of the video in seconds as defined by a sender.
pub duration: u32,
pub duration: Seconds,
/// An animation thumbnail as defined by a sender.
pub thumb: Option<PhotoSize>,
@ -62,7 +62,7 @@ mod tests {
file: FileMeta { id: "id".to_string(), unique_id: "".to_string(), size: 6500 },
width: 320,
height: 320,
duration: 59,
duration: Seconds::from_seconds(59),
thumb: Some(PhotoSize {
file: FileMeta { id: "id".to_owned(), unique_id: "".to_owned(), size: 3452 },
width: 320,

View file

@ -1,7 +1,7 @@
use mime::Mime;
use serde::{Deserialize, Serialize};
use crate::types::{FileMeta, PhotoSize};
use crate::types::{FileMeta, PhotoSize, Seconds};
/// This object represents an audio file to be treated as music by the Telegram
/// clients.
@ -15,7 +15,7 @@ pub struct Audio {
pub file: FileMeta,
/// A duration of the audio in seconds as defined by a sender.
pub duration: u32,
pub duration: Seconds,
/// A performer of the audio as defined by a sender or by audio tags.
pub performer: Option<String>,
@ -36,7 +36,7 @@ pub struct Audio {
#[cfg(test)]
mod tests {
use crate::types::FileMeta;
use crate::types::{FileMeta, Seconds};
use super::*;
@ -60,7 +60,7 @@ mod tests {
}"#;
let expected = Audio {
file: FileMeta { id: "id".to_string(), unique_id: "".to_string(), size: 123_456 },
duration: 60,
duration: Seconds::from_seconds(60),
performer: Some("Performer".to_string()),
title: Some("Title".to_string()),
mime_type: Some("application/zip".parse().unwrap()),

View file

@ -1,6 +1,8 @@
use serde::{Deserialize, Serialize};
use crate::types::{ChatId, ChatLocation, ChatPermissions, ChatPhoto, Message, True, User};
use crate::types::{
ChatId, ChatLocation, ChatPermissions, ChatPhoto, Message, Seconds, True, User,
};
/// This object represents a chat.
///
@ -29,7 +31,7 @@ pub struct Chat {
/// deleted; in seconds. Returned only in [`GetChat`].
///
/// [`GetChat`]: crate::payloads::GetChat
pub message_auto_delete_time: Option<u32>,
pub message_auto_delete_time: Option<Seconds>,
/// `true`, if non-administrators can only get the list of bots and
/// administrators in the chat. Returned only in [`GetChat`].
@ -202,7 +204,7 @@ pub struct PublicChatSupergroup {
/// unpriviledged user. Returned only from [`GetChat`].
///
/// [`GetChat`]: crate::payloads::GetChat
pub slow_mode_delay: Option<u32>,
pub slow_mode_delay: Option<Seconds>,
/// Unique identifier for the linked chat, i.e. the discussion group
/// identifier for a channel and vice versa. Returned only in [`GetChat`].
@ -355,7 +357,7 @@ impl Chat {
///
/// [`GetChat`]: crate::payloads::GetChat
#[must_use]
pub fn slow_mode_delay(&self) -> Option<u32> {
pub fn slow_mode_delay(&self) -> Option<Seconds> {
if let ChatKind::Public(this) = &self.kind {
if let PublicChatKind::Supergroup(this) = &this.kind {
return this.slow_mode_delay;

View file

@ -29,7 +29,7 @@ pub struct InlineQueryResultLocation {
/// Period in seconds for which the location can be updated, should be
/// between 60 and 86400.
pub live_period: Option<i32>,
pub live_period: Option<u32>,
/// For live locations, a direction in which the user is moving, in degrees.
/// Must be between 1 and 360 if specified.
@ -116,7 +116,7 @@ impl InlineQueryResultLocation {
}
#[must_use]
pub fn live_period(mut self, val: i32) -> Self {
pub fn live_period(mut self, val: u32) -> Self {
self.live_period = Some(val);
self
}

View file

@ -1,5 +1,7 @@
use serde::{Deserialize, Serialize};
use crate::types::Seconds;
/// This object represents a point on the map.
#[derive(Copy, Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct Location {
@ -14,7 +16,7 @@ pub struct Location {
/// Time relative to the message sending date, during which the location can
/// be updated, in seconds. For active live locations only.
pub live_period: Option<u32>,
pub live_period: Option<Seconds>,
/// The direction in which user is moving, in degrees; 1-360. For active
/// live locations only.

View file

@ -1,9 +1,11 @@
use serde::{Deserialize, Serialize};
use crate::types::Seconds;
/// This object represents a service message about a change in auto-delete timer
/// settings.
#[derive(Copy, Clone, Debug, Eq, Hash, PartialEq, Serialize, Deserialize)]
pub struct MessageAutoDeleteTimerChanged {
/// New auto-delete time for messages in the chat
pub message_auto_delete_time: u32,
pub message_auto_delete_time: Seconds,
}

View file

@ -1,4 +1,4 @@
use crate::types::{MessageEntity, PollType, User};
use crate::types::{MessageEntity, PollType, Seconds, User};
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
@ -48,7 +48,7 @@ pub struct Poll {
pub explanation_entities: Option<Vec<MessageEntity>>,
/// Amount of time in seconds the poll will be active after creation.
pub open_period: Option<u16>,
pub open_period: Option<Seconds>,
/// Point in time when the poll will be automatically closed.
#[serde(default, with = "crate::types::serde_opt_date_from_unix_timestamp")]

View file

@ -1,7 +1,7 @@
use std::time::Duration;
use serde::{Deserialize, Serialize};
use crate::types::{ChatId, Seconds};
/// Contains information about why a request was unsuccessful.
///
/// [The official docs](https://core.telegram.org/bots/api#responseparameters).
@ -9,25 +9,22 @@ use serde::{Deserialize, Serialize};
#[serde(rename_all = "snake_case")]
pub enum ResponseParameters {
/// The group has been migrated to a supergroup with the specified
/// identifier. This number may be greater than 32 bits and some
/// programming languages may have difficulty/silent defects in
/// interpreting it. But it is smaller than 52 bits, so a signed 64 bit
/// integer or double-precision float type are safe for storing this
/// identifier.
MigrateToChatId(i64),
MigrateToChatId(ChatId),
/// In case of exceeding flood control, the number of seconds left to wait
/// before the request can be repeated.
RetryAfter(#[serde(with = "crate::types::duration_secs")] Duration),
RetryAfter(Seconds),
}
#[cfg(test)]
mod tests {
use super::*;
use crate::types::{ChatId, Seconds};
#[test]
fn migrate_to_chat_id_deserialization() {
let expected = ResponseParameters::MigrateToChatId(123_456);
let expected = ResponseParameters::MigrateToChatId(ChatId(123_456));
let actual: ResponseParameters =
serde_json::from_str(r#"{"migrate_to_chat_id":123456}"#).unwrap();
@ -36,7 +33,7 @@ mod tests {
#[test]
fn retry_after_deserialization() {
let expected = ResponseParameters::RetryAfter(Duration::from_secs(123_456));
let expected = ResponseParameters::RetryAfter(Seconds::from_seconds(123_456));
let actual: ResponseParameters = serde_json::from_str(r#"{"retry_after":123456}"#).unwrap();
assert_eq!(expected, actual);

View file

@ -1,7 +1,7 @@
use mime::Mime;
use serde::{Deserialize, Serialize};
use crate::types::{FileMeta, PhotoSize};
use crate::types::{FileMeta, PhotoSize, Seconds};
/// This object represents a video file.
///
@ -20,7 +20,7 @@ pub struct Video {
pub height: u32,
/// Duration of the video in seconds as defined by sender.
pub duration: u32,
pub duration: Seconds,
/// Video thumbnail.
pub thumb: Option<PhotoSize>,

View file

@ -1,6 +1,11 @@
use serde::{Deserialize, Serialize};
use crate::types::Seconds;
/// This object represents a service message about a video chat ended in the
/// chat.
#[derive(Clone, Debug, Eq, Hash, PartialEq, Serialize, Deserialize)]
pub struct VideoChatEnded {}
pub struct VideoChatEnded {
/// Video chat duration in seconds.
duration: Seconds,
}

View file

@ -1,6 +1,6 @@
use serde::{Deserialize, Serialize};
use crate::types::{FileMeta, PhotoSize};
use crate::types::{FileMeta, PhotoSize, Seconds};
/// This object represents a [video message] (available in Telegram apps as of
/// [v.4.0]).
@ -21,7 +21,7 @@ pub struct VideoNote {
pub length: u32,
/// Duration of the video in seconds as defined by sender.
pub duration: u32,
pub duration: Seconds,
/// Video thumbnail.
pub thumb: Option<PhotoSize>,

View file

@ -1,7 +1,7 @@
use mime::Mime;
use serde::{Deserialize, Serialize};
use crate::types::FileMeta;
use crate::types::{FileMeta, Seconds};
/// This object represents a voice note.
///
@ -14,7 +14,7 @@ pub struct Voice {
pub file: FileMeta,
/// Duration of the audio in seconds as defined by sender.
pub duration: u32,
pub duration: Seconds,
/// MIME type of the file as defined by sender.
#[serde(with = "crate::types::non_telegram_types::mime::opt_deser")]