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
use std::cmp::Ordering;
use std::convert::TryFrom;
#[cfg(test)]
use std::convert::TryInto;
use arrayvec::ArrayVec;
use smartstring::alias::String;
use crate::genre::Genre;
use crate::EpisodeLink;
use crate::Error;
use crate::Title;
use crate::TitleType;
#[derive(Eq, PartialEq)]
pub struct Show {
pub imdb_id: u32,
pub title: String,
pub is_adult: bool,
pub start_year: u16,
pub end_year: Option<u16>,
pub runtime_minutes: Option<u16>,
pub genres: ArrayVec<Genre, 3>,
pub episodes: Vec<Episode>
}
impl std::fmt::Debug for Show {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Show")
.field("imdb_id", &self.imdb_id)
.field("title", &self.title)
.field("is_adult", &self.is_adult)
.field("start_year", &self.start_year)
.field("end_year", &self.end_year)
.field("runtime_minutes", &self.runtime_minutes)
.field("genres", &self.genres)
.field("episodes", &self.episodes.len())
.finish()
}
}
impl Show {
#[cfg(test)]
#[allow(clippy::too_many_arguments)]
pub(crate) fn new(imdb_id: u32, title: &str, is_adult: bool, start_year: u16, end_year: Option<u16>, runtime_minutes: Option<u16>, genres: &[Genre], episodes: &[Episode]) -> Self {
Self{
imdb_id,
title: String::from(title),
is_adult,
start_year,
end_year,
runtime_minutes,
genres: genres.try_into().unwrap(),
episodes: episodes.to_vec()
}
}
pub(crate) fn from_wrapped_title(input: Result<Title, Error>) -> Result<Self, Error> {
Self::try_from(input?)
}
}
impl TryFrom<Title> for Show {
type Error = Error;
fn try_from(input: Title) -> Result<Self, Error> {
match input.title_type {
TitleType::TVSeries => Ok(Self{
imdb_id: input.imdb_id,
title: input.primary_title,
is_adult: input.is_adult,
start_year: match input.start_year {
Some(v) => v,
None => return Err(Error::YearMissing)
},
end_year: input.end_year,
runtime_minutes: input.runtime_minutes,
genres: input.genres,
episodes: Vec::new()
}),
_ => Err(Error::WrongMediaType(input.title_type.into(), "Show"))
}
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Episode {
pub season: u16,
pub episode: u16,
pub imdb_id: u32,
pub title: String,
pub year: Option<u16>,
pub runtime_minutes: Option<u16>
}
impl PartialOrd for Episode {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl Ord for Episode {
fn cmp(&self, other: &Self) -> Ordering {
match self.season.cmp(&other.season) {
Ordering::Equal => self.episode.cmp(&other.episode),
v => v
}
}
}
impl Episode {
#[cfg(test)]
pub(crate) fn new(season: u16, episode: u16, imdb_id: u32, title: &str, year: Option<u16>, runtime_minutes: Option<u16>) -> Self {
Self{
season,
episode,
imdb_id,
title: String::from(title),
year,
runtime_minutes
}
}
pub(crate) fn from_title_and_link(title: Title, link: EpisodeLink) -> Result<Self, Error> {
match title.title_type {
TitleType::Episode => Ok(Self{
season: match link.season {
Some(v) => v,
None => return Err(Error::SeasonMissing)
},
episode: match link.episode {
Some(v) => v,
None => return Err(Error::EpisodeMissing)
},
imdb_id: title.imdb_id,
title: title.primary_title,
year: title.start_year,
runtime_minutes: title.runtime_minutes
}),
_ => Err(Error::WrongMediaType(title.title_type.into(), "Episode"))
}
}
}
pub(crate) fn title_matches_show_name_and_year(title: &Result<Title, Error>, name: &str, year: u16) -> bool {
if let Ok(title) = title {
if(title.title_type != TitleType::TVSeries) {
return false;
}
if let Some(title_year) = title.start_year {
if(title_year != year) {
return false;
}
} else {
return false;
}
if(title.primary_title != name && title.original_title != name) {
return false;
}
return true;
}
false
}