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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
pub mod filename;
pub mod mock;
pub mod tmdb;
use async_trait::async_trait;
use std::fmt::Debug;
use std::sync::Arc;
use std::time::Duration;
use displaydoc::Display;
use serde::Deserialize;
use serde::Serialize;
use thiserror::Error;
pub type Result<T> = ::core::result::Result<T, Error>;
#[derive(Clone, Display, Debug, Error, Serialize)]
pub enum Error {
Timeout,
ReachedMaxTries,
DeserializationError { body: Arc<str>, error: String },
NoResults { query: String, year: Option<i32> },
NoSeasonsFound { id: u64 },
NoEpisodesFound { id: u64, season: u64 },
NoGenreFound { id: u64 },
OtherError(#[serde(skip)] Arc<dyn std::error::Error + Send + Sync + 'static>),
RemoteApiError { code: u16, message: String },
}
impl Error {
pub fn other(error: impl std::error::Error + Send + Sync + 'static) -> Self {
let err = Arc::new(error);
Self::OtherError(err)
}
}
#[derive(Default, Clone, Debug, Serialize, Deserialize, PartialEq, PartialOrd)]
pub struct ExternalMedia {
pub external_id: String,
pub title: String,
pub description: Option<String>,
#[serde(with = "chrono::serde::ts_seconds_option")]
pub release_date: Option<chrono::DateTime<chrono::Utc>>,
pub posters: Vec<String>,
pub backdrops: Vec<String>,
pub genres: Vec<String>,
pub rating: Option<f64>,
pub duration: Option<Duration>,
}
#[derive(Clone, Default, Debug, Serialize, Deserialize, PartialEq, Eq, PartialOrd)]
pub struct ExternalSeason {
pub external_id: String,
pub title: Option<String>,
pub description: Option<String>,
pub posters: Vec<String>,
pub season_number: u64,
}
#[derive(Clone, Default, Debug, Serialize, Deserialize, PartialEq, Eq, PartialOrd)]
pub struct ExternalEpisode {
pub external_id: String,
pub title: Option<String>,
pub description: Option<String>,
pub episode_number: u64,
pub stills: Vec<String>,
pub duration: Option<Duration>,
}
impl ExternalEpisode {
pub fn title_or_episode(&self) -> String {
self.title
.clone()
.unwrap_or_else(|| self.episode_number.to_string())
}
}
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq, PartialOrd)]
pub struct ExternalActor {
pub external_id: String,
pub name: String,
pub profile_path: Option<String>,
pub character: String,
}
#[derive(Clone, Copy, Debug, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum MediaSearchType {
Movie,
Tv,
}
impl std::fmt::Display for MediaSearchType {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
MediaSearchType::Movie => write!(f, "movie"),
MediaSearchType::Tv => write!(f, "tv"),
}
}
}
pub trait ExternalQueryIntoShow: IntoQueryShow + ExternalQuery {}
#[async_trait]
pub trait ExternalQuery: Debug + Send + Sync {
async fn search(&self, title: &str, year: Option<i32>) -> Result<Vec<ExternalMedia>>;
async fn search_by_id(&self, external_id: &str) -> Result<ExternalMedia>;
async fn cast(&self, external_id: &str) -> Result<Vec<ExternalActor>>;
}
pub trait IntoQueryShow {
fn as_query_show<'a>(&'a self) -> Option<&'a dyn ExternalQueryShow> {
None
}
fn into_query_show(self: Arc<Self>) -> Option<Arc<dyn ExternalQueryShow>> {
None
}
}
#[async_trait]
pub trait ExternalQueryShow: ExternalQuery {
async fn seasons_for_id(&self, external_id: &str) -> Result<Vec<ExternalSeason>>;
async fn episodes_for_season(
&self,
external_id: &str,
season_number: u64,
) -> Result<Vec<ExternalEpisode>>;
}