use std::vec::IntoIter;
use std::collections::VecDeque;
use std::thread;
use std::time::Duration;
use responses::listing;
use client::RedditClient;
use structures::submission::Submission;
use traits::{Content, PageListing};
use errors::APIError;
pub struct Listing<'a> {
client: &'a RedditClient,
query_stem: String,
data: listing::ListingData<listing::Submission>
}
impl<'a> Listing<'a> {
pub fn new(client: &RedditClient, query_stem: String, data: listing::ListingData<listing::Submission>) -> Listing {
Listing {
client: client,
query_stem: query_stem,
data: data
}
}
}
impl<'a> PageListing for Listing<'a> {
fn before(&self) -> Option<String> {
self.data.before.to_owned()
}
fn after(&self) -> Option<String> {
self.data.after.to_owned()
}
fn modhash(&self) -> Option<String> {
self.data.modhash.to_owned()
}
}
impl<'a> Listing<'a> {
fn fetch_after(&mut self) -> Result<Listing<'a>, APIError> {
match self.after() {
Some(after_id) => {
let url = format!("{}&after={}", self.query_stem, after_id);
self.client.get_json::<listing::Listing>(&url, false)
.and_then(|res| Ok(Listing::new(self.client, self.query_stem.to_owned(), res.data)))
},
None => Err(APIError::ExhaustedListing)
}
}
}
impl<'a> Iterator for Listing<'a> {
type Item = Submission<'a>;
fn next(&mut self) -> Option<Submission<'a>> {
if self.data.children.is_empty() {
if self.after().is_none() {
None
} else {
let mut new_listing = self.fetch_after().expect("After does not exist!");
self.data.children.append(&mut new_listing.data.children);
self.data.after = new_listing.data.after;
self.next()
}
} else {
let child = self.data.children.drain(..1).next().unwrap();
Some(Submission::new(self.client, child.data))
}
}
}
pub struct PostStream<'a> {
client: &'a RedditClient,
set: VecDeque<String>,
current_iter: Option<IntoIter<Submission<'a>>>,
url: String
}
impl<'a> PostStream<'a> {
pub fn new(client: &'a RedditClient, url: String) -> PostStream<'a> {
PostStream {
set: VecDeque::new(),
current_iter: None,
client: client,
url: url
}
}
}
impl<'a> Iterator for PostStream<'a> {
type Item = Submission<'a>;
fn next(&mut self) -> Option<Submission<'a>> {
if self.current_iter.is_some() {
let mut iter = self.current_iter.take().unwrap();
let next_iter = iter.next();
if next_iter.is_some() {
let res = next_iter.unwrap();
let name = res.name().to_owned();
let mut contains = false;
for item in &self.set {
if item == &name {
contains = true;
}
}
if contains {
self.current_iter = Some(iter);
self.next()
} else {
self.set.push_back(name);
if self.set.len() > 10 {
self.set.pop_front();
}
self.current_iter = Some(iter);
Some(res)
}
} else {
self.next()
}
} else {
thread::sleep(Duration::new(5, 0));
let req: Result<listing::Listing, APIError> = self.client.get_json(&self.url, false);
let current_iter = if let Ok(res) = req {
Some(res.data.children.into_iter().map(|i| {
Submission::new(self.client, i.data)
}).rev().collect::<Vec<Submission<'a>>>().into_iter())
} else {
None
};
self.current_iter = current_iter;
self.next()
}
}
}