Serialize time as a millis epoch timestamp in cursors

This commit is contained in:
Natty 2024-01-10 03:09:14 +01:00
parent b2543b29d3
commit f06c74c85c
Signed by: natty
GPG Key ID: BF6CB659ADEE60EC
1 changed files with 32 additions and 1 deletions

View File

@ -5,17 +5,46 @@ pub mod note;
pub mod timeline; pub mod timeline;
pub mod user; pub mod user;
use crate::endpoints::ErrorKind;
use crate::util_types::U64Range; use crate::util_types::U64Range;
use chrono::{DateTime, Utc}; use chrono::{DateTime, Utc};
use serde::de::Error; use serde::de::Error;
use serde::{Deserialize, Deserializer, Serialize}; use serde::{Deserialize, Deserializer, Serialize};
use ts_rs::TS; use ts_rs::TS;
pub(crate) mod packed_time {
use chrono::{DateTime, NaiveDateTime, Utc};
use serde::de::Error;
use serde::{Deserialize, Deserializer, Serializer};
pub(crate) fn serialize<S>(value: &DateTime<Utc>, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.serialize_str(&value.timestamp_millis().to_string())
}
pub(crate) fn deserialize<'de, D>(deserializer: D) -> Result<DateTime<Utc>, D::Error>
where
D: Deserializer<'de>,
{
Ok(DateTime::<Utc>::from_utc(
NaiveDateTime::from_timestamp_millis(
String::deserialize(deserializer)?
.parse::<i64>()
.map_err(Error::custom)?,
)
.ok_or_else(|| Error::custom("millisecond value out of range"))?,
Utc,
))
}
}
#[derive(Clone, Debug, Deserialize, Serialize, TS)] #[derive(Clone, Debug, Deserialize, Serialize, TS)]
#[ts(export)] #[ts(export)]
pub struct RangeFilter { pub struct RangeFilter {
#[serde(with = "packed_time")]
pub time_start: DateTime<Utc>, pub time_start: DateTime<Utc>,
#[serde(with = "packed_time")]
pub time_end: DateTime<Utc>, pub time_end: DateTime<Utc>,
pub id_start: String, pub id_start: String,
pub id_end: String, pub id_end: String,
@ -24,6 +53,7 @@ pub struct RangeFilter {
#[derive(Clone, Debug, Deserialize, Serialize, TS)] #[derive(Clone, Debug, Deserialize, Serialize, TS)]
#[ts(export)] #[ts(export)]
pub struct StartFilter { pub struct StartFilter {
#[serde(with = "packed_time")]
pub time_start: DateTime<Utc>, pub time_start: DateTime<Utc>,
pub id_start: String, pub id_start: String,
} }
@ -31,6 +61,7 @@ pub struct StartFilter {
#[derive(Clone, Debug, Deserialize, Serialize, TS)] #[derive(Clone, Debug, Deserialize, Serialize, TS)]
#[ts(export)] #[ts(export)]
pub struct EndFilter { pub struct EndFilter {
#[serde(with = "packed_time")]
pub time_end: DateTime<Utc>, pub time_end: DateTime<Utc>,
pub id_end: String, pub id_end: String,
} }