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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
//! Traits used by the library. Those are of mostly internal use and normally shouldn't need
//! implementation by some user-made type.
//!
//! Note that it is recommended to always import this module for things to work fine, such as
//! using `X::fetch` methods, where `X` is any model implementing `PropFetchable` or
//! `PropLimFetchable` (there are some that do not implement either, and rather have their own
//! implementation of a `fetch` function, because they have 3 or more parameters).

use crate::error::{Result};

#[cfg(feature = "async")]
use async_trait::async_trait;
use crate::http::Client;
// use serde::de::DeserializeOwned;

use crate::http::routes::Route;

pub mod propfetch {
    use super::*;

    /// A trait representing a type with a property used to be fetched, and a route with which to
    /// fetch it. This can be either a tag or the limit of fetching, for example.
    ///
    /// The property must be returned by the `get_fetch_prop` function.
    /// The route must be returned by the `get_route` function.
    /// This trait is used in parallel with `PropFetchable`.
    ///
    /// [`PropFetchable`]: traits/propfetch/trait.PropFetchable.html
    /// [`GetFetchProp`]: traits/propfetch/trait.GetFetchProp.html
    pub trait GetFetchProp: Sized {
        type Property: ?Sized;

        /// Obtain the revelant property for fetching.
        fn get_fetch_prop(&self) -> &Self::Property;
        // necessary for Refetchable blanket impl

        /// Obtain the route for fetching by using a property.
        fn get_route(prop: &Self::Property) -> Route;
    }

    /// A trait representing a weaker variant of [`GetFetchProp`]; only indicates the fetching route can
    /// be obtained by the `get_route` method by specifying a property, not that such property is
    /// obtainable. Note that all `GetFetchProp` implementers also implement `PropRouteable`, thanks
    /// to a blanket impl.
    pub trait PropRouteable: Sized {
        type Property: ?Sized;

        /// Obtain the route for fetching by using a property.
        fn get_route(prop: &Self::Property) -> Route;
    }

    impl<T: GetFetchProp> PropRouteable for T {
        type Property = <T as GetFetchProp>::Property;

        fn get_route(prop: &<Self as PropRouteable>::Property) -> Route {
            <Self as GetFetchProp>::get_route(prop)
        }
    }

    /// A trait representing a type whose instance can be fetched from the API using some property.
    /// This is usually the object's tag.
    #[cfg_attr(feature = "async", async_trait)]
    pub trait PropFetchable: Sized {
        type Property: ?Sized;

        /// (Sync) Fetch and construct a new instance of this type.
        ///
        /// # Errors
        ///
        /// This function may error:
        /// - While requesting (will return an [`Error::Request`]);
        /// - After receiving a bad status code (API or other error - returns an [`Error::Status`]);
        /// - After a ratelimit is indicated by the API, while also specifying when it is lifted ([`Error::Ratelimited`]);
        /// - While parsing incoming JSON (will return an [`Error::Json`]).
        ///
        /// (All of those, of course, wrapped inside an `Err`.)
        ///
        /// # Examples
        ///
        /// Fetching a [`Player`] instance from a player tag:
        ///
        /// ```rust,ignore
        /// use brawl_api::{Client, Player, traits::*};
        ///
        /// # fn main() -> Result<(), Box<dyn ::std::error::Error>> {
        /// let my_client = Client::new("my auth token");
        /// let player = Player::fetch(&my_client, "#PLAYERTAGHERE")?;
        /// // now the data for the given player is available for use
        ///
        /// #     Ok(())
        /// # }
        /// ```
        ///
        /// [`Player`]: model/players/player/struct.Player.html
        /// [`Error::Request`]: error/enum.Error.html#variant.Request
        /// [`Error::Status`]: error/enum.Error.html#variant.Status
        /// [`Error::Ratelimited`]: error/enum.Error.html#variant.Ratelimited
        /// [`Error::Json`]: error/enum.Error.html#variant.Json
        fn fetch(client: &Client, prop: &Self::Property) -> Result<Self>;

        /// (Async) Fetch and construct a new instance of this type.
        ///
        /// # Errors
        ///
        /// This function may error:
        /// - While requesting (will return an [`Error::Request`]);
        /// - After receiving a bad status code (API or other error - returns an [`Error::Status`]);
        /// - After a ratelimit is indicated by the API, while also specifying when it is lifted ([`Error::Ratelimited`]);
        /// - While parsing incoming JSON (will return an [`Error::Json`]).
        ///
        /// (All of those, of course, wrapped inside an `Err`.)
        ///
        /// # Examples
        ///
        /// Fetching a [`Player`] instance from a player tag:
        ///
        /// ```rust,ignore
        /// use brawl_api::{Client, Player, traits::*};
        ///
        /// # async fn main() -> Result<(), Box<dyn ::std::error::Error>> {
        /// let my_client = Client::new("my auth token");
        /// let player = Player::a_fetch(&my_client, "#PLAYERTAGHERE").await?;
        /// // now the data for the given player is available for use
        ///
        /// #     Ok(())
        /// # }
        /// ```
        ///
        /// [`Player`]: model/players/player/struct.Player.html
        /// [`Error::Request`]: error/enum.Error.html#variant.Request
        /// [`Error::Status`]: error/enum.Error.html#variant.Status
        /// [`Error::Ratelimited`]: error/enum.Error.html#variant.Ratelimited
        /// [`Error::Json`]: error/enum.Error.html#variant.Json
        #[cfg(feature = "async")]
        async fn a_fetch(client: &Client, prop: &'async_trait Self::Property) -> Result<Self>
            where Self: 'async_trait,
                  Self::Property: 'async_trait;
    }
}

pub use propfetch::*;

pub mod proplimfetch {
    use super::*;
    use num_traits::PrimInt;

    /// A trait representing a type that returns a [`Route`] object given a property and a limit
    /// of how many to fetch.
    pub trait PropLimRouteable {
        type Property: ?Sized;
        type Limit: PrimInt;  // must be numeric!

        /// Obtain the route for fetching by using a property and specifying the limit of fetching.
        fn get_route(prop: &Self::Property, limit: Self::Limit) -> Route;
    }


    /// A trait representing a type whose instance can be fetched from the API using some property
    /// and specifying a limit of how many objects to fetch.
    ///
    /// **Note:** types which simply require the limit for fetching use [`PropFetchable`] instead
    /// (with the limit being the property itself).
    #[cfg_attr(feature = "async", async_trait)]
    pub trait PropLimFetchable: Sized {
        type Property: ?Sized;
        type Limit: PrimInt;  // numeric

        /// (Sync) Fetch and construct a new instance of this type.
        ///
        /// # Errors
        ///
        /// This function may error:
        /// - While requesting (will return an [`Error::Request`]);
        /// - After receiving a bad status code (API or other error - returns an [`Error::Status`]);
        /// - After a ratelimit is indicated by the API, while also specifying when it is lifted ([`Error::Ratelimited`]);
        /// - While parsing incoming JSON (will return an [`Error::Json`]).
        ///
        /// (All of those, of course, wrapped inside an `Err`.)
        ///
        /// # Examples
        ///
        /// Fetching a world-wide player leaderboard ([`PlayerLeaderboard`]):
        /// ```rust,ignore
        /// use brawl_api::{PlayerLeaderboard, Client, traits::PropLimFetchable};
        ///
        /// # fn main() -> Result<(), Box<dyn ::std::error::Error>> {
        /// let client = Client::new("my auth key");
        ///
        /// // if the fetch is successful, then the variable below will have the global top 100 players
        /// // in the 'items' field (i.e. '*top100players').
        /// let top100players: PlayerLeaderboard = PlayerLeaderboard::fetch(&client, "global", 100)?;
        ///
        /// // get player ranked #1. The items are usually sorted (i.e. rank 1 on index [0], rank 2
        /// // on index [1] etc.), but, to make the program absolutely safe, might want to .sort()
        /// let player1 = &top100players[0];
        ///
        /// assert_eq!(player1.rank, 1);
        ///
        /// #     Ok(())
        /// # }
        /// ```
        ///
        /// [`PlayerLeaderboard`]: model/rankings/players/struct.PlayerLeaderboard.html
        /// [`Error::Request`]: error/enum.Error.html#variant.Request
        /// [`Error::Status`]: error/enum.Error.html#variant.Status
        /// [`Error::Ratelimited`]: error/enum.Error.html#variant.Ratelimited
        /// [`Error::Json`]: error/enum.Error.html#variant.Json
        fn fetch(client: &Client, prop: &Self::Property, limit: Self::Limit) -> Result<Self>;

        /// (Async) Fetch and construct a new instance of this type.
        ///
        /// # Errors
        ///
        /// This function may error:
        /// - While requesting (will return an [`Error::Request`]);
        /// - After receiving a bad status code (API or other error - returns an [`Error::Status`]);
        /// - After a ratelimit is indicated by the API, while also specifying when it is lifted ([`Error::Ratelimited`]);
        /// - While parsing incoming JSON (will return an [`Error::Json`]).
        ///
        /// (All of those, of course, wrapped inside an `Err`.)
        ///
        /// # Examples
        ///
        /// Fetching a world-wide player leaderboard ([`PlayerLeaderboard`]):
        /// ```rust,ignore
        /// use brawl_api::{PlayerLeaderboard, Client, traits::PropLimFetchable};
        ///
        /// # async fn main() -> Result<(), Box<dyn ::std::error::Error>> {
        /// let client = Client::new("my auth key");
        ///
        /// // if the fetch is successful, then the variable below will have the global top 100 players
        /// // in the 'items' field (i.e. '*top100players').
        /// let top100players: PlayerLeaderboard = PlayerLeaderboard::a_fetch(&client, "global", 100).await?;
        ///
        /// // get player ranked #1. The items are usually sorted (i.e. rank 1 on index [0], rank 2
        /// // on index [1] etc.), but, to make the program absolutely safe, might want to .sort()
        /// let player1 = &top100players[0];
        ///
        /// assert_eq!(player1.rank, 1);
        ///
        /// #     Ok(())
        /// # }
        /// ```
        ///
        /// [`PlayerLeaderboard`]: model/rankings/players/struct.PlayerLeaderboard.html
        /// [`Error::Request`]: error/enum.Error.html#variant.Request
        /// [`Error::Status`]: error/enum.Error.html#variant.Status
        /// [`Error::Ratelimited`]: error/enum.Error.html#variant.Ratelimited
        /// [`Error::Json`]: error/enum.Error.html#variant.Json
        #[cfg(feature = "async")]
        async fn a_fetch(client: &Client, prop: &'async_trait Self::Property, limit: Self::Limit) -> Result<Self>
            where Self: 'async_trait,
                  Self::Property: 'async_trait,
                  Self::Limit: 'async_trait;
    }
}

pub use proplimfetch::*;

// endregion:PropFetch

/// A trait representing a type whose instance can be fetched again.
/// Note that all types implementing [`GetFetchProp`] and [`PropFetchable`] also implement
/// [`Refetchable`] due to a blanket implementation.
///
/// [`PropFetchable`]: traits/propfetch/trait.PropFetchable.html
/// [`GetFetchProp`]: traits/propfetch/trait.GetFetchProp.html
#[cfg_attr(feature = "async", async_trait)]
pub trait Refetchable: Sized {
    /// (Sync) Causes this instance to be re-fetched (i.e., updated to latest Brawl Stars data).
    ///
    /// # Examples
    ///
    /// ```rust,ignore
    /// use brawl_api::prelude::*;
    ///
    /// # fn main() -> Result<(), Box<dyn ::std::error::Error>> {
    /// let my_client = Client::new("my auth key");
    ///
    /// let player = Player::fetch(&my_client, "#PLAYER_TAG_HERE")?;
    ///
    /// // after using it a bit, we want to update its data
    ///
    /// let player = player.refetch(&my_client)?;  // `refetch` does not mutate the instance!
    ///
    /// // player variable is now up-to-date.
    ///
    /// #     Ok(())
    /// # }
    /// ```
    fn refetch(&self, client: &Client) -> Result<Self>;

    /// (Sync) Like `refetch`, but mutates the instance, returning an immutable reference to it.
    ///
    /// Its usage and errors are the same (it is called, after all), but the old variable does
    /// not need to be assigned; rather, that is done for the programmer (the variable's value
    /// **is entirely replaced** by a new one, if the fetching is successful).
    fn refetch_update(&mut self, client: &Client) -> Result<&Self> {
        *self = self.refetch(client)?;
        Ok(self as &Self)
    }

    /// (Async) Causes this instance to be re-fetched (i.e., updated to latest Brawl Stars data).
    ///
    /// # Examples
    ///
    /// ```rust,ignore
    /// use brawl_api::prelude::*;
    ///
    /// # async fn main() -> Result<(), Box<dyn ::std::error::Error>> {
    /// let my_client = Client::new("my auth key");
    ///
    /// let player = Player::a_fetch(&my_client, "#PLAYER_TAG_HERE").await?;
    ///
    /// // after using it a bit, we want to update its data
    ///
    /// let player = player.a_refetch(&my_client).await?;  // this does not mutate the old instance!
    ///
    /// // player variable is now up-to-date.
    ///
    /// #     Ok(())
    /// # }
    /// ```
    #[cfg(feature = "async")]
    async fn a_refetch(&self, client: &Client) -> Result<Self>;

    /// (Async) Like `a_refetch`, but mutates the instance, returning an immutable reference to it.
    ///
    /// Its usage and errors are the same (it is called, after all), but the old variable does
    /// not need to be assigned; rather, that is done for the programmer (the variable's value
    /// **is entirely replaced** by a new one, if the fetching is successful).
    #[cfg(feature = "async")]
    async fn a_refetch_update(&'async_trait mut self, client: &Client) -> Result<&'async_trait Self>
        where Self: Send + Sync,
    {
        *self = self.a_refetch(client).await?;
        Ok(self as &Self)
    }
}

#[cfg_attr(feature = "async", async_trait)]
impl<T> Refetchable for T
    where T: PropFetchable<Property=<T as GetFetchProp>::Property> + GetFetchProp + Sized + Send + Sync,
          <T as GetFetchProp>::Property: Sync + Send {
    fn refetch(&self, client: &Client) -> Result<Self> {
        Self::fetch(client, self.get_fetch_prop())
    }

    #[cfg(feature = "async")]
    async fn a_refetch(&self, client: &Client) -> Result<Self>
        where T: 'async_trait,
        <T as GetFetchProp>::Property: 'async_trait,
    {
        Self::a_fetch(client, self.get_fetch_prop()).await
    }
}


/// A trait indicating that another type can be converted into this one by fetching from the API.
/// Note that, thanks to a blanket implementation, implementing this implies implementing
/// [`FetchInto`] for the other type.
///
/// [`FetchInto`]: traits/trait.FetchInto.html
#[cfg_attr(feature = "async", async_trait)]
pub trait FetchFrom<T>: Sized {
    /// (Sync) Attempts to request to the API and return a new instance of the type being turned
    /// into.
    ///
    /// # Errors
    ///
    /// See the respective struct's `fetch` implementation (or `PropFetchable`/`PropLimFetchable`
    /// implementation).
    ///
    /// # Examples
    ///
    /// ```rust,ignore
    /// use brawl_api::{Client, Player, Club, traits::*};
    ///
    /// # fn main() -> Result<(), Box<dyn ::std::error::Error>> {
    /// let my_client = Client::new("my auth token");
    /// let club = Club::fetch(&my_client, "#CLUB_TAG_HERE")?;
    /// let some_member = &club.members[0];
    /// let some_player = Player::fetch_from(&my_client, some_member)?;
    /// // now `some_member`'s full data, as a Player, is available for use.
    ///
    /// #     Ok(())
    /// # }
    /// ```
    fn fetch_from(client: &Client, value: &T) -> Result<Self>;

    /// (Async) Attempts to request to the API and return a new instance of the type being turned
    /// into.
    ///
    /// # Errors
    ///
    /// See the respective struct's `a_fetch` implementation (or `PropFetchable`/`PropLimFetchable`
    /// implementation).
    ///
    /// # Examples
    ///
    /// ```rust,ignore
    /// use brawl_api::{Client, Player, Club, traits::*};
    ///
    /// # async fn main() -> Result<(), Box<dyn ::std::error::Error>> {
    /// let my_client = Client::new("my auth token");
    /// let club = Club::a_fetch(&my_client, "#CLUB_TAG_HERE").await?;
    /// let some_member = &club.members[0];
    /// let some_player = Player::a_fetch_from(&my_client, some_member).await?;
    /// // now `some_member`'s full data, as a Player, is available for use.
    ///
    /// #     Ok(())
    /// # }
    /// ```
    #[cfg(feature = "async")]
    async fn a_fetch_from(client: &Client, value: &T) -> Result<Self>;
}

/// A trait indicating that this type can be converted into another by fetching from the API.
/// Note that [`FetchFrom`] should be implemented, in order to apply the respective blanket
/// implementation of this trait.
///
/// [`FetchFrom`]: traits/trait.FetchFrom.html
#[cfg_attr(feature = "async", async_trait)]
pub trait FetchInto<T>: Sized {
    /// (Sync) Attempts to request to the API and return a new instance of the type being turned
    /// into.
    ///
    /// # Errors
    ///
    /// See the respective into-type's `fetch` implementation (or `PropFetchable`/`PropLimFetchable`
    /// implementation).
    ///
    /// # Examples
    ///
    /// See [`FetchFrom::<T>::fetch_from`].
    fn fetch_into(&self, client: &Client) -> Result<T>;

    #[cfg(feature = "async")]
    /// (Async) Attempts to request to the API and return a new instance of the type being turned
    /// into.
    ///
    /// # Errors
    ///
    /// See the respective into-type's `a_fetch` implementation (or
    /// `PropFetchable`/`PropLimFetchable` implementation).
    ///
    /// # Examples
    ///
    /// See [`FetchFrom::<T>::a_fetch_from`].
    async fn a_fetch_into(&self, client: &Client) -> Result<T>
        where T: 'async_trait;
}

// FetchFrom implies FetchInto
#[cfg_attr(feature = "async", async_trait)]
impl<T, U> FetchInto<U> for T
    where T: Sync + Send, U: FetchFrom<T> + Sync + Send
{
    fn fetch_into(&self, client: &Client) -> Result<U> {
        U::fetch_from(client, self)
    }

    #[cfg(feature = "async")]
    async fn a_fetch_into(&self, client: &Client) -> Result<U>
        where U: 'async_trait
    {
        U::a_fetch_from(client, self).await
    }
}

// FetchFrom (and thus FetchInto) is reflexive
#[cfg_attr(feature = "async", async_trait)]
impl<T: Sync + Send + Clone> FetchFrom<T> for T {
    /// (Sync) Returns a copy of the current instance when attempting to fetch from itself.
    /// In order to re-fetch, see [`Refetchable`].
    ///
    /// # Errors
    ///
    /// Never errors; is only a [`Result`] in order to match the trait signature.
    ///
    /// [`Refetchable`]: trait.Refetchable.html
    /// [`Result`]: ../error/type.Result.html
    fn fetch_from(_: &Client, t: &T) -> Result<T> { Ok(t.to_owned()) }

    /// (Async) Returns a copy of the current instance when attempting to fetch from itself.
    /// In order to re-fetch, see [`Refetchable`].
    ///
    /// # Errors
    ///
    /// Never errors; is only a [`Result`] in order to match the trait signature.
    ///
    /// [`Refetchable`]: trait.Refetchable.html
    /// [`Result`]: ../error/type.Result.html
    #[cfg(feature = "async")]
    async fn a_fetch_from(_: &Client, t: &T) -> Result<Self> { Ok(t.to_owned()) }
}