feat: use official date serializer, fix test

This commit is contained in:
ThetaDev 2023-12-06 21:53:35 +01:00
parent cf24f978f2
commit deeffacc1c
No known key found for this signature in database
GPG key ID: 649CA4EBDC338394
5 changed files with 4 additions and 60 deletions

View file

@ -12,11 +12,10 @@ pub use frameset::{Frameset, FramesetUrls};
use std::{collections::BTreeSet, ops::Range};
use serde::{Deserialize, Serialize};
use serde_with::serde_as;
use time::{Date, OffsetDateTime};
use self::{paginator::Paginator, richtext::RichText};
use crate::{error::Error, param::Country, serializer::DateYmd, validate};
use crate::{error::Error, param::Country, validate};
/*
#COMMON
@ -501,7 +500,6 @@ pub struct Subtitle {
*/
/// YouTube playlist
#[serde_as]
#[derive(Clone, Debug, Serialize, Deserialize)]
#[non_exhaustive]
pub struct Playlist {
@ -520,7 +518,6 @@ pub struct Playlist {
/// Channel of the playlist
pub channel: Option<ChannelId>,
/// Last update date
#[serde_as(as = "Option<DateYmd>")]
pub last_update: Option<Date>,
/// Textual last update date
pub last_update_txt: Option<String>,
@ -739,7 +736,6 @@ pub struct Channel<T> {
}
/// Detailed channel information
#[serde_as]
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[non_exhaustive]
pub struct ChannelInfo {
@ -757,7 +753,6 @@ pub struct ChannelInfo {
/// Channel video count
pub video_count: Option<u64>,
/// Channel creation date
#[serde_as(as = "Option<DateYmd>")]
pub create_date: Option<Date>,
/// Channel view count
pub view_count: Option<u64>,

View file

@ -1,50 +0,0 @@
use serde::{
de::{self, Visitor},
ser, Serialize,
};
use serde_with::{DeserializeAs, SerializeAs};
use time::{macros::format_description, Date};
const YMD_FORMAT: &[time::format_description::FormatItem] =
format_description!("[year]-[month]-[day]");
pub struct DateYmd;
impl SerializeAs<Date> for DateYmd {
fn serialize_as<S>(date: &Date, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
date.format(YMD_FORMAT)
.map_err(ser::Error::custom)?
.serialize(serializer)
}
}
impl<'de> DeserializeAs<'de, Date> for DateYmd {
fn deserialize_as<D>(deserializer: D) -> Result<Date, D::Error>
where
D: serde::Deserializer<'de>,
{
struct DateYmdVisitor;
impl<'de> Visitor<'de> for DateYmdVisitor {
type Value = Date;
fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
formatter.write_str("a YYYY-MM-DD formatted date")
}
fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
where
E: serde::de::Error,
{
Date::parse(v, YMD_FORMAT).map_err(|_| {
de::Error::invalid_value(de::Unexpected::Str(v), &"a YYYY-MM-DD formatted date")
})
}
}
deserializer.deserialize_str(DateYmdVisitor)
}
}

View file

@ -1,10 +1,8 @@
pub mod text;
mod date;
mod range;
mod vec_log_err;
pub use date::DateYmd;
pub use range::Range;
pub use vec_log_err::VecSkipErrorWrap;