From 5c3a305b6be79b77ee41599bdc74bff060528427 Mon Sep 17 00:00:00 2001 From: Lachezar Lechev Date: Wed, 26 Jun 2024 13:45:53 +0300 Subject: [PATCH] feat: CatalogsWithExtra - serialize model Signed-off-by: Lachezar Lechev --- stremio-core-web/src/model/mod.rs | 2 +- stremio-core-web/src/model/model.rs | 22 +- .../model/serialize_catalogs_with_extra.rs | 193 ++++++++++-------- 3 files changed, 130 insertions(+), 87 deletions(-) diff --git a/stremio-core-web/src/model/mod.rs b/stremio-core-web/src/model/mod.rs index 916c5008a..f1fc4b349 100644 --- a/stremio-core-web/src/model/mod.rs +++ b/stremio-core-web/src/model/mod.rs @@ -1,7 +1,7 @@ pub mod deep_links_ext; mod serialize_catalogs_with_extra; -use serialize_catalogs_with_extra::*; +pub use serialize_catalogs_with_extra::*; mod serialize_continue_watching_preview; use serialize_continue_watching_preview::*; diff --git a/stremio-core-web/src/model/model.rs b/stremio-core-web/src/model/model.rs index 803789866..01e9c32e0 100644 --- a/stremio-core-web/src/model/model.rs +++ b/stremio-core-web/src/model/model.rs @@ -32,13 +32,15 @@ use stremio_core::{ use crate::{ env::WebEnv, model::{ - serialize_catalogs_with_extra, serialize_continue_watching_preview, serialize_ctx, - serialize_data_export, serialize_discover, serialize_installed_addons, serialize_library, - serialize_local_search, serialize_meta_details, serialize_player, serialize_remote_addons, + serialize_continue_watching_preview, serialize_ctx, serialize_data_export, + serialize_discover, serialize_installed_addons, serialize_library, serialize_local_search, + serialize_meta_details, serialize_player, serialize_remote_addons, serialize_streaming_server, }, }; +use super::SerializeModel; + #[derive(Model, Clone)] #[cfg_attr(debug_assertions, derive(Serialize))] #[model(WebEnv)] @@ -133,7 +135,12 @@ impl WebModel { self.streaming_server.base_url.as_ref(), &self.ctx.profile.settings, ), - WebModelField::Board => serialize_catalogs_with_extra(&self.board, &self.ctx), + WebModelField::Board => { + // let old = serialize_catalogs_with_extra(&self.board, &self.ctx); + crate::model::CatalogsWithExtra::new(&self.board, &self.ctx) + .serialize_model() + .expect("JsValue from model::CatalogsWithExtra") + } WebModelField::Discover => { serialize_discover(&self.discover, &self.ctx, &self.streaming_server) } @@ -149,7 +156,12 @@ impl WebModel { self.streaming_server.base_url.as_ref(), "continuewatching".to_owned(), ), - WebModelField::Search => serialize_catalogs_with_extra(&self.search, &self.ctx), + WebModelField::Search => { + // let old = serialize_catalogs_with_extra(&self.search, &self.ctx) + crate::model::CatalogsWithExtra::new(&self.search, &self.ctx) + .serialize_model() + .expect("JsValue from model::CatalogsWithExtra") + } WebModelField::LocalSearch => serialize_local_search(&self.local_search), WebModelField::MetaDetails => { serialize_meta_details(&self.meta_details, &self.ctx, &self.streaming_server) diff --git a/stremio-core-web/src/model/serialize_catalogs_with_extra.rs b/stremio-core-web/src/model/serialize_catalogs_with_extra.rs index 961790245..a58ab3d3b 100644 --- a/stremio-core-web/src/model/serialize_catalogs_with_extra.rs +++ b/stremio-core-web/src/model/serialize_catalogs_with_extra.rs @@ -1,84 +1,82 @@ -use crate::model::deep_links_ext::DeepLinksExt; use gloo_utils::format::JsValueSerdeExt; use itertools::Itertools; use serde::Serialize; -use stremio_core::deep_links::{DiscoverDeepLinks, MetaItemDeepLinks}; -use stremio_core::models::catalogs_with_extra::{CatalogsWithExtra, Selected}; -use stremio_core::models::common::Loadable; -use stremio_core::models::ctx::Ctx; -use stremio_core::types::resource::PosterShape; use wasm_bindgen::JsValue; -mod model { +use crate::model::deep_links_ext::DeepLinksExt; - use super::*; - #[derive(Serialize)] - #[serde(rename_all = "camelCase")] - pub struct ManifestPreview<'a> { - pub id: &'a str, - pub name: &'a str, - } - #[derive(Serialize)] - #[serde(rename_all = "camelCase")] - pub struct DescriptorPreview<'a> { - pub manifest: ManifestPreview<'a>, - } - #[derive(Serialize)] - #[serde(rename_all = "camelCase")] - pub struct MetaItemPreview<'a> { - #[serde(flatten)] - pub meta_item: &'a stremio_core::types::resource::MetaItemPreview, - pub poster_shape: &'a PosterShape, - pub watched: bool, - pub deep_links: MetaItemDeepLinks, - } - #[derive(Serialize)] - #[serde(rename_all = "camelCase")] - pub struct ResourceLoadable<'a> { - pub id: String, - pub name: String, - pub r#type: String, - pub addon: DescriptorPreview<'a>, - pub content: Option>, String>>, - pub deep_links: DiscoverDeepLinks, - } - #[derive(Serialize)] - #[serde(rename_all = "camelCase")] - pub struct CatalogsWithExtra<'a> { - pub selected: &'a Option, - pub catalogs: Vec>, - } +pub use stremio_core::{ + deep_links::{DiscoverDeepLinks, MetaItemDeepLinks}, + models::{catalogs_with_extra::Selected, common::Loadable, ctx::Ctx}, + types::resource::PosterShape, +}; + +#[derive(Serialize)] +#[serde(rename_all = "camelCase")] +pub struct ManifestPreview<'a> { + pub id: &'a str, + pub name: &'a str, +} +#[derive(Serialize)] +#[serde(rename_all = "camelCase")] +pub struct DescriptorPreview<'a> { + pub manifest: ManifestPreview<'a>, +} +#[derive(Serialize)] +#[serde(rename_all = "camelCase")] +pub struct MetaItemPreview<'a> { + #[serde(flatten)] + pub meta_item: &'a stremio_core::types::resource::MetaItemPreview, + pub poster_shape: &'a PosterShape, + pub watched: bool, + pub deep_links: MetaItemDeepLinks, +} +#[derive(Serialize)] +#[serde(rename_all = "camelCase")] +pub struct ResourceLoadable<'a> { + pub id: String, + pub name: String, + pub r#type: String, + pub addon: DescriptorPreview<'a>, + pub content: Option>, String>>, + pub deep_links: DiscoverDeepLinks, +} +#[derive(Serialize)] +#[serde(rename_all = "camelCase")] +pub struct CatalogsWithExtra<'a> { + pub selected: &'a Option, + pub catalogs: Vec>, } -pub fn serialize_catalogs_with_extra( - catalogs_with_extra: &CatalogsWithExtra, - ctx: &Ctx, -) -> JsValue { - ::from_serde(&model::CatalogsWithExtra { - selected: &catalogs_with_extra.selected, - catalogs: catalogs_with_extra - .catalogs - .iter() - .filter_map(|catalog| catalog.first()) - .filter_map(|catalog| { - ctx.profile - .addons - .iter() - .find(|addon| addon.transport_url == catalog.request.base) - .and_then(|addon| { - addon - .manifest - .catalogs - .iter() - .find(|manifest_catalog| { - manifest_catalog.id == catalog.request.path.id - && manifest_catalog.r#type == catalog.request.path.r#type - }) - .map(|manifest_catalog| (addon, manifest_catalog, catalog)) - }) - }) - .map( - |(addon, manifest_catalog, catalog)| model::ResourceLoadable { +impl<'a> CatalogsWithExtra<'a> { + pub fn new( + catalogs_with_extra: &'a stremio_core::models::catalogs_with_extra::CatalogsWithExtra, + ctx: &'a Ctx, + ) -> Self { + Self { + selected: &catalogs_with_extra.selected, + catalogs: catalogs_with_extra + .catalogs + .iter() + .filter_map(|catalog| catalog.first()) + .filter_map(|catalog| { + ctx.profile + .addons + .iter() + .find(|addon| addon.transport_url == catalog.request.base) + .and_then(|addon| { + addon + .manifest + .catalogs + .iter() + .find(|manifest_catalog| { + manifest_catalog.id == catalog.request.path.id + && manifest_catalog.r#type == catalog.request.path.r#type + }) + .map(|manifest_catalog| (addon, manifest_catalog, catalog)) + }) + }) + .map(|(addon, manifest_catalog, catalog)| ResourceLoadable { id: manifest_catalog.id.to_string(), name: manifest_catalog .name @@ -86,8 +84,8 @@ pub fn serialize_catalogs_with_extra( .unwrap_or(&addon.manifest.name) .to_string(), r#type: manifest_catalog.r#type.to_string(), - addon: model::DescriptorPreview { - manifest: model::ManifestPreview { + addon: DescriptorPreview { + manifest: ManifestPreview { id: &addon.manifest.id, name: &addon.manifest.name, }, @@ -101,7 +99,7 @@ pub fn serialize_catalogs_with_extra( .iter() .unique_by(|meta_item| &meta_item.id) .take(10) - .map(|meta_item| model::MetaItemPreview { + .map(|meta_item| crate::model::MetaItemPreview { meta_item, poster_shape: poster_shape .unwrap_or(&meta_item.poster_shape), @@ -125,9 +123,42 @@ pub fn serialize_catalogs_with_extra( None => None, }, deep_links: DiscoverDeepLinks::from(&catalog.request).into_web_deep_links(), - }, - ) - .collect::>(), - }) - .expect("JsValue from model::CatalogsWithExtra") + }) + .collect::>(), + } + } +} + +pub struct AppleKotlinStruct {} + +pub trait SerializeModel { + type Error; + + fn serialize_model(&self) -> Result; +} + +impl<'a> SerializeModel for CatalogsWithExtra<'a> { + type Error = serde_json::Error; + // type Out = JsValue; + + fn serialize_model(&self) -> Result { + JsValue::try_from(self) + } +} + +impl<'a> SerializeModel for CatalogsWithExtra<'a> { + type Error = serde_json::Error; + // type Out = JsValue; + + fn serialize_model(&self) -> Result { + Ok(AppleKotlinStruct {}) + } +} + +impl<'a> TryFrom<&CatalogsWithExtra<'a>> for JsValue { + type Error = serde_json::Error; + + fn try_from(catalogs: &CatalogsWithExtra<'a>) -> Result { + ::from_serde(&catalogs) + } }