From 0146202236f733e05b210237854a5250623c004b Mon Sep 17 00:00:00 2001 From: asonix Date: Tue, 29 Aug 2023 12:59:36 -0500 Subject: [PATCH] Fully commit to HumanDate --- src/details.rs | 43 +++++++++++++++++++------------------------ src/repo/sled.rs | 11 +++++++---- src/repo_04/sled.rs | 24 ++++++++++++++++++++++-- 3 files changed, 48 insertions(+), 30 deletions(-) diff --git a/src/details.rs b/src/details.rs index 8255aee..86b752a 100644 --- a/src/details.rs +++ b/src/details.rs @@ -9,10 +9,10 @@ use actix_web::web; use time::{format_description::well_known::Rfc3339, OffsetDateTime}; #[derive(Copy, Clone, Debug, serde::Deserialize, serde::Serialize)] -#[serde(untagged)] -pub(crate) enum MaybeHumanDate { - HumanDate(#[serde(with = "time::serde::rfc3339")] time::OffsetDateTime), - OldDate(#[serde(serialize_with = "time::serde::rfc3339::serialize")] time::OffsetDateTime), +#[serde(transparent)] +pub(crate) struct HumanDate { + #[serde(with = "time::serde::rfc3339")] + pub(crate) timestamp: time::OffsetDateTime, } #[derive(Clone, Debug, serde::Deserialize, serde::Serialize)] @@ -21,7 +21,7 @@ pub(crate) struct Details { height: u16, frames: Option, content_type: Serde, - created_at: MaybeHumanDate, + created_at: HumanDate, format: InternalFormat, } @@ -31,10 +31,7 @@ impl Details { } pub(crate) fn created_at(&self) -> time::OffsetDateTime { - match self.created_at { - MaybeHumanDate::OldDate(timestamp) => timestamp, - MaybeHumanDate::HumanDate(timestamp) => timestamp, - } + self.created_at.timestamp } pub(crate) async fn from_bytes(timeout: u64, input: web::Bytes) -> Result { @@ -87,7 +84,7 @@ impl Details { width: u16, height: u16, frames: Option, - created_at: MaybeHumanDate, + created_at: HumanDate, ) -> Self { Self { width, @@ -110,29 +107,27 @@ impl Details { height, frames, content_type: Serde::new(format.media_type()), - created_at: MaybeHumanDate::HumanDate(OffsetDateTime::now_utc()), + created_at: HumanDate { + timestamp: OffsetDateTime::now_utc(), + }, format, } } } -impl From for std::time::SystemTime { - fn from(this: MaybeHumanDate) -> Self { - match this { - MaybeHumanDate::OldDate(old) => old.into(), - MaybeHumanDate::HumanDate(human) => human.into(), - } +impl From for std::time::SystemTime { + fn from(HumanDate { timestamp }: HumanDate) -> Self { + timestamp.into() } } -impl std::fmt::Display for MaybeHumanDate { +impl std::fmt::Display for HumanDate { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - match self { - Self::OldDate(date) | Self::HumanDate(date) => { - let s = date.format(&Rfc3339).map_err(|_| std::fmt::Error)?; + let s = self + .timestamp + .format(&Rfc3339) + .map_err(|_| std::fmt::Error)?; - f.write_str(&s) - } - } + f.write_str(&s) } } diff --git a/src/repo/sled.rs b/src/repo/sled.rs index a7899c0..10d488b 100644 --- a/src/repo/sled.rs +++ b/src/repo/sled.rs @@ -1,5 +1,5 @@ use crate::{ - details::MaybeHumanDate, + details::HumanDate, serde_str::Serde, store::StoreError, stream::{from_iterator, LocalBoxStream}, @@ -142,9 +142,12 @@ impl SledRepo { #[tracing::instrument(level = "warn", skip_all)] pub(crate) async fn export(&self) -> Result<(), RepoError> { - let path = self - .export_path - .join(MaybeHumanDate::HumanDate(time::OffsetDateTime::now_utc()).to_string()); + let path = self.export_path.join( + HumanDate { + timestamp: time::OffsetDateTime::now_utc(), + } + .to_string(), + ); let export_db = Self::open(path, self.cache_capacity)?; diff --git a/src/repo_04/sled.rs b/src/repo_04/sled.rs index 5af0b69..594a994 100644 --- a/src/repo_04/sled.rs +++ b/src/repo_04/sled.rs @@ -1,4 +1,5 @@ use crate::{ + details::HumanDate, repo_04::{ Alias, AliasRepo, BaseRepo, DeleteToken, Details, HashRepo, Identifier, IdentifierRepo, RepoError, SettingsRepo, @@ -15,6 +16,21 @@ use std::{ }, }; +#[derive(Copy, Clone, Debug, serde::Deserialize, serde::Serialize)] +#[serde(untagged)] +enum MaybeHumanDate { + HumanDate(#[serde(with = "time::serde::rfc3339")] time::OffsetDateTime), + OldDate(#[serde(serialize_with = "time::serde::rfc3339::serialize")] time::OffsetDateTime), +} + +impl MaybeHumanDate { + fn into_human_date(self) -> HumanDate { + match self { + Self::HumanDate(timestamp) | Self::OldDate(timestamp) => HumanDate { timestamp }, + } + } +} + macro_rules! b { ($self:ident.$ident:ident, $expr:expr) => {{ let $ident = $self.$ident.clone(); @@ -63,7 +79,7 @@ pub(crate) struct OldDetails { height: u16, frames: Option, content_type: crate::serde_str::Serde, - created_at: crate::details::MaybeHumanDate, + created_at: MaybeHumanDate, #[serde(skip_serializing_if = "Option::is_none")] format: Option, } @@ -87,7 +103,11 @@ impl OldDetails { })?; Some(Details::from_parts_full( - format, width, height, frames, created_at, + format, + width, + height, + frames, + created_at.into_human_date(), )) } }