Fully commit to HumanDate

This commit is contained in:
asonix 2023-08-29 12:59:36 -05:00
parent 64950bfe0e
commit 0146202236
3 changed files with 48 additions and 30 deletions

View file

@ -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<u32>,
content_type: Serde<mime::Mime>,
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<Self, Error> {
@ -87,7 +84,7 @@ impl Details {
width: u16,
height: u16,
frames: Option<u32>,
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<MaybeHumanDate> for std::time::SystemTime {
fn from(this: MaybeHumanDate) -> Self {
match this {
MaybeHumanDate::OldDate(old) => old.into(),
MaybeHumanDate::HumanDate(human) => human.into(),
}
impl From<HumanDate> 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)
}
}

View file

@ -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)?;

View file

@ -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<u32>,
content_type: crate::serde_str::Serde<mime::Mime>,
created_at: crate::details::MaybeHumanDate,
created_at: MaybeHumanDate,
#[serde(skip_serializing_if = "Option::is_none")]
format: Option<crate::formats::InternalFormat>,
}
@ -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(),
))
}
}