diff --git a/src/main.rs b/src/main.rs index 1f51afa..9b9b9a5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -24,16 +24,12 @@ use crate::opts::*; fn main() { let opts = Opts::from_args(); let comments = Comments::for_user(&opts.redditor); - let all = comments - .take(opts.fetch) - .filter_map(|c| c.ok()) - .collect::>(); println!( "Hello, world! {:?}", update( &opts.repo.unwrap(), - &comments.take(opts.fetch).filter_map(|c| c.ok()), + comments.take(opts.fetch).filter_map(|c| c.ok()), &opts.redditor, &("reddit.com/u/".to_owned() + &opts.redditor), (opts.threshold, opts.thresholdp) @@ -43,7 +39,7 @@ fn main() { fn update<'a>( repo: &PathBuf, - current: impl IntoIterator, + current: impl IntoIterator, redditor: &str, email: &str, threshold: (u32, u8), @@ -87,14 +83,13 @@ fn update<'a>( if delta.len() > 0 { fs_write(&path, to_string_pretty(&comment)?)?; commit_msg = delta.iter().map(|d| d.to_string()).collect::>()[..].join("\n"); - index.update_all(vec![&path], None)?; - + //index.update_all(vec![&path], None)?; updated += 1; } } else { create_dir_all((&path).parent().unwrap())?; fs_write(&path, to_string_pretty(&comment)?)?; - index.add_all(vec![&path], git2::IndexAddOption::DEFAULT, None)?; + //index.add_all(vec![&path], git2::IndexAddOption::DEFAULT, None)?; commit_msg = CommentDelta::New.to_string(); updated += 1; } @@ -135,7 +130,7 @@ fn update<'a>( git.commit( Some("HEAD"), &sig_backdate, - &sig_backdate, + &sig, &commit_msg, &tree, &[&parent], diff --git a/src/model.rs b/src/model.rs index 7897375..3d78f06 100644 --- a/src/model.rs +++ b/src/model.rs @@ -77,7 +77,7 @@ pub struct ListItem Deserialize<'a>> { pub struct Comments { pub url: String, continuation: Option, - buffer: Option>>>, Box>>, + buffer: Option>>>, Box>>, } impl Comments { @@ -126,34 +126,41 @@ impl Iterator for Comments { //return Ok((comments.iter().map(|li| li.data).collect(), continuation)); } - if let (Some(ref mut buffer), ref mut continuation, ref url) = - (&mut self.buffer, &mut self.continuation, &self.url) - { - if let Some(comment) = buffer.next() { - Some(comment) - } else { - continuation.clone().and_then(|cont| { - let page = request_paged(&(url.to_string() + "?after=" + &cont[..])); - match page { - Ok((comments, cont)) => { - **continuation = cont; - *buffer = Box::new(comments.into_iter().map(Ok)) - } - Err(e) => *buffer = Box::new(iter::once(Err(e))), - }; - buffer.next() - }) - } - } else { - let page = request_paged(&self.url); - match page { - Ok((comments, cont)) => { - self.continuation = cont; - self.buffer = Some(Box::new(comments.into_iter().map(Ok))) + match (&mut self.buffer, &mut self.continuation, &self.url) { + (Some(Ok(ref mut buffer)), ref mut continuation, ref url) => { + if let Some(comment) = buffer.next() { + Some(comment) + } else { + continuation.clone().and_then(|cont| { + let page = request_paged(&(url.to_string() + "?after=" + &cont[..])); + match page { + Ok((comments, cont)) => { + **continuation = cont; + *buffer = Box::new(comments.into_iter().map(Ok)) + } + Err(e) => *buffer = Box::new(iter::once(Err(e))), + }; + buffer.next() + }) } - Err(e) => self.buffer = Some(Box::new(iter::once(Err(e)))), - }; - self.buffer.as_mut().and_then(|it| it.next()) + } + (None, _, _) => { + let page = request_paged(&self.url); + match page { + Ok((comments, cont)) => { + self.continuation = cont; + self.buffer = Some(Ok(Box::new(comments.into_iter().map(Ok)))) + } + Err(e) => self.buffer = Some(Err(e)), + }; + let buf = self.buffer.as_mut()?; + if let Ok(ref mut buf) = buf { + buf.next() + } else { + None + } + } + (Some(Err(_)), _, _) => None, } } } diff --git a/src/opts.rs b/src/opts.rs index a701676..d0b4643 100644 --- a/src/opts.rs +++ b/src/opts.rs @@ -4,13 +4,28 @@ pub use structopt::StructOpt; #[derive(StructOpt, Debug)] #[structopt(name = "gitredditor")] pub struct Opts { - #[structopt(short = "f", long = "fetch", default_value = "20", env = "GITREDDITOR_CNT")] + #[structopt( + short = "f", + long = "fetch", + default_value = "20", + env = "GITREDDITOR_CNT" + )] pub fetch: usize, - #[structopt(short = "t", long = "threshold", default_value = "5", env = "GITREDDITOR_TH")] + #[structopt( + short = "t", + long = "threshold", + default_value = "5", + env = "GITREDDITOR_TH" + )] pub threshold: u32, - #[structopt(short = "p", long = "threshold-percent", default_value = "5", env = "GITREDDITOR_THP")] + #[structopt( + short = "p", + long = "threshold-percent", + default_value = "5", + env = "GITREDDITOR_THP" + )] pub thresholdp: u8, #[structopt(short = "r", long = "redditor", env = "GITREDDITOR_U")]