You've already forked AstralRinth
forked from didirus/AstralRinth
Switch to time crate, add file sizes (#329)
* Switch to time crate, add file sizes * Update deps, adjust pack format * Run formatter, fix clippy
This commit is contained in:
@@ -3,5 +3,6 @@ pub mod env;
|
||||
pub mod ext;
|
||||
pub mod guards;
|
||||
pub mod routes;
|
||||
pub mod time_ser;
|
||||
pub mod validate;
|
||||
pub mod webhook;
|
||||
|
||||
42
src/util/time_ser.rs
Normal file
42
src/util/time_ser.rs
Normal file
@@ -0,0 +1,42 @@
|
||||
//! Use the well-known [RFC3339 format] when serializing and deserializing an [`OffsetDateTime`].
|
||||
//!
|
||||
//! Use this module in combination with serde's [`#[with]`][with] attribute.
|
||||
//!
|
||||
//! [RFC3339 format]: https://tools.ietf.org/html/rfc3339#section-5.6
|
||||
//! [with]: https://serde.rs/field-attrs.html#with
|
||||
|
||||
use core::fmt;
|
||||
use core::marker::PhantomData;
|
||||
|
||||
use serde::{de, Deserializer, Serialize, Serializer};
|
||||
use time::Format::Rfc3339;
|
||||
use time::OffsetDateTime;
|
||||
|
||||
/// Serialize an [`OffsetDateTime`] using the well-known RFC3339 format.
|
||||
pub fn serialize<S: Serializer>(
|
||||
datetime: &OffsetDateTime,
|
||||
serializer: S,
|
||||
) -> Result<S::Ok, S::Error> {
|
||||
datetime.format(Rfc3339).serialize(serializer)
|
||||
}
|
||||
|
||||
/// Deserialize an [`OffsetDateTime`] from its RFC3339 representation.
|
||||
pub fn deserialize<'a, D: Deserializer<'a>>(
|
||||
deserializer: D,
|
||||
) -> Result<OffsetDateTime, D::Error> {
|
||||
deserializer.deserialize_any(Visitor(PhantomData))
|
||||
}
|
||||
|
||||
pub(super) struct Visitor<T: ?Sized>(pub(super) PhantomData<T>);
|
||||
|
||||
impl<'a> de::Visitor<'a> for Visitor<OffsetDateTime> {
|
||||
type Value = OffsetDateTime;
|
||||
|
||||
fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
formatter.write_str("an `OffsetDateTime`")
|
||||
}
|
||||
|
||||
fn visit_str<E: de::Error>(self, value: &str) -> Result<OffsetDateTime, E> {
|
||||
OffsetDateTime::parse(value, Rfc3339).map_err(E::custom)
|
||||
}
|
||||
}
|
||||
@@ -1,13 +1,14 @@
|
||||
use crate::models::projects::Project;
|
||||
use chrono::{DateTime, Utc};
|
||||
use serde::Serialize;
|
||||
use time::OffsetDateTime;
|
||||
|
||||
#[derive(Serialize)]
|
||||
struct DiscordEmbed {
|
||||
pub title: String,
|
||||
pub description: String,
|
||||
pub url: String,
|
||||
pub timestamp: DateTime<Utc>,
|
||||
#[serde(with = "crate::util::time_ser")]
|
||||
pub timestamp: OffsetDateTime,
|
||||
pub color: u32,
|
||||
pub fields: Vec<DiscordEmbedField>,
|
||||
pub image: DiscordEmbedImage,
|
||||
|
||||
Reference in New Issue
Block a user