use std::collections::HashMap;
use std::default::Default;
use std::str::FromStr;
pub type HttpHeaderType = HashMap<String, Vec<String>>;
pub trait Populatable {
fn populate(raw_header: &HttpHeaderType) -> Self;
}
#[derive(Debug)]
pub struct Rate {
pub limit: u32,
pub remaining: u32,
pub reset: String
}
#[derive(Debug)]
pub struct Page {
pub number: u64,
}
#[derive(Debug)]
pub struct Response {
pub resp: HttpHeaderType,
pub next: Option<Page>,
pub last: Option<Page>,
pub first: Option<Page>,
pub prev: Option<Page>,
pub rate: Rate,
}
fn get_single_header_value<T>(raw_data: &HttpHeaderType, key: &str) -> T where T: Default + FromStr {
match str::parse(&raw_data[key][0]) {
Ok(x) => x,
Err(..) => Default::default(),
}
}
impl Populatable for Rate {
fn populate(raw_header: &HttpHeaderType) -> Rate {
Rate {
limit: get_single_header_value(raw_header, "x-ratelimit-limit"),
remaining: get_single_header_value(raw_header, "x-ratelimit-remaining"),
reset: raw_header["x-ratelimit-reset"][0].clone(),
}
}
}
impl Populatable for Response {
fn populate(raw_header: &HttpHeaderType) -> Response {
Response {
next: None,
last: None,
first: None,
prev: None,
rate: Rate::populate(raw_header),
resp: raw_header.clone(),
}
}
}