Compare commits

..

No commits in common. "9a652d851fa962183e60b11cbb4e34477a979aac" and "e0124894732a9dd3bd6390b54cb3e087d05dce51" have entirely different histories.

2 changed files with 20 additions and 14 deletions

View file

@ -3,7 +3,7 @@ use std::convert::TryFrom;
use once_cell::sync::Lazy; use once_cell::sync::Lazy;
use regex::Regex; use regex::Regex;
use serde::{Deserialize, Deserializer}; use serde::{Deserialize, Deserializer};
use serde_with::{serde_as, DefaultOnError, DeserializeAs, VecSkipError}; use serde_with::{serde_as, DeserializeAs, VecSkipError};
use crate::{ use crate::{
client::response::url_endpoint::{ client::response::url_endpoint::{
@ -122,13 +122,10 @@ struct RichTextInternal {
} }
/// TextLinkRun is a single component from a YouTube text with links /// TextLinkRun is a single component from a YouTube text with links
#[serde_as]
#[derive(Deserialize)] #[derive(Deserialize)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
struct RichTextRun { struct RichTextRun {
text: String, text: String,
#[serde(default)]
#[serde_as(as = "DefaultOnError")]
navigation_endpoint: Option<NavigationEndpoint>, navigation_endpoint: Option<NavigationEndpoint>,
} }

View file

@ -20,6 +20,13 @@ where
where where
D: serde::Deserializer<'de>, D: serde::Deserializer<'de>,
{ {
#[derive(serde::Deserialize)]
#[serde(untagged)]
enum GoodOrError<T> {
Good(T),
Error(serde_json::Value),
}
struct SeqVisitor<T>(PhantomData<T>); struct SeqVisitor<T>(PhantomData<T>);
impl<'de, T> Visitor<'de> for SeqVisitor<T> impl<'de, T> Visitor<'de> for SeqVisitor<T>
@ -39,14 +46,16 @@ where
let mut values = Vec::with_capacity(seq.size_hint().unwrap_or_default()); let mut values = Vec::with_capacity(seq.size_hint().unwrap_or_default());
let mut warnings = Vec::new(); let mut warnings = Vec::new();
loop { while let Some(value) = seq.next_element()? {
match seq.next_element::<T>() { match value {
Ok(val) => match val { GoodOrError::<T>::Good(value) => {
Some(val) => values.push(val), values.push(value);
None => break, }
}, GoodOrError::<T>::Error(value) => {
Err(e) => { warnings.push(format!(
warnings.push(format!("error deserializing item: {e}")); "error deserializing item: {}",
serde_json::to_string(&value).unwrap_or_default()
));
} }
} }
} }
@ -177,8 +186,8 @@ mod tests {
insta::assert_debug_snapshot!(res.items.warnings, @r###" insta::assert_debug_snapshot!(res.items.warnings, @r###"
[ [
"error deserializing item: missing field `name` at line 1 column 40", "error deserializing item: {\"xyz\":\"i2\"}",
"error deserializing item: missing field `name` at line 1 column 73", "error deserializing item: {\"namra\":\"i4\"}",
] ]
"###); "###);
} }