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-08 23:27:38 +02:00
parent a46aaff8b0
commit 990af9e484
3 changed files with 58 additions and 41 deletions

View File

@ -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::<Vec<_>>();
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<Item = &'a Comment>,
current: impl IntoIterator<Item = Comment>,
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::<Vec<_>>()[..].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],

View File

@ -77,7 +77,7 @@ pub struct ListItem<T: for<'a> Deserialize<'a>> {
pub struct Comments {
pub url: String,
continuation: Option<String>,
buffer: Option<Result<Box<Iterator<Item=Result<Comment, Box<Error>>>>, Box<Error>>>,
buffer: Option<Result<Box<Iterator<Item = Result<Comment, Box<Error>>>>, Box<Error>>>,
}
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,
}
}
}

View File

@ -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")]