brawl_api/
lib.rs

1//! The `brawl-api` crate creates models and fetching methods employing the
2//! [official Brawl Stars API](https://developer.brawlstars.com). It aims to be an usable,
3//! predictable and programmer-friendly library. Anywhere it is not seen as so, contribution is
4//! very welcome.
5//!
6//! # Requesting
7//!
8//! All the requesting work is done by the [`reqwest` crate].
9//! The library supports both sync (blocking) and, with the `async` feature (enabled by default),
10//! async function styles - all fetching methods with that duality have a sync variant (with its
11//! normal name) and an async one (named by prefixing the sync name with `a_` - e.g.: `fetch` (sync)
12//! and `a_fetch` (async)).
13//!
14//! # Deserialization and data structure
15//!
16//! Deserialization of JSON is done thanks to the [`serde`] and [`serde-json`] libraries, which are
17//! of invaluable aid on that aspect. This means that **all library models** implement the
18//! [`Serialize`] and [`Deserialize`] serde traits, optimized to match the JSON formats documented
19//! in the API's official documentation.
20//!
21//! In addition, it is noticeable that most API models implement the `Default` trait. This is for
22//! usage with `serde`, in order to ensure that the values will be there, regardless of an API
23//! failure. Fields not marked with `Option<T>` **should be there** by most standards - note that
24//! the official Brawl Stars API docs state that all fields everywhere are optional (which isn't
25//! helpful), even though most of them will always occur unless a server fault or some unexpected,
26//! new use-case was added (in which case, the library can be blamed for not being updated - **feel
27//! free to contribute with a PR, or just an issue poking me about this, adding this to the
28//! library!**).
29//!
30//! For more info on models, see the [`model`] module.
31//!
32//! # Recommended Usage
33//!
34//! It is recommended to import the library using its [`prelude`] module:
35//!
36//! ```rust
37//! use brawl_api::prelude::*;
38//! ```
39//!
40//! This brings into scope all of the library's traits, models and the helper [`Brawlers`] enum.
41//!
42//! If it is not desired to bring all models into scope, then at least **import all traits** so that
43//! models work properly:
44//!
45//! ```rust
46//! use brawl_api::traits::*;
47//! ```
48//!
49//! # Feature Flags
50//!
51//! The crate has a few feature flags available (all enabled by default):
52//!
53//! - `async` flag:
54//!     - Enables the usage of async (non-blocking) fetch functions - `a_fetch`, `a_fetch_from`,
55//! `a_fetch_into`, `a_refetch` - where applicable.
56//!     - Adds `async_trait` as a dependency.
57//! - `auto-hashtag` flag: Enables the smart insertion of hashtags on anywhere a tag is required.
58//!     - This means, for example, that on a [`Player::fetch`] call, which requires the tag of the
59//! player to be fetched, one can pass a string containing a hashtag at the start (in which case,
60//! the library simply uses it) ***or without*** (then, with this feature on, the lib adds it
61//! automatically).
62//!     - Disabling this requires passing hashtags at the start of every tag string. This is due to
63//! how the API parses tags, and not much can be done about it.
64//! - `chrono`: Adds `chrono` as dependency and enables the usage of [`TimeLike.parse`], which
65//! parses an incoming timestamp into a [`chrono::DateTime<chrono::Utc>`].
66//! - `players` flag: Enables the usage of the [`model::players`] module (for the `/players` endpoint).
67//! - `clubs` flag: Enables the usage of the [`model::clubs`] module (for the `/clubs` endpoint).
68//! - `rankings` flag: Enables the usage of the [`model::rankings`] module (for the `/rankings` endpoint).
69//! - `brawlers` flag: Enables the usage of the [`model::brawlers`] module (for the `/brawlers` endpoint).
70//!
71//! [`reqwest` crate]: https://crates.io/crate/reqwest
72//! [`serde`]: https://crates.io/crate/serde
73//! [`serde-json`]: https://crates.io/crate/serde-json
74//! [`Serialize`]: https://docs.rs/serde/*/ser/trait.Serialize.html
75//! [`Deserialize`]: https://docs.rs/serde/*/de/trait.Deserialize.html
76//! [`Player::fetch`]: model/players/player/struct.Player.html#method.fetch
77//! [`TimeLike.parse`]: time/struct.TimeLike.html#method.parse
78//! [`chrono::DateTime<chrono::Utc>`]: https://docs.rs/chrono/*/chrono/struct.DateTime.html
79//! [`model`]: model/index.html
80//! [`prelude`]: prelude/index.html
81//! [`Brawlers`]: constants/enum.Brawlers.html
82//! [`model::players`]: model/players/index.html
83//! [`model::clubs`]: model/clubs/index.html
84//! [`model::rankings`]: model/rankings/index.html
85//! [`model::brawlers`]: model/brawlers/index.html
86
87pub(crate) mod util;
88
89pub(crate) mod serde;
90
91pub mod constants;
92pub use constants::Brawlers;
93
94pub mod http;
95pub use http::client::Client;
96
97mod macros;
98
99pub mod model;
100
101pub mod time;
102pub use time::TimeLike;
103
104#[cfg(any(feature = "players", feature = "brawlers"))]
105pub use model::common::StarPower;
106
107#[cfg(feature = "players")]
108pub use model::players::{
109    Player, PlayerClub, PlayerBrawlerStat,
110    battlelog::{
111        BattleLog,
112        Battle, BattleEvent, BattleResultInfo,
113        BattlePlayer, BattleBrawler, BattleOutcome,
114    },
115};
116
117pub mod traits;
118
119#[cfg(feature = "clubs")]
120pub use model::clubs::{Club, ClubMember, ClubMembers, ClubMemberRole, ClubType};
121
122#[cfg(feature = "rankings")]
123pub use model::rankings::{
124    players::{PlayerLeaderboard, PlayerRanking, PlayerRankingClub},
125    clubs::{ClubLeaderboard, ClubRanking},
126    brawlers::BrawlerLeaderboard,
127};
128
129#[cfg(feature = "brawlers")]
130pub use model::brawlers::{BrawlerList, Brawler};
131
132pub mod error;
133pub use error::{Error, Result};
134
135pub mod prelude;
136
137//#[cfg(test)]
138//mod tests {
139//    #[test]
140//    fn it_works() {
141//        assert_eq!(2 + 2, 4);
142//    }
143//}