imdb-async 0.12.2

Opinionated and unopinionated async wrappers to efficiently retrieve and parse IMDB's dataset
Documentation
use std::convert::TryFrom;
#[cfg(test)]
use std::convert::TryInto;

use arrayvec::ArrayVec;
use compact_str::CompactString;

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: CompactString,
	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: CompactString::new(title),
			is_adult,
			year,
			runtime_minutes,
			genres: genres.try_into().unwrap()
		}
	}

	#[inline]
	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;
	#[inline]
	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"))
		}
	}
}

#[inline]
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
}