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

@ -5,6 +5,7 @@ edition = "2021"
authors = ["ThetaDev <thetadev@magenta.de>"] authors = ["ThetaDev <thetadev@magenta.de>"]
license = "GPL-3.0" license = "GPL-3.0"
description = "Client for the public YouTube / YouTube Music API (Innertube), inspired by NewPipe" description = "Client for the public YouTube / YouTube Music API (Innertube), inspired by NewPipe"
repository = "https://code.thetadev.de/ThetaDev/rustypipe"
keywords = ["youtube", "video", "music"] keywords = ["youtube", "video", "music"]
categories = ["api-bindings", "multimedia"] categories = ["api-bindings", "multimedia"]
@ -51,7 +52,7 @@ serde_plain = "1.0.1"
rand = "0.8.5" rand = "0.8.5"
time = { version = "0.3.15", features = [ time = { version = "0.3.15", features = [
"macros", "macros",
"serde", "serde-human-readable",
"serde-well-known", "serde-well-known",
] } ] }
futures = "0.3.21" futures = "0.3.21"

View file

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

View file

@ -754,7 +754,7 @@ fn get_video_details_no_desc(rp: RustyPipe) {
assert_eq!(details.id, "VYJNSQ_ANyA"); assert_eq!(details.id, "VYJNSQ_ANyA");
assert_eq!(details.name, "Cricket comedy by Modi"); assert_eq!(details.name, "Cricket comedy by Modi");
assert_eq!(details.channel.id, "UC8gBy2lByHxIyoPMglerNWg"); assert_eq!(details.channel.id, "UC8gBy2lByHxIyoPMglerNWg");
assert_eq!(details.channel.name, "TMP VIBES"); assert_eq!(details.channel.name, "TMP Shorts");
assert!(!details.channel.avatar.is_empty(), "no channel avatars"); assert!(!details.channel.avatar.is_empty(), "no channel avatars");
assert!( assert!(