twilight_http_ratelimiting::headers

Enum RatelimitHeaders

source
#[non_exhaustive]
pub enum RatelimitHeaders { Global(Global), None, Present(Present), }
Expand description

Parsed headers.

The headers of a response may either indicate a global ratelimit, a bucketed ratelimit, or no ratelimit at all.

Variants (Non-exhaustive)§

This enum is marked as non-exhaustive
Non-exhaustive enums could have additional variants added in future. Therefore, when matching against variants of non-exhaustive enums, an extra wildcard arm must be added to account for any future variants.
§

Global(Global)

Global ratelimit encountered.

§

None

No ratelimit headers present.

§

Present(Present)

Bucketed ratelimit present.

Implementations§

source§

impl RatelimitHeaders

source

pub const fn is_global(&self) -> bool

Whether the ratelimit headers are global.

source

pub const fn is_none(&self) -> bool

Whether there are no ratelimit headers.

source

pub const fn is_present(&self) -> bool

Whether the ratelimit headers are for a bucket.

source

pub fn from_pairs<'a>( headers: impl Iterator<Item = (&'a str, &'a [u8])>, ) -> Result<Self, HeaderParsingError>

Parse headers from an iterator of tuples containing the header name and value.

Headers names must be lowercase.

§Examples

Parse a standard list of headers from a response:

use twilight_http_ratelimiting::RatelimitHeaders;

let headers = [
    (
        "x-ratelimit-bucket",
        "d721dea6054f6322373d361f98e5c38b".as_bytes(),
    ),
    ("x-ratelimit-limit", "10".as_bytes()),
    ("x-ratelimit-remaining", "9".as_bytes()),
    ("x-ratelimit-reset", "1573795260.333".as_bytes()),
    ("x-ratelimit-reset-after", "10.000".as_bytes()),
];

let headers = RatelimitHeaders::from_pairs(headers.into_iter())?;
assert!(matches!(
    headers,
    RatelimitHeaders::Present(p) if p.remaining() == 9,
));

Parse a list of headers denoting that the user has been globally ratelimited:

use twilight_http_ratelimiting::RatelimitHeaders;

let headers = [
    ("retry-after", "487".as_bytes()),
    ("x-ratelimit-global", "true".as_bytes()),
];

let headers = RatelimitHeaders::from_pairs(headers.into_iter())?;
assert!(matches!(
    headers,
    RatelimitHeaders::Global(g) if g.retry_after() == 487,
));
§Errors

Errors if a required header is missing or if a header value is of an invalid type.

Trait Implementations§

source§

impl Clone for RatelimitHeaders

source§

fn clone(&self) -> RatelimitHeaders

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for RatelimitHeaders

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> CloneToUninit for T
where T: Clone,

source§

unsafe fn clone_to_uninit(&self, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for T
where T: Clone,

source§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

source§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more