use std::convert::Infallible;
#[derive(Debug, Hash, Default, Clone, Copy, PartialEq, PartialOrd, Eq, Ord)]
pub struct RateLimit {
pub limit_minute: usize,
pub limit_month: usize,
pub remainig_minute: usize,
pub remaining_month: usize,
}
pub struct RateLimitIgnore;
impl TryFrom<&reqwest::Response> for RateLimit {
type Error = ();
fn try_from(value: &reqwest::Response) -> Result<Self, Self::Error> {
let headers = value.headers();
let h = |name| {
headers
.get(name)
.ok_or(())
.and_then(|value| atoi::atoi(value.as_bytes()).ok_or(()))
};
Ok(Self {
limit_minute: h("X-RateLimit-Limit-Quota-Minute")?,
limit_month: h("X-RateLimit-Limit-Quota-Month")?,
remainig_minute: h("X-RateLimit-Remaining-Quota-Minute")?,
remaining_month: h("X-RateLimit-Remaining-Quota-Month")?,
})
}
}
impl TryFrom<&reqwest::Response> for RateLimitIgnore {
type Error = Infallible;
#[inline] fn try_from(_: &reqwest::Response) -> Result<Self, Self::Error> { Ok(RateLimitIgnore) }
}
mod private {
use super::*;
pub trait Sealed<'a>: TryFrom<&'a reqwest::Response> {}
impl<'a> Sealed<'a> for RateLimit {}
impl<'a> Sealed<'a> for RateLimitIgnore {}
}
pub trait RateLimitData<'a>: private::Sealed<'a> {}
impl<'a> RateLimitData<'a> for RateLimit {}
impl<'a> RateLimitData<'a> for RateLimitIgnore {}