-
Notifications
You must be signed in to change notification settings - Fork 16
/
serialize_continue_watching_preview.rs
119 lines (106 loc) · 3.98 KB
/
serialize_continue_watching_preview.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
use wasm_bindgen::JsValue;
use stremio_core::models::continue_watching_preview::ContinueWatchingPreview;
pub fn serialize_continue_watching_preview(
continue_watching_preview: &ContinueWatchingPreview,
) -> JsValue {
JsValue::from_serde(&model::ContinueWatchingPreview::from(
continue_watching_preview,
))
.unwrap()
}
mod model {
use serde::Serialize;
use url::Url;
use stremio_core::{
deep_links::{LibraryDeepLinks, LibraryItemDeepLinks},
types::resource::PosterShape,
};
use crate::model::deep_links_ext::DeepLinksExt;
#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
pub struct ContinueWatchingPreview<'a> {
pub items: Vec<Item<'a>>,
pub deep_links: LibraryDeepLinks,
}
impl<'a> From<&'a stremio_core::models::continue_watching_preview::ContinueWatchingPreview>
for ContinueWatchingPreview<'a>
{
fn from(
continue_watching_preview: &'a stremio_core::models::continue_watching_preview::ContinueWatchingPreview,
) -> Self {
Self {
items: continue_watching_preview
.items
.iter()
.map(Item::from)
.collect::<Vec<_>>(),
deep_links: LibraryDeepLinks::from(&"continuewatching".to_owned())
.into_web_deep_links(),
}
}
}
#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
pub struct Item<'a> {
#[serde(flatten)]
library_item: LibraryItem<'a>,
/// a count of the total notifications we have for this item
notifications: usize,
}
impl<'a> From<&'a stremio_core::models::continue_watching_preview::Item> for Item<'a> {
fn from(item: &'a stremio_core::models::continue_watching_preview::Item) -> Self {
Self {
library_item: LibraryItem::from(&item.library_item),
notifications: item.notifications,
}
}
}
#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
pub struct LibraryItem<'a> {
#[serde(rename = "_id")]
pub id: &'a String,
pub name: &'a String,
pub r#type: &'a String,
pub poster: &'a Option<Url>,
pub poster_shape: &'a PosterShape,
pub progress: f64,
pub deep_links: LibraryItemDeepLinks,
pub state: LibraryItemState<'a>,
}
impl<'a> From<&'a stremio_core::types::library::LibraryItem> for LibraryItem<'a> {
fn from(library_item: &'a stremio_core::types::library::LibraryItem) -> Self {
LibraryItem {
id: &library_item.id,
name: &library_item.name,
r#type: &library_item.r#type,
poster: &library_item.poster,
poster_shape: match library_item.poster_shape {
// override poster shape if it's Landscape to over be a Square.
PosterShape::Landscape => &PosterShape::Square,
// else use the provided shape
_ => &library_item.poster_shape,
},
progress: if library_item.state.time_offset > 0 && library_item.state.duration > 0 {
library_item.state.time_offset as f64 / library_item.state.duration as f64
} else {
0.0
},
deep_links: LibraryItemDeepLinks::from(library_item).into_web_deep_links(),
state: LibraryItemState::from(&library_item.state),
}
}
}
#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
pub struct LibraryItemState<'a> {
pub video_id: Option<&'a String>,
}
impl<'a> From<&'a stremio_core::types::library::LibraryItemState> for LibraryItemState<'a> {
fn from(state: &'a stremio_core::types::library::LibraryItemState) -> Self {
Self {
video_id: state.video_id.as_ref(),
}
}
}
}