libtwitch-rs 0.3.0

Rust library for the Twitch APIv5 (Kraken)
Documentation
// This file was ((taken|adapted)|contains (data|code)) from twitch_api,
// Copyright 2017 Matt Shanker
// It's licensed under the Apache License, Version 2.0.
// You may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// (Modifications|Other (data|code)|Everything else) Copyright 2019 the
// libtwitch-rs authors.  See copying.md for further legal info.

use std::{
    self,
    collections::HashMap,
    io::Write,
};

use serde::Deserialize;

use super::super::{
    response::TwitchResult,
    TwitchClient,
};

/// Gets games sorted by number of current viewers on Twitch, most popular first
///
/// #### Authentication: `None`
pub fn top(c: &TwitchClient) -> TwitchResult<TopGames> {
    let iter = TopGames {
        client: c,
        cur: None,
        offset: 0,
    };
    Ok(iter)
}

///////////////////////////////////////
// GetTopGames
///////////////////////////////////////
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)
    }
}

///////////////////////////////////////
// TESTS
///////////////////////////////////////

#[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();
    }
}