lotr_api/item/
mod.rs

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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
//! This module contains the data structures for the items that are returned by the API.
//! It also holds the [`attribute::Attribute`] enum and its derivatives, that contain the attributes
//! that represent the fields of the items ( they are used for filtering and sorting ).

use self::object::{Book, Chapter, Character, Movie, Quote};

pub mod attribute;
pub mod object;

/// The different types of items that can be retrieved from the API.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ItemType {
    Book,
    Movie,
    Quote,
    Character,
    Chapter,
}

impl From<&str> for ItemType {
    fn from(value: &str) -> Self {
        match value {
            "book" => ItemType::Book,
            "movie" => ItemType::Movie,
            "quote" => ItemType::Quote,
            "character" => ItemType::Character,
            "chapter" => ItemType::Chapter,
            _ => panic!("Invalid item type"),
        }
    }
}

/// The different items that can be retrieved from the API.
/// They are all wrapped in this enum, so that they can be used in the same vector.
#[derive(Debug, Clone, PartialEq)]
pub enum Item {
    Book(Book),
    Movie(Movie),
    Quote(Quote),
    Character(Character),
    Chapter(Chapter),
}

impl From<Book> for Item {
    fn from(book: Book) -> Self {
        Item::Book(book)
    }
}

impl TryInto<Book> for Item {
    type Error = ();

    fn try_into(self) -> Result<Book, Self::Error> {
        match self {
            Item::Book(book) => Ok(book),
            _ => Err(()),
        }
    }
}

impl From<Movie> for Item {
    fn from(movie: Movie) -> Self {
        Item::Movie(movie)
    }
}

impl TryInto<Movie> for Item {
    type Error = ();

    fn try_into(self) -> Result<Movie, Self::Error> {
        match self {
            Item::Movie(movie) => Ok(movie),
            _ => Err(()),
        }
    }
}

impl From<Quote> for Item {
    fn from(quote: Quote) -> Self {
        Item::Quote(quote)
    }
}

impl TryInto<Quote> for Item {
    type Error = ();

    fn try_into(self) -> Result<Quote, Self::Error> {
        match self {
            Item::Quote(quote) => Ok(quote),
            _ => Err(()),
        }
    }
}

impl From<Character> for Item {
    fn from(character: Character) -> Self {
        Item::Character(character)
    }
}

impl TryInto<Character> for Item {
    type Error = ();

    fn try_into(self) -> Result<Character, Self::Error> {
        match self {
            Item::Character(character) => Ok(character),
            _ => Err(()),
        }
    }
}

impl From<Chapter> for Item {
    fn from(chapter: Chapter) -> Self {
        Item::Chapter(chapter)
    }
}