mirror of
https://github.com/teloxide/teloxide.git
synced 2025-01-03 17:52:12 +01:00
Add a Seconds
newtype
This commit is contained in:
parent
387f6d1284
commit
13032ac8dc
2 changed files with 61 additions and 0 deletions
|
@ -248,10 +248,12 @@ mod non_telegram_types {
|
|||
|
||||
mod chat_id;
|
||||
mod recipient;
|
||||
mod seconds;
|
||||
mod user_id;
|
||||
|
||||
pub use chat_id::*;
|
||||
pub use recipient::*;
|
||||
pub use seconds::*;
|
||||
pub use user_id::*;
|
||||
|
||||
use serde::Serialize;
|
||||
|
|
59
crates/teloxide-core/src/types/seconds.rs
Normal file
59
crates/teloxide-core/src/types/seconds.rs
Normal file
|
@ -0,0 +1,59 @@
|
|||
use serde::{Deserialize, Serialize};
|
||||
|
||||
/// A wrapper around `u32` which represents duration is seconds.
|
||||
#[derive(Clone, Copy)]
|
||||
#[derive(PartialEq, Eq, PartialOrd, Ord, Hash)]
|
||||
#[derive(Debug, derive_more::Display)]
|
||||
#[display(fmt = "{}s", _0)]
|
||||
#[derive(Serialize, Deserialize)]
|
||||
#[serde(transparent)]
|
||||
pub struct Seconds(u32);
|
||||
|
||||
impl Seconds {
|
||||
/// Creates a new duration with a given number of `seconds`.
|
||||
pub const fn from_seconds(seconds: u32) -> Self {
|
||||
Self(seconds)
|
||||
}
|
||||
|
||||
/// Returns the number of seconds in this duration
|
||||
pub const fn seconds(self) -> u32 {
|
||||
self.0
|
||||
}
|
||||
|
||||
/// Returns [`std::time::Duration`] equivalent of this duration.
|
||||
pub const fn duration(self) -> std::time::Duration {
|
||||
std::time::Duration::from_secs(self.seconds() as u64)
|
||||
}
|
||||
|
||||
/// Returns [`chrono::Duration`] equivalent of this duration.
|
||||
pub fn chrono_duration(self) -> chrono::Duration {
|
||||
chrono::Duration::seconds(self.seconds() as i64)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
struct W {
|
||||
seconds: Seconds,
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn deserialization() {
|
||||
let expected = Seconds::from_seconds(123456);
|
||||
let W { seconds: actual } = serde_json::from_str(r#"{"seconds":123456}"#).unwrap();
|
||||
|
||||
assert_eq!(expected, actual);
|
||||
assert_eq!(actual.seconds(), 123456);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn serialization() {
|
||||
let expected = r#"{"seconds":123456}"#;
|
||||
let actual = serde_json::to_string(&W { seconds: Seconds::from_seconds(123456) }).unwrap();
|
||||
|
||||
assert_eq!(expected, actual);
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue