lotr_api/item/
mod.rs

1//! This module contains the data structures for the items that are returned by the API.
2//! It also holds the [`attribute::Attribute`] enum and its derivatives, that contain the attributes
3//! that represent the fields of the items ( they are used for filtering and sorting ).
4
5use self::object::{Book, Chapter, Character, Movie, Quote};
6
7pub mod attribute;
8pub mod object;
9
10/// The different types of items that can be retrieved from the API.
11#[derive(Debug, Clone, PartialEq, Eq)]
12pub enum ItemType {
13    Book,
14    Movie,
15    Quote,
16    Character,
17    Chapter,
18}
19
20impl From<&str> for ItemType {
21    fn from(value: &str) -> Self {
22        match value {
23            "book" => ItemType::Book,
24            "movie" => ItemType::Movie,
25            "quote" => ItemType::Quote,
26            "character" => ItemType::Character,
27            "chapter" => ItemType::Chapter,
28            _ => panic!("Invalid item type"),
29        }
30    }
31}
32
33/// The different items that can be retrieved from the API.
34/// They are all wrapped in this enum, so that they can be used in the same vector.
35#[derive(Debug, Clone, PartialEq)]
36pub enum Item {
37    Book(Book),
38    Movie(Movie),
39    Quote(Quote),
40    Character(Character),
41    Chapter(Chapter),
42}
43
44impl From<Book> for Item {
45    fn from(book: Book) -> Self {
46        Item::Book(book)
47    }
48}
49
50impl TryInto<Book> for Item {
51    type Error = ();
52
53    fn try_into(self) -> Result<Book, Self::Error> {
54        match self {
55            Item::Book(book) => Ok(book),
56            _ => Err(()),
57        }
58    }
59}
60
61impl From<Movie> for Item {
62    fn from(movie: Movie) -> Self {
63        Item::Movie(movie)
64    }
65}
66
67impl TryInto<Movie> for Item {
68    type Error = ();
69
70    fn try_into(self) -> Result<Movie, Self::Error> {
71        match self {
72            Item::Movie(movie) => Ok(movie),
73            _ => Err(()),
74        }
75    }
76}
77
78impl From<Quote> for Item {
79    fn from(quote: Quote) -> Self {
80        Item::Quote(quote)
81    }
82}
83
84impl TryInto<Quote> for Item {
85    type Error = ();
86
87    fn try_into(self) -> Result<Quote, Self::Error> {
88        match self {
89            Item::Quote(quote) => Ok(quote),
90            _ => Err(()),
91        }
92    }
93}
94
95impl From<Character> for Item {
96    fn from(character: Character) -> Self {
97        Item::Character(character)
98    }
99}
100
101impl TryInto<Character> for Item {
102    type Error = ();
103
104    fn try_into(self) -> Result<Character, Self::Error> {
105        match self {
106            Item::Character(character) => Ok(character),
107            _ => Err(()),
108        }
109    }
110}
111
112impl From<Chapter> for Item {
113    fn from(chapter: Chapter) -> Self {
114        Item::Chapter(chapter)
115    }
116}