Handle float in duration

This commit is contained in:
blank X 2021-06-23 19:20:26 +07:00
parent 696415770e
commit 2524833326
Signed by: blankie
GPG Key ID: CC15FC822C7F61F5
1 changed files with 122 additions and 1 deletions

View File

@ -1,5 +1,8 @@
use serde::Deserialize; use serde::de::{self, Visitor};
use serde::{Deserialize, Deserializer};
use std::fmt;
use std::io; use std::io;
use std::marker::PhantomData;
use std::process::ExitStatus; use std::process::ExitStatus;
use std::string::FromUtf8Error; use std::string::FromUtf8Error;
use tokio::task::JoinError; use tokio::task::JoinError;
@ -27,6 +30,7 @@ pub struct VideoData {
pub requested_formats: Vec<VideoURL>, pub requested_formats: Vec<VideoURL>,
#[serde(default)] #[serde(default)]
pub url: Option<Url>, pub url: Option<Url>,
#[serde(deserialize_with = "convert_to_u64")]
pub duration: u64, pub duration: u64,
pub id: String, pub id: String,
pub title: String, pub title: String,
@ -96,3 +100,120 @@ impl From<FromUtf8Error> for Error {
Error::FromUtf8Error(error) Error::FromUtf8Error(error)
} }
} }
fn convert_to_u64<'de, D>(deserializer: D) -> std::result::Result<u64, D::Error>
where
D: Deserializer<'de>,
{
struct ConvertToU64<T>(PhantomData<fn() -> T>);
impl<'de> Visitor<'de> for ConvertToU64<u64> {
type Value = u64;
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_str("an integer between 0 and 2^63")
}
fn visit_i8<E>(self, value: i8) -> std::result::Result<Self::Value, E>
where
E: de::Error,
{
use std::u64;
if value >= 0 {
Ok(value as u64)
} else {
Err(E::custom(format!("u64 out of range: {}", value)))
}
}
fn visit_i16<E>(self, value: i16) -> std::result::Result<Self::Value, E>
where
E: de::Error,
{
use std::u64;
if value >= 0 {
Ok(value as u64)
} else {
Err(E::custom(format!("u64 out of range: {}", value)))
}
}
fn visit_i32<E>(self, value: i32) -> std::result::Result<Self::Value, E>
where
E: de::Error,
{
use std::u64;
if value >= 0 {
Ok(value as u64)
} else {
Err(E::custom(format!("u64 out of range: {}", value)))
}
}
fn visit_i64<E>(self, value: i64) -> std::result::Result<Self::Value, E>
where
E: de::Error,
{
use std::u64;
if value >= 0 {
Ok(value as u64)
} else {
Err(E::custom(format!("u64 out of range: {}", value)))
}
}
fn visit_u8<E>(self, value: u8) -> std::result::Result<Self::Value, E>
where
E: de::Error,
{
Ok(u64::from(value))
}
fn visit_u16<E>(self, value: u16) -> std::result::Result<Self::Value, E>
where
E: de::Error,
{
Ok(u64::from(value))
}
fn visit_u32<E>(self, value: u32) -> std::result::Result<Self::Value, E>
where
E: de::Error,
{
Ok(u64::from(value))
}
fn visit_u64<E>(self, value: u64) -> std::result::Result<Self::Value, E>
where
E: de::Error,
{
Ok(value)
}
fn visit_f32<E>(self, value: f32) -> std::result::Result<Self::Value, E>
where
E: de::Error,
{
let value = value.ceil();
if value >= 0.0 {
Ok(value as u64)
} else {
Err(E::custom(format!("u64 out of range: {}", value)))
}
}
fn visit_f64<E>(self, value: f64) -> std::result::Result<Self::Value, E>
where
E: de::Error,
{
let value = value.ceil();
if value >= 0.0 {
Ok(value as u64)
} else {
Err(E::custom(format!("u64 out of range: {}", value)))
}
}
}
deserializer.deserialize_any(ConvertToU64(PhantomData))
}