use std::{
self,
collections::HashMap,
io::Write,
};
use serde::Deserialize;
use super::super::{
response::TwitchResult,
TwitchClient,
};
pub fn top(c: &TwitchClient) -> TwitchResult<TopGames> {
let iter = TopGames {
client: c,
cur: None,
offset: 0,
};
Ok(iter)
}
pub struct TopGames<'c> {
client: &'c TwitchClient,
cur: Option<SerdeTopGames>,
offset: i32,
}
#[derive(Deserialize, Debug)]
pub struct TopGame {
pub channels: i32,
pub viewers: i32,
pub game: Game,
}
#[derive(Deserialize, Debug)]
pub struct Game {
#[serde(rename = "_id")]
pub id: i64,
#[serde(rename = "box")]
pub _box: HashMap<String, String>,
pub giantbomb_id: i64,
pub logo: HashMap<String, String>,
pub name: String,
#[serde(default)]
pub popularity: i32,
}
#[derive(Deserialize, Debug)]
struct SerdeTopGames {
top: Vec<TopGame>,
}
impl<'c> Iterator for TopGames<'c> {
type Item = TopGame;
fn next(&mut self) -> Option<TopGame> {
let url = &format!("/games/top?limit=100&offset={}", self.offset);
next_result!(self, &url, SerdeTopGames, top)
}
}
#[cfg(test)]
mod tests {
use crate::{
new,
tests::CLIENTID,
};
#[test]
fn top() {
let c = new(String::from(CLIENTID));
let mut r = super::top(&c).unwrap();
r.next();
}
}