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
use std::convert::TryFrom;
#[cfg(test)]
use std::convert::TryInto;

use arrayvec::ArrayVec;
use smartstring::alias::String;

use crate::genre::Genre;
use crate::Error;
use crate::Title;
use crate::TitleType;

#[derive(Debug, Eq, PartialEq)]
/// Represents a movie from IMDB.  Pared down from [Title] based on fields that make sense for movies.
pub struct Movie {
	pub imdb_id: u32,
	pub title: String,
	pub is_adult: bool,
	pub year: u16,
	pub runtime_minutes: Option<u16>,
	pub genres: ArrayVec<Genre, 3>
}

impl Movie {
	#[cfg(test)]
	pub(crate) fn new(imdb_id: u32, title: &str, is_adult: bool, year: u16, runtime_minutes: Option<u16>, genres: &[Genre]) -> Self {
		Self{
			imdb_id,
			title: String::from(title),
			is_adult,
			year,
			runtime_minutes,
			genres: genres.try_into().unwrap()
		}
	}

	pub(crate) fn from_wrapped_title(input: Result<Title, Error>) -> Result<Self, Error> {
		Self::try_from(input?)
	}
}

impl TryFrom<Title> for Movie {
	type Error = Error;
	fn try_from(input: Title) -> Result<Self, Error> {
		match input.title_type {
			TitleType::Movie => Ok(Self{
				imdb_id: input.imdb_id,
				title: input.primary_title,
				is_adult: input.is_adult,
				year: match input.start_year {
					Some(v) => v,
					None => match input.end_year {
						Some(v) => v,
						None => return Err(Error::YearMissing)
					}
				},
				//year: input.start_year,
				runtime_minutes: input.runtime_minutes,
				genres: input.genres
			}),
			_ => Err(Error::WrongMediaType(input.title_type.into(), "Movie"))
		}
	}
}

pub(crate) fn title_matches_movie_name_and_year(title: &Result<Title, Error>, name: &str, year: u16) -> bool {
	if let Ok(title) = title {
		if(title.title_type != TitleType::Movie) {
			return false;
		}
		if let Some(title_year) = title.start_year {
			if(title_year != year) {
				return false;
			}
		} else if let Some(title_year) = title.end_year {
			if(title_year != year) {
				return false;
			}
		} else {
			return false;
		}
		if(title.primary_title != name && title.original_title != name) {
			return false;
		}
		return true;
	}
	false
}