moved datetime functions to utils dir
This commit is contained in:
parent
1c461ddb9d
commit
15a203acf2
@ -1,5 +1,4 @@
|
|||||||
use super::posts::{deserialize_datetime, serialize_datetime};
|
use crate::{datasources::comments::CommentsDatasource, state::AppState, utils::datetime::*};
|
||||||
use crate::{datasources::comments::CommentsDatasource, state::AppState};
|
|
||||||
use axum::{
|
use axum::{
|
||||||
extract::{Path, State},
|
extract::{Path, State},
|
||||||
http::StatusCode,
|
http::StatusCode,
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
use std::collections::HashMap;
|
use crate::{
|
||||||
use std::fmt;
|
datasources::posts::PostsDatasource,
|
||||||
|
state::AppState,
|
||||||
use crate::utils::rss;
|
utils::{datetime::*, rss},
|
||||||
use crate::{datasources::posts::PostsDatasource, state::AppState};
|
};
|
||||||
use axum::http::{HeaderMap, HeaderValue};
|
use axum::http::{HeaderMap, HeaderValue};
|
||||||
use axum::{
|
use axum::{
|
||||||
extract::{Path, State},
|
extract::{Path, State},
|
||||||
@ -13,7 +13,8 @@ use axum::{
|
|||||||
};
|
};
|
||||||
use chrono::Utc;
|
use chrono::Utc;
|
||||||
use fred::types::Expiration;
|
use fred::types::Expiration;
|
||||||
use serde::{Deserialize, Deserializer, Serialize, Serializer};
|
use serde::{Deserialize, Serialize};
|
||||||
|
use std::collections::HashMap;
|
||||||
|
|
||||||
#[derive(sqlx::FromRow, Deserialize, Serialize, Debug, Clone)]
|
#[derive(sqlx::FromRow, Deserialize, Serialize, Debug, Clone)]
|
||||||
pub struct Post {
|
pub struct Post {
|
||||||
@ -359,56 +360,3 @@ impl PostsRoute {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn serialize_datetime<S>(
|
|
||||||
date: &Option<chrono::DateTime<Utc>>,
|
|
||||||
serializer: S,
|
|
||||||
) -> Result<S::Ok, S::Error>
|
|
||||||
where
|
|
||||||
S: Serializer,
|
|
||||||
{
|
|
||||||
serializer.serialize_str(&date.unwrap().to_rfc3339())
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn deserialize_datetime<'de, D>(
|
|
||||||
deserializer: D,
|
|
||||||
) -> Result<Option<chrono::DateTime<Utc>>, D::Error>
|
|
||||||
where
|
|
||||||
D: Deserializer<'de>,
|
|
||||||
{
|
|
||||||
struct DateTimeVisitor;
|
|
||||||
|
|
||||||
impl<'de> serde::de::Visitor<'de> for DateTimeVisitor {
|
|
||||||
type Value = Option<chrono::DateTime<Utc>>;
|
|
||||||
|
|
||||||
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
|
|
||||||
formatter.write_str("an ISO 8601 formatted date string or null")
|
|
||||||
}
|
|
||||||
|
|
||||||
fn visit_str<E>(self, value: &str) -> Result<Self::Value, E>
|
|
||||||
where
|
|
||||||
E: serde::de::Error,
|
|
||||||
{
|
|
||||||
match chrono::DateTime::parse_from_rfc3339(value) {
|
|
||||||
Ok(dt) => Ok(Some(dt.with_timezone(&Utc))),
|
|
||||||
Err(e) => Err(E::custom(format!("Failed to parse datetime: {}", e))),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn visit_none<E>(self) -> Result<Self::Value, E>
|
|
||||||
where
|
|
||||||
E: serde::de::Error,
|
|
||||||
{
|
|
||||||
Ok(None)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn visit_some<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
|
|
||||||
where
|
|
||||||
D: Deserializer<'de>,
|
|
||||||
{
|
|
||||||
deserializer.deserialize_str(self)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
deserializer.deserialize_option(DateTimeVisitor)
|
|
||||||
}
|
|
||||||
|
56
backend/public/src/utils/datetime.rs
Normal file
56
backend/public/src/utils/datetime.rs
Normal file
@ -0,0 +1,56 @@
|
|||||||
|
use chrono::Utc;
|
||||||
|
use serde::{Deserializer, Serializer};
|
||||||
|
use std::fmt;
|
||||||
|
|
||||||
|
pub fn serialize_datetime<S>(
|
||||||
|
date: &Option<chrono::DateTime<Utc>>,
|
||||||
|
serializer: S,
|
||||||
|
) -> Result<S::Ok, S::Error>
|
||||||
|
where
|
||||||
|
S: Serializer,
|
||||||
|
{
|
||||||
|
serializer.serialize_str(&date.unwrap().to_rfc3339())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn deserialize_datetime<'de, D>(
|
||||||
|
deserializer: D,
|
||||||
|
) -> Result<Option<chrono::DateTime<Utc>>, D::Error>
|
||||||
|
where
|
||||||
|
D: Deserializer<'de>,
|
||||||
|
{
|
||||||
|
struct DateTimeVisitor;
|
||||||
|
|
||||||
|
impl<'de> serde::de::Visitor<'de> for DateTimeVisitor {
|
||||||
|
type Value = Option<chrono::DateTime<Utc>>;
|
||||||
|
|
||||||
|
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
|
||||||
|
formatter.write_str("an ISO 8601 formatted date string or null")
|
||||||
|
}
|
||||||
|
|
||||||
|
fn visit_str<E>(self, value: &str) -> Result<Self::Value, E>
|
||||||
|
where
|
||||||
|
E: serde::de::Error,
|
||||||
|
{
|
||||||
|
match chrono::DateTime::parse_from_rfc3339(value) {
|
||||||
|
Ok(dt) => Ok(Some(dt.with_timezone(&Utc))),
|
||||||
|
Err(e) => Err(E::custom(format!("Failed to parse datetime: {}", e))),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn visit_none<E>(self) -> Result<Self::Value, E>
|
||||||
|
where
|
||||||
|
E: serde::de::Error,
|
||||||
|
{
|
||||||
|
Ok(None)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn visit_some<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
|
||||||
|
where
|
||||||
|
D: Deserializer<'de>,
|
||||||
|
{
|
||||||
|
deserializer.deserialize_str(self)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
deserializer.deserialize_option(DateTimeVisitor)
|
||||||
|
}
|
@ -1 +1,2 @@
|
|||||||
|
pub mod datetime;
|
||||||
pub mod rss;
|
pub mod rss;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user