diff --git a/src/client.rs b/src/client.rs index 3d620e0..8fc3c45 100644 --- a/src/client.rs +++ b/src/client.rs @@ -1226,301 +1226,6 @@ where } } -pub mod query { - //! Structs for query parameters. - - use std::num::NonZeroU8; - - /// A struct for query parameters for the XP leaderboard. - /// - /// `None` means default value. - /// - /// This structure manages the following four query parameters: - /// - /// - `before`(f64): The lower bound in XP. - /// Use this to paginate upwards. - /// Take the highest seen XP and pass that back through this field to continue scrolling. - /// If set, the search order is reversed (returning the lowest items that match the query) - /// - /// - `after`(f64): The upper bound in XP. - /// Use this to paginate downwards. - /// Take the lowest seen XP and pass that back through this field to continue scrolling. - /// Infinite([`f64::INFINITY`]) by default. - /// - /// - `limit`([NonZeroU8]): The amount of entries to return. - /// Between 1 and 100. - /// 50 by default. - /// - /// - `country`(String): The ISO 3166-1 country code to filter to. - /// Leave unset to not filter by country. - /// - /// ***The `before` and `after` parameters may not be combined.** - /// - /// # Examples - /// - /// ``` - /// use tetr_ch::client::query::XPLeaderboardQuery; - /// - /// // Default(descending, fifty entries) query. - /// let q1 = XPLeaderboardQuery::new(); - /// - /// // 50,000,000,000,000xp or less, thirty entries, filter by Japan. - /// let q2 = XPLeaderboardQuery::new() - /// .after(50_000_000_000_000.) - /// .limit(3) - /// .country("jp"); - /// - /// // 50,000,000,000,000xp or higher. - /// // Also sort by XP ascending. - /// let q3 = XPLeaderboardQuery::new() - /// .before(50_000_000_000_000.); - /// - /// // You can restore the query parameters to default as follows: - /// let mut q4 = XPLeaderboardQuery::new().country("us"); - /// q4.init(); - /// ``` - #[derive(Clone, Debug, Default)] - pub struct XPLeaderboardQuery { - /// The bound in XP. - /// - /// The `before` and `after` parameters may not be combined, - /// so either set the parameter with an enum or set it to default(after) by passing `None`. - pub before_or_after: Option, - /// The amount of entries to return. - /// Between 1 and 100. 50 by default. - pub limit: Option, - /// The ISO 3166-1 country code to filter to. Leave unset to not filter by country. - /// But some vanity flags exist. - pub country: Option, - } - - impl XPLeaderboardQuery { - /// Creates a new[`XPLeaderboardQuery`]. - /// Values are set to default. - /// - /// # Examples - /// - /// Creates a new[`XPLeaderboardQuery`] with default parameters. - /// - /// ``` - /// # use tetr_ch::client::query::XPLeaderboardQuery; - /// let query = XPLeaderboardQuery::new(); - /// ``` - pub fn new() -> Self { - Self::default() - } - - /// Initializes the [`XPLeaderboardQuery`]. - /// - /// # Examples - /// - /// Initializes the [`XPLeaderboardQuery`] with default parameters. - /// - /// ``` - /// # use tetr_ch::client::query::XPLeaderboardQuery; - /// let mut query = XPLeaderboardQuery::new(); - /// query.init(); - /// ``` - pub fn init(self) -> Self { - Self::default() - } - - /// Set the query parameter`before`. - /// - /// The `before` and `after` parameters may not be combined, - /// so even if there is an `after` parameter, the `before` parameter takes precedence and overrides it. - /// Disabled by default. - /// - /// # Examples - /// - /// Sets the query parameter`before` to `50,000,000,000,000`. - /// - /// ``` - /// # use tetr_ch::client::query::XPLeaderboardQuery; - /// let mut query = XPLeaderboardQuery::new() - /// .before(50_000_000_000_000.); - /// ``` - pub fn before(self, bound: f64) -> Self { - Self { - before_or_after: if bound.is_infinite() { - Some(BeforeAfter::Before(bound)) - } else { - None - }, - ..self - } - } - - /// Set the query parameter`after`. - /// - /// The `before` and `after` parameters may not be combined, - /// so even if there is a `before` parameter, the `after` parameter takes precedence and overrides it. - /// Infinite([`f64::INFINITY`]) by default. - /// - /// # Examples - /// - /// Sets the query parameter`after` to `50,000,000,000,000`. - /// - /// ``` - /// # use tetr_ch::client::query::XPLeaderboardQuery; - /// let mut query = XPLeaderboardQuery::new() - /// .after(50_000_000_000_000.); - /// ``` - pub fn after(self, bound: f64) -> Self { - Self { - before_or_after: Some(BeforeAfter::After(bound)), - ..self - } - } - - /// Set the query parameter`limit` - /// The amount of entries to return, Between `1` and `100`. - /// 50 by default. - /// - /// # Examples - /// - /// Sets the query parameter`limit` to `5`. - /// - /// ``` - /// # use tetr_ch::client::query::XPLeaderboardQuery; - /// let mut query = XPLeaderboardQuery::new().limit(5); - /// ``` - /// - /// # Panics - /// - /// Panics if argument`limit` is not between `1` and `100`. - /// - /// ```should_panic - /// # use tetr_ch::client::query::XPLeaderboardQuery; - /// let mut query = XPLeaderboardQuery::new().limit(0); - /// ``` - /// - /// ```should_panic - /// # use tetr_ch::client::query::XPLeaderboardQuery; - /// let mut query = XPLeaderboardQuery::new().limit(101); - /// ``` - pub fn limit(self, limit: u8) -> Self { - if (1..=100).contains(&limit) { - // 1 <= limit && limit <= 100 - Self { - limit: Some(NonZeroU8::new(limit).unwrap()), - ..self - } - } else { - panic!( - "The argument`limit` must be between 1 and 100.\n\ - Received: {}", - limit - ); - } - } - - /// Set the query parameter`country`. - /// - /// # Examples - /// - /// Sets the query parameter`country` to `ca`. - /// - /// ``` - /// # use tetr_ch::client::query::XPLeaderboardQuery; - /// let mut query = XPLeaderboardQuery::new().country("ca"); - /// ``` - pub fn country(self, country: &str) -> Self { - Self { - country: Some(country.to_owned().to_uppercase()), - ..self - } - } - - /// Whether the query parameters`limit` is out of bounds. - /// - /// # Examples - /// - /// ``` - /// # use tetr_ch::client::query::XPLeaderboardQuery; - /// use std::num::NonZeroU8; - /// - /// let invalid_query = XPLeaderboardQuery{ - /// limit: Some(NonZeroU8::new(101).unwrap()), - /// ..XPLeaderboardQuery::new() - /// }; - /// assert!(invalid_query.is_invalid_limit_range()); - /// ``` - #[allow(clippy::nonminimal_bool)] - pub fn is_invalid_limit_range(&self) -> bool { - if let Some(l) = self.limit { - !(l <= NonZeroU8::new(100).unwrap()) - } else { - false - } - } - - /// Builds the query parameters to `Vec<(String, String)>`. - /// - /// # Examples - /// - /// ```ignore - /// # use tetr_ch::client::query::XPLeaderboardQuery; - /// let query = XPLeaderboardQuery::new(); - /// let query_params = query.build(); - /// ``` - pub(crate) fn build(mut self) -> Vec<(String, String)> { - // For not pass "inf" to puery parameters. - if let Some(BeforeAfter::After(b)) = self.before_or_after { - if b.is_infinite() { - self.before_or_after = None; - } - } - let mut result = Vec::new(); - if let Some(b_a) = self.before_or_after.clone() { - match b_a { - BeforeAfter::Before(b) => result.push(("before".to_string(), b.to_string())), - BeforeAfter::After(b) => result.push(("after".to_string(), b.to_string())), - } - } - if let Some(l) = self.limit { - result.push(("limit".to_string(), l.to_string())); - } - if let Some(c) = self.country { - result.push(("country".to_string(), c)); - } - result - } - - /// Returns the default [`XPLeaderboardQuery`]. - /// - /// # Examples - /// - /// ```ignore - /// # use tetr_ch::client::query::XPLeaderboardQuery; - /// let query = XPLeaderboardQuery::default(); - /// ``` - fn default() -> Self { - Self { - before_or_after: None, - limit: None, - country: None, - } - } - } - - /// The bound. - /// - /// The `before` and `after` parameters may not be combined, - /// so need to either set the parameter. - #[derive(Clone, Debug)] - pub enum BeforeAfter { - /// The lower bound. - /// Use this to paginate upwards. - /// Take the highest seen value and pass that back through this field to continue scrolling. - /// If set, the search order is reversed (returning the lowest items that match the query) - Before(f64), - /// Use this to paginate downwards. - /// Take the lowest seen value and pass that back through this field to continue scrolling. - After(f64), - } -} - pub mod stream { //! Features for streams.