Create LivePeriod enum and use it in live_period fields

TBA 7.3 introduced `0x7FFFFFFF` 'indefinite' live period
This commit is contained in:
Andrey Brusnik 2024-08-18 21:30:01 +04:00
parent d1108459b1
commit d388aa08e8
No known key found for this signature in database
GPG key ID: D33232F28CFF442C
9 changed files with 146 additions and 18 deletions

View file

@ -1298,9 +1298,9 @@ Schema(
),
Param(
name: "live_period",
ty: Option(u32),
ty: Option(RawTy("LivePeriod")),
descr: Doc(
md: "Period in seconds for which the location will be updated (see [Live Locations], should be between 60 and 86400.",
md: "Period in seconds for which the location will be updated (see [Live Locations], should be between 60 and 86400, or 0x7FFFFFFF for live locations that can be edited indefinitely.",
md_links: {"Live Locations": "https://telegram.org/blog/live-locations"}
)
),

View file

@ -3,7 +3,7 @@
use serde::Serialize;
use crate::types::{
BusinessConnectionId, Message, Recipient, ReplyMarkup, ReplyParameters, ThreadId,
BusinessConnectionId, LivePeriod, Message, Recipient, ReplyMarkup, ReplyParameters, ThreadId,
};
impl_payload! {
@ -27,10 +27,10 @@ impl_payload! {
pub message_thread_id: ThreadId,
/// The radius of uncertainty for the location, measured in meters; 0-1500
pub horizontal_accuracy: f64,
/// Period in seconds for which the location will be updated (see [Live Locations], should be between 60 and 86400.
/// Period in seconds for which the location will be updated (see [Live Locations], should be between 60 and 86400, or 0x7FFFFFFF for live locations that can be edited indefinitely.
///
/// [Live Locations]: https://telegram.org/blog/live-locations
pub live_period: u32,
pub live_period: LivePeriod,
/// For live locations, a direction in which the user is moving, in degrees. Must be between 1 and 360 if specified.
pub heading: u16,
/// For live locations, a maximum distance for proximity alerts about approaching another chat member, in meters. Must be between 1 and 100000 if specified.

View file

@ -96,6 +96,7 @@ pub use keyboard_button_request_chat::*;
pub use keyboard_button_request_users::*;
pub use label_price::*;
pub use link_preview_options::*;
pub use live_period::*;
pub use location::*;
pub use login_url::*;
pub use mask_position::*;
@ -234,6 +235,7 @@ mod keyboard_button_request_chat;
mod keyboard_button_request_users;
mod label_price;
mod link_preview_options;
mod live_period;
mod location;
mod login_url;
mod mask_position;

View file

@ -1271,7 +1271,7 @@ mod tests {
longitude: 1.0,
title: String::from("title"),
horizontal_accuracy: Some(1.0),
live_period: Some(1),
live_period: Some(1.into()),
heading: Some(1),
proximity_alert_radius: Some(1),
reply_markup: Some(InlineKeyboardMarkup::default()),

View file

@ -1,6 +1,6 @@
use serde::{Deserialize, Serialize};
use crate::types::{InlineKeyboardMarkup, InputMessageContent};
use crate::types::{InlineKeyboardMarkup, InputMessageContent, LivePeriod};
/// Represents a location on a map.
///
@ -28,8 +28,9 @@ pub struct InlineQueryResultLocation {
pub horizontal_accuracy: Option<f64>,
/// Period in seconds for which the location can be updated, should be
/// between 60 and 86400.
pub live_period: Option<u32>,
/// between 60 and 86400, or 0x7FFFFFFF for live locations that can be
/// edited indefinitely.
pub live_period: Option<LivePeriod>,
/// For live locations, a direction in which the user is moving, in degrees.
/// Must be between 1 and 360 if specified.
@ -116,7 +117,7 @@ impl InlineQueryResultLocation {
}
#[must_use]
pub fn live_period(mut self, val: u32) -> Self {
pub fn live_period(mut self, val: LivePeriod) -> Self {
self.live_period = Some(val);
self
}

View file

@ -1,7 +1,7 @@
use reqwest::Url;
use serde::{Deserialize, Serialize};
use crate::types::{LabeledPrice, LinkPreviewOptions, MessageEntity, ParseMode};
use crate::types::{LabeledPrice, LinkPreviewOptions, LivePeriod, MessageEntity, ParseMode};
/// This object represents the content of a message to be sent as a result of an
/// inline query.
@ -97,8 +97,9 @@ pub struct InputMessageContentLocation {
pub horizontal_accuracy: Option<f64>,
/// Period in seconds for which the location can be updated, should be
/// between 60 and 86400.
pub live_period: Option<u32>,
/// between 60 and 86400, or 0x7FFFFFFF for live locations that can be
/// edited indefinitely.
pub live_period: Option<LivePeriod>,
/// For live locations, a direction in which the user is moving, in degrees.
/// Must be between 1 and 360 if specified.
@ -136,7 +137,7 @@ impl InputMessageContentLocation {
}
#[must_use]
pub const fn live_period(mut self, val: u32) -> Self {
pub const fn live_period(mut self, val: LivePeriod) -> Self {
self.live_period = Some(val);
self
}

View file

@ -0,0 +1,123 @@
#![allow(clippy::from_over_into)]
use serde::{Deserialize, Serialize};
use crate::types::Seconds;
/// Period in seconds for which the location can be updated, should be
/// between 60 and 86400, or 0x7FFFFFFF for live locations that can be
/// edited indefinitely.
#[derive(Clone, Copy)]
#[derive(Debug, derive_more::Display)]
#[derive(PartialEq, Eq, PartialOrd, Ord, Hash)]
#[derive(Serialize)]
#[serde(untagged)]
pub enum LivePeriod {
Timeframe(Seconds),
Indefinite,
}
impl LivePeriod {
pub fn timeframe(&self) -> Option<Seconds> {
self.try_into().ok()
}
pub fn is_indefinite(&self) -> bool {
matches!(self, Self::Indefinite)
}
pub fn from_u32(seconds: u32) -> Self {
LivePeriod::Timeframe(Seconds::from_seconds(seconds))
}
pub fn from_seconds(seconds: Seconds) -> Self {
seconds.into()
}
}
impl TryInto<Seconds> for LivePeriod {
type Error = &'static str;
fn try_into(self) -> Result<Seconds, Self::Error> {
match self {
LivePeriod::Timeframe(v) => Ok(v),
LivePeriod::Indefinite => Err("indefinite live period"),
}
}
}
impl TryInto<Seconds> for &LivePeriod {
type Error = &'static str;
fn try_into(self) -> Result<Seconds, Self::Error> {
match self {
LivePeriod::Timeframe(v) => Ok(*v),
LivePeriod::Indefinite => Err("indefinite live period"),
}
}
}
impl Into<LivePeriod> for Seconds {
fn into(self) -> LivePeriod {
LivePeriod::Timeframe(self)
}
}
impl Into<LivePeriod> for &Seconds {
fn into(self) -> LivePeriod {
LivePeriod::Timeframe(*self)
}
}
impl Into<LivePeriod> for u32 {
fn into(self) -> LivePeriod {
LivePeriod::Timeframe(Seconds::from_seconds(self))
}
}
impl Into<LivePeriod> for &u32 {
fn into(self) -> LivePeriod {
LivePeriod::Timeframe(Seconds::from_seconds(*self))
}
}
impl<'de> Deserialize<'de> for LivePeriod {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
let value = u32::deserialize(deserializer)?;
if value == 0x7FFFFFFF {
Ok(LivePeriod::Indefinite)
} else {
Ok(LivePeriod::Timeframe(Seconds::from_seconds(value)))
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[derive(Serialize, Deserialize)]
struct Struct {
live_period: Option<LivePeriod>,
}
#[test]
fn deserialize_indefinite() {
let json = r#"{"live_period": 2147483647}"#; // 0x7FFFFFFF
let expected = LivePeriod::Indefinite;
let Struct { live_period } = serde_json::from_str(json).unwrap();
assert_eq!(live_period, Some(expected));
}
#[test]
fn deserialize_900() {
let json = r#"{"live_period": 900}"#;
let expected = LivePeriod::from_u32(900);
let Struct { live_period } = serde_json::from_str(json).unwrap();
assert_eq!(live_period, Some(expected));
}
}

View file

@ -1,6 +1,6 @@
use serde::{Deserialize, Serialize};
use crate::types::Seconds;
use crate::types::LivePeriod;
/// This object represents a point on the map.
#[serde_with::skip_serializing_none]
@ -17,7 +17,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<Seconds>,
pub live_period: Option<LivePeriod>,
/// The direction in which user is moving, in degrees; 1-360. For active
/// live locations only.

View file

@ -2224,7 +2224,8 @@ mod tests {
"venue": {
"location": {
"latitude": 0.0,
"longitude": 0.0
"longitude": 0.0,
"live_period": 900
},
"title": "Title",
"address": "Address",
@ -2240,7 +2241,7 @@ mod tests {
longitude: 0.0,
latitude: 0.0,
horizontal_accuracy: None,
live_period: None,
live_period: Some(LivePeriod::from_u32(900)),
heading: None,
proximity_alert_radius: None
},