mirror of
https://git.asonix.dog/asonix/pict-rs
synced 2024-12-22 19:31:35 +00:00
Fully commit to HumanDate
This commit is contained in:
parent
64950bfe0e
commit
0146202236
3 changed files with 48 additions and 30 deletions
|
@ -9,10 +9,10 @@ use actix_web::web;
|
||||||
use time::{format_description::well_known::Rfc3339, OffsetDateTime};
|
use time::{format_description::well_known::Rfc3339, OffsetDateTime};
|
||||||
|
|
||||||
#[derive(Copy, Clone, Debug, serde::Deserialize, serde::Serialize)]
|
#[derive(Copy, Clone, Debug, serde::Deserialize, serde::Serialize)]
|
||||||
#[serde(untagged)]
|
#[serde(transparent)]
|
||||||
pub(crate) enum MaybeHumanDate {
|
pub(crate) struct HumanDate {
|
||||||
HumanDate(#[serde(with = "time::serde::rfc3339")] time::OffsetDateTime),
|
#[serde(with = "time::serde::rfc3339")]
|
||||||
OldDate(#[serde(serialize_with = "time::serde::rfc3339::serialize")] time::OffsetDateTime),
|
pub(crate) timestamp: time::OffsetDateTime,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Debug, serde::Deserialize, serde::Serialize)]
|
#[derive(Clone, Debug, serde::Deserialize, serde::Serialize)]
|
||||||
|
@ -21,7 +21,7 @@ pub(crate) struct Details {
|
||||||
height: u16,
|
height: u16,
|
||||||
frames: Option<u32>,
|
frames: Option<u32>,
|
||||||
content_type: Serde<mime::Mime>,
|
content_type: Serde<mime::Mime>,
|
||||||
created_at: MaybeHumanDate,
|
created_at: HumanDate,
|
||||||
format: InternalFormat,
|
format: InternalFormat,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -31,10 +31,7 @@ impl Details {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn created_at(&self) -> time::OffsetDateTime {
|
pub(crate) fn created_at(&self) -> time::OffsetDateTime {
|
||||||
match self.created_at {
|
self.created_at.timestamp
|
||||||
MaybeHumanDate::OldDate(timestamp) => timestamp,
|
|
||||||
MaybeHumanDate::HumanDate(timestamp) => timestamp,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) async fn from_bytes(timeout: u64, input: web::Bytes) -> Result<Self, Error> {
|
pub(crate) async fn from_bytes(timeout: u64, input: web::Bytes) -> Result<Self, Error> {
|
||||||
|
@ -87,7 +84,7 @@ impl Details {
|
||||||
width: u16,
|
width: u16,
|
||||||
height: u16,
|
height: u16,
|
||||||
frames: Option<u32>,
|
frames: Option<u32>,
|
||||||
created_at: MaybeHumanDate,
|
created_at: HumanDate,
|
||||||
) -> Self {
|
) -> Self {
|
||||||
Self {
|
Self {
|
||||||
width,
|
width,
|
||||||
|
@ -110,29 +107,27 @@ impl Details {
|
||||||
height,
|
height,
|
||||||
frames,
|
frames,
|
||||||
content_type: Serde::new(format.media_type()),
|
content_type: Serde::new(format.media_type()),
|
||||||
created_at: MaybeHumanDate::HumanDate(OffsetDateTime::now_utc()),
|
created_at: HumanDate {
|
||||||
|
timestamp: OffsetDateTime::now_utc(),
|
||||||
|
},
|
||||||
format,
|
format,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<MaybeHumanDate> for std::time::SystemTime {
|
impl From<HumanDate> for std::time::SystemTime {
|
||||||
fn from(this: MaybeHumanDate) -> Self {
|
fn from(HumanDate { timestamp }: HumanDate) -> Self {
|
||||||
match this {
|
timestamp.into()
|
||||||
MaybeHumanDate::OldDate(old) => old.into(),
|
|
||||||
MaybeHumanDate::HumanDate(human) => human.into(),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl std::fmt::Display for MaybeHumanDate {
|
impl std::fmt::Display for HumanDate {
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
match self {
|
let s = self
|
||||||
Self::OldDate(date) | Self::HumanDate(date) => {
|
.timestamp
|
||||||
let s = date.format(&Rfc3339).map_err(|_| std::fmt::Error)?;
|
.format(&Rfc3339)
|
||||||
|
.map_err(|_| std::fmt::Error)?;
|
||||||
|
|
||||||
f.write_str(&s)
|
f.write_str(&s)
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
use crate::{
|
use crate::{
|
||||||
details::MaybeHumanDate,
|
details::HumanDate,
|
||||||
serde_str::Serde,
|
serde_str::Serde,
|
||||||
store::StoreError,
|
store::StoreError,
|
||||||
stream::{from_iterator, LocalBoxStream},
|
stream::{from_iterator, LocalBoxStream},
|
||||||
|
@ -142,9 +142,12 @@ impl SledRepo {
|
||||||
|
|
||||||
#[tracing::instrument(level = "warn", skip_all)]
|
#[tracing::instrument(level = "warn", skip_all)]
|
||||||
pub(crate) async fn export(&self) -> Result<(), RepoError> {
|
pub(crate) async fn export(&self) -> Result<(), RepoError> {
|
||||||
let path = self
|
let path = self.export_path.join(
|
||||||
.export_path
|
HumanDate {
|
||||||
.join(MaybeHumanDate::HumanDate(time::OffsetDateTime::now_utc()).to_string());
|
timestamp: time::OffsetDateTime::now_utc(),
|
||||||
|
}
|
||||||
|
.to_string(),
|
||||||
|
);
|
||||||
|
|
||||||
let export_db = Self::open(path, self.cache_capacity)?;
|
let export_db = Self::open(path, self.cache_capacity)?;
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
use crate::{
|
use crate::{
|
||||||
|
details::HumanDate,
|
||||||
repo_04::{
|
repo_04::{
|
||||||
Alias, AliasRepo, BaseRepo, DeleteToken, Details, HashRepo, Identifier, IdentifierRepo,
|
Alias, AliasRepo, BaseRepo, DeleteToken, Details, HashRepo, Identifier, IdentifierRepo,
|
||||||
RepoError, SettingsRepo,
|
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 {
|
macro_rules! b {
|
||||||
($self:ident.$ident:ident, $expr:expr) => {{
|
($self:ident.$ident:ident, $expr:expr) => {{
|
||||||
let $ident = $self.$ident.clone();
|
let $ident = $self.$ident.clone();
|
||||||
|
@ -63,7 +79,7 @@ pub(crate) struct OldDetails {
|
||||||
height: u16,
|
height: u16,
|
||||||
frames: Option<u32>,
|
frames: Option<u32>,
|
||||||
content_type: crate::serde_str::Serde<mime::Mime>,
|
content_type: crate::serde_str::Serde<mime::Mime>,
|
||||||
created_at: crate::details::MaybeHumanDate,
|
created_at: MaybeHumanDate,
|
||||||
#[serde(skip_serializing_if = "Option::is_none")]
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
format: Option<crate::formats::InternalFormat>,
|
format: Option<crate::formats::InternalFormat>,
|
||||||
}
|
}
|
||||||
|
@ -87,7 +103,11 @@ impl OldDetails {
|
||||||
})?;
|
})?;
|
||||||
|
|
||||||
Some(Details::from_parts_full(
|
Some(Details::from_parts_full(
|
||||||
format, width, height, frames, created_at,
|
format,
|
||||||
|
width,
|
||||||
|
height,
|
||||||
|
frames,
|
||||||
|
created_at.into_human_date(),
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue