Skip to content

Commit

Permalink
Merge branch 'notifications-bucket-part-2' into continue-watching-part-2
Browse files Browse the repository at this point in the history
  • Loading branch information
elpiel committed Jul 27, 2023
2 parents 54b7128 + 20a7736 commit 426b861
Showing 1 changed file with 42 additions and 21 deletions.
63 changes: 42 additions & 21 deletions src/types/addon/response.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
use crate::types::addon::DescriptorPreview;
use crate::types::resource::{MetaItem, MetaItemPreview, Stream, Subtitles};
use derive_more::TryInto;
use serde::de::Deserializer;
use serde::{Deserialize, Serialize};
use serde::{de::Deserializer, Deserialize, Serialize};
use serde_with::{serde_as, VecSkipError};

#[derive(Clone, TryInto, Serialize, Debug)]
#[cfg_attr(test, derive(PartialEq, Eq))]
use crate::types::{
addon::DescriptorPreview,
resource::{MetaItem, MetaItemPreview, Stream, Subtitles},
};


/// Resource Response from an addon.
///
/// Deserializing the struct from json will skip any invalid Vec items
/// and will skip any unknown to the variants fields.
#[derive(Clone, TryInto, Serialize, Debug, PartialEq, Eq)]
#[serde(untagged)]
#[serde_as]
pub enum ResourceResponse {
Metas {
metas: Vec<MetaItemPreview>,
Expand All @@ -29,6 +37,13 @@ pub enum ResourceResponse {
},
}

#[serde_as]
#[derive(Clone, Serialize, Deserialize, Debug)]
#[serde(transparent)]
struct SkipError<T: for<'a> Deserialize<'a> + Serialize>(
#[serde_as(as = "VecSkipError<_>")] Vec<T>,
);

impl<'de> Deserialize<'de> for ResourceResponse {
fn deserialize<D>(deserializer: D) -> Result<ResourceResponse, D::Error>
where
Expand All @@ -43,32 +58,38 @@ impl<'de> Deserialize<'de> for ResourceResponse {
))
}
};

if let Some(value) = value.get_mut("metas") {
Ok(ResourceResponse::Metas {
metas: serde_json::from_value(value.take()).map_err(serde::de::Error::custom)?,
})
let skip = serde_json::from_value::<SkipError<_>>(value.take())
.map_err(serde::de::Error::custom)?;

Ok(ResourceResponse::Metas { metas: skip.0 })
} else if let Some(value) = value.get_mut("metasDetailed") {
let skip = serde_json::from_value::<SkipError<_>>(value.take())
.map_err(serde::de::Error::custom)?;

Ok(ResourceResponse::MetasDetailed {
metas_detailed: serde_json::from_value(value.take())
.map_err(serde::de::Error::custom)?,
metas_detailed: skip.0,
})
} else if let Some(value) = value.get_mut("meta") {
Ok(ResourceResponse::Meta {
meta: serde_json::from_value(value.take()).map_err(serde::de::Error::custom)?,
})
} else if let Some(value) = value.get_mut("streams") {
Ok(ResourceResponse::Streams {
streams: serde_json::from_value(value.take()).map_err(serde::de::Error::custom)?,
})
let skip = serde_json::from_value::<SkipError<_>>(value.take())
.map_err(serde::de::Error::custom)?;

Ok(ResourceResponse::Streams { streams: skip.0 })
} else if let Some(value) = value.get_mut("subtitles") {
Ok(ResourceResponse::Subtitles {
subtitles: serde_json::from_value(value.take())
.map_err(serde::de::Error::custom)?,
})
let skip = serde_json::from_value::<SkipError<_>>(value.take())
.map_err(serde::de::Error::custom)?;

Ok(ResourceResponse::Subtitles { subtitles: skip.0 })
} else if let Some(value) = value.get_mut("addons") {
Ok(ResourceResponse::Addons {
addons: serde_json::from_value(value.take()).map_err(serde::de::Error::custom)?,
})
let skip = serde_json::from_value::<SkipError<_>>(value.take())
.map_err(serde::de::Error::custom)?;

Ok(ResourceResponse::Addons { addons: skip.0 })
} else {
Err(serde::de::Error::custom(
"Cannot deserialize as ResourceResponse",
Expand Down

0 comments on commit 426b861

Please sign in to comment.