1
0
mirror of https://github.com/shimunn/gitredditor.git synced 2023-11-17 18:42:43 +01:00
This commit is contained in:
shimunn
2019-05-05 17:23:46 +02:00
commit e26ee189d7
4 changed files with 1676 additions and 0 deletions

51
src/model.rs Normal file
View File

@@ -0,0 +1,51 @@
use serde::{Deserialize, Serialize};
use std::error::Error;
use std::iter;
use std::iter::Chain;
#[derive(Serialize, Deserialize, Debug)]
pub struct Comment {
score: u32,
id: String,
created: u64,
permalink: String,
edited: bool,
}
pub struct Comments {
pub url: String,
continuation: Option<String>,
buffer: Box<Iterator<Item = Result<Comment, Box<Error>>>>,
}
impl Iterator for Comments {
type Item = Result<Comment, Box<Error>>;
fn next(&mut self) -> Option<Self::Item> {
fn request_paged(url: &str) -> Result<(Vec<Comment>, Option<String>), Box<Error>> {
let comment_json = reqwest::get(url)?.text()?;
let continuation: Option<String> = unimplemented!();
let comments: Vec<Comment> = unimplemented!();
return Ok((comments, continuation));
}
if let Some(comment) = self.buffer.next() {
Some(comment)
} else {
self.continuation.clone().and_then(|cont| {
let page = request_paged(&self.url);
match page {
Ok((comments, cont)) => {
self.continuation = cont;
self.buffer = Box::new(comments.into_iter().map(Ok))
}
Err(e) => self.buffer = Box::new(iter::once(Err(e))),
};
self.buffer.next()
})
}
}
}