simkl 0.1.0

Library to build queries for SIMKL and decoding JSON responses using Serde
Documentation
use std::fmt;

/// Movie genre values used by the Simkl API (e.g. as the `&genre=` query parameter).
#[derive(Debug, Clone, Copy, PartialEq)]
#[repr(u8)]
pub enum MovieGenre {
    Action,
    Adventure,
    Animation,
    Comedy,
    Crime,
    Documentary,
    Drama,
    Erotica,
    Family,
    Fantasy,
    Foreign,
    History,
    Horror,
    Music,
    Mystery,
    Romance,
    ScienceFiction,
    Thriller,
    TvMovie,
    War,
    Western,
}

impl MovieGenre {
    /// Returns the kebab-case API value for the genre (e.g. `"science-fiction"`, `"tv-movie"`).
    pub fn as_str(&self) -> &'static str {
        match self {
            MovieGenre::Action => "action",
            MovieGenre::Adventure => "adventure",
            MovieGenre::Animation => "animation",
            MovieGenre::Comedy => "comedy",
            MovieGenre::Crime => "crime",
            MovieGenre::Documentary => "documentary",
            MovieGenre::Drama => "drama",
            MovieGenre::Erotica => "erotica",
            MovieGenre::Family => "family",
            MovieGenre::Fantasy => "fantasy",
            MovieGenre::Foreign => "foreign",
            MovieGenre::History => "history",
            MovieGenre::Horror => "horror",
            MovieGenre::Music => "music",
            MovieGenre::Mystery => "mystery",
            MovieGenre::Romance => "romance",
            MovieGenre::ScienceFiction => "science-fiction",
            MovieGenre::Thriller => "thriller",
            MovieGenre::TvMovie => "tv-movie",
            MovieGenre::War => "war",
            MovieGenre::Western => "western",
        }
    }
}

impl fmt::Display for MovieGenre {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.as_str())
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_movie_genre_as_str_single_word() {
        assert_eq!(MovieGenre::Action.as_str(), "action");
        assert_eq!(MovieGenre::Family.as_str(), "family");
        assert_eq!(MovieGenre::Western.as_str(), "western");
    }

    #[test]
    fn test_movie_genre_as_str_hyphenated() {
        assert_eq!(MovieGenre::ScienceFiction.as_str(), "science-fiction");
        assert_eq!(MovieGenre::TvMovie.as_str(), "tv-movie");
    }

    #[test]
    fn test_movie_genre_display() {
        assert_eq!(MovieGenre::ScienceFiction.to_string(), "science-fiction");
        assert_eq!(MovieGenre::TvMovie.to_string(), "tv-movie");
    }
}