Skip to main content

igdb_rs/
client_macros.rs

1macro_rules! create_client {
2    ($i: ident, $j: ident, $k: ident) => {
3        pub struct $i {
4            endpoint_client: EndpointClient,
5        }
6
7        impl $i {
8            ///Receives a configured RequestBuilder an returns a result containing a collection
9            pub async fn get(&self, request_builder: RequestBuilder) -> Result<Vec<$j>, Error> {
10                self.endpoint_client.get::<$j>(request_builder).await
11            }
12            ///Returns a collection filtered by id and limits the retrieved registries using limit parameter value.
13            pub async fn get_by_id(&self, id: usize, limit: usize) -> Result<Vec<$j>, Error> {
14                let mut request = RequestBuilder::new();
15                request
16                    .all_fields()
17                    .add_where("id", Equality::Equal, id.to_string())
18                    .limit(limit);
19
20                self.get(request).await
21            }
22            /// Returns the element by Id for this client in Option<T> format.
23            pub async fn get_first_by_id(&self, id: usize) -> Result<$j, Error> {
24                match self.get_by_id(id, 1).await {
25                    Ok(ref d) if !d.is_empty() => Ok(d[0].clone()),
26                    Ok(_) => Err(std::io::Error::new(
27                        std::io::ErrorKind::InvalidData,
28                        format!("Empty response from server for query with id: {}", id),
29                    )
30                    .into()),
31                    Err(e) => {
32                        log::error!("{}", e);
33                        Err(e)
34                    }
35                }
36            }
37        }
38
39        impl IGDBClient {
40            /// Returns a reference to the client
41            pub fn $k(&self) -> $i {
42                $i {
43                    endpoint_client: EndpointClient::new(self.api_key.clone(), Endpoint::$k),
44                }
45            }
46        }
47    };
48}
49
50macro_rules! expand_get_by_game_id {
51    ($i: ident, $j: ident) => {
52        impl $i {
53            ///Receives a game_id and a limit of registries and returns an Option<Vec> of elements
54            pub async fn get_by_game_id(&self, game_id: usize, limit: usize) -> Option<Vec<$j>> {
55                let mut request = RequestBuilder::new();
56                request
57                    .all_fields()
58                    .add_where("game", Equality::Equal, game_id.to_string())
59                    .limit(limit);
60
61                match self.get(request).await {
62                    Ok(d) => Some(d),
63                    Err(e) => {
64                        log::error!("{}", e);
65                        None
66                    }
67                }
68            }
69        }
70    };
71}
72
73#[allow(unused_macros)]
74macro_rules! request {
75    () => {
76        IGDBClient::create_request()
77    };
78}
79
80macro_rules! use_client_imports {
81    () => {
82        use crate::{
83            endpoint_client::EndpointClient, endpoints::Endpoint, media_quality::MediaQuality,
84            model::age_rating::AgeRating, model::artwork::Artwork, model::character::Character,
85            model::character_mug_shot::CharacterMugshot, model::company::Company,
86            model::cover::Cover, model::engine::Engine, model::franchise::Franchise,
87            model::game_mode::GameMode, model::game_video::GameVideo, model::games::Game,
88            model::multiplayer_mode::MultiplayerMode, model::platform::Platform,
89            model::platform_logo::PlatformLogo, model::player_perspective::PlayerPerspective,
90            model::release_date::ReleaseDate, model::screenshot::Screenshot, model::theme::Theme,
91            model::website::Website, request_builder::Equality, request_builder::RequestBuilder,
92        };
93
94        use crate::Error;
95    };
96}