1use std::convert::TryFrom;
2#[cfg(test)]
3use std::convert::TryInto;
4
5use arrayvec::ArrayVec;
6use compact_str::CompactString;
7
8use crate::genre::Genre;
9use crate::Error;
10use crate::Title;
11use crate::TitleType;
12
13#[derive(Debug, Eq, PartialEq)]
14pub struct Movie {
16 pub imdb_id: u32,
17 pub title: CompactString,
18 pub is_adult: bool,
19 pub year: u16,
20 pub runtime_minutes: Option<u16>,
21 pub genres: ArrayVec<Genre, 3>
22}
23
24impl Movie {
25 #[cfg(test)]
26 pub(crate) fn new(imdb_id: u32, title: &str, is_adult: bool, year: u16, runtime_minutes: Option<u16>, genres: &[Genre]) -> Self {
27 Self{
28 imdb_id,
29 title: CompactString::new(title),
30 is_adult,
31 year,
32 runtime_minutes,
33 genres: genres.try_into().unwrap()
34 }
35 }
36
37 #[inline]
38 pub(crate) fn from_wrapped_title(input: Result<Title, Error>) -> Result<Self, Error> {
39 Self::try_from(input?)
40 }
41}
42
43impl TryFrom<Title> for Movie {
44 type Error = Error;
45 #[inline]
46 fn try_from(input: Title) -> Result<Self, Error> {
47 match input.title_type {
48 TitleType::Movie => Ok(Self{
49 imdb_id: input.imdb_id,
50 title: input.primary_title,
51 is_adult: input.is_adult,
52 year: match input.start_year {
53 Some(v) => v,
54 None => match input.end_year {
55 Some(v) => v,
56 None => return Err(Error::YearMissing)
57 }
58 },
59 runtime_minutes: input.runtime_minutes,
61 genres: input.genres
62 }),
63 _ => Err(Error::WrongMediaType(input.title_type.into(), "Movie"))
64 }
65 }
66}
67
68#[inline]
69pub(crate) fn title_matches_movie_name_and_year(title: &Result<Title, Error>, name: &str, year: u16) -> bool {
70 if let Ok(title) = title {
71 if(title.title_type != TitleType::Movie) {
72 return false;
73 }
74 if let Some(title_year) = title.start_year {
75 if(title_year != year) {
76 return false;
77 }
78 } else if let Some(title_year) = title.end_year {
79 if(title_year != year) {
80 return false;
81 }
82 } else {
83 return false;
84 }
85 if(title.primary_title != name && title.original_title != name) {
86 return false;
87 }
88 return true;
89 }
90 false
91}
92