google_maps2/request_rate/
current_rate.rs

1//! Contains the `CurrentRate` struct and its associated traits. Used to track
2//! the current effective request rate.
3
4use crate::request_rate::rate_to_string::rate_to_string;
5use std::time::Instant;
6
7/// Contains the current request rate in the form of _requests_ since _first
8/// request_.
9#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
10pub struct CurrentRate {
11    /// The _instant_ of the first request to the API. If the API has not been
12    /// called, the `Option` value will be `None`.
13    pub first_request: Option<Instant>,
14    /// The _count_ of total number of requests to the API since the first
15    /// request.
16    pub request_count: u64,
17} // struct
18
19impl std::convert::From<&CurrentRate> for String {
20    /// Converts a `CurrentRate` enum to a `String` that contains a
21    /// human-friendly & readable rate.
22    fn from(current_rate: &CurrentRate) -> Self {
23        current_rate.first_request.map_or_else(
24            || Self::from("None"),
25            |first_request| {
26                rate_to_string(
27                    current_rate.request_count,
28                    &first_request.elapsed(),
29                    "request",
30                    "requests",
31                )
32            }, // rate_to_string
33        ) // map_or_else
34    } // fn
35} // impl
36
37impl std::default::Default for CurrentRate {
38    /// Returns a reasonable default values for the `CurrentRate` struct.
39    fn default() -> Self {
40        Self {
41            first_request: None,
42            request_count: 0,
43        } // struct
44    } // fn
45} // impl
46
47impl std::fmt::Display for CurrentRate {
48    /// Formats a `CurrentRate` enum into a string that is presentable to the
49    /// end user.
50    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
51        write!(f, "{}", String::from(self))
52    } // fn
53} // impl