aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorTeddy Wing2020-08-02 04:31:11 +0200
committerTeddy Wing2020-08-02 04:31:11 +0200
commit38a871f28bad90e238021d8cc46b9fa926f9df75 (patch)
tree5ae0e9ecf8fec46c7a48c4665b333833310c33d1 /src
parent13710b49cc40c0e78eec9a94ce9b0ea8a09c3860 (diff)
downloadgit-suggestion-38a871f28bad90e238021d8cc46b9fa926f9df75.tar.bz2
git-sugpatch: Error if no remote and suggestion ID argument
We want to allow not having a remote when URL arguments are given, but require it when a suggestion ID argument is given (otherwise we wouldn't have an owner-repo pair to make the GitHub request). Had some trouble with the `OwnerRepo.o_r` value. It was being moved into the closure, so tried a loop. There was a similar problem with the loop. However, by returning, I was able to get a reference to the `Result` instead of having it be moved.
Diffstat (limited to 'src')
-rw-r--r--src/bin/git-sugpatch.rs158
-rw-r--r--src/config.rs8
-rw-r--r--src/error.rs5
-rw-r--r--src/lib.rs3
4 files changed, 124 insertions, 50 deletions
diff --git a/src/bin/git-sugpatch.rs b/src/bin/git-sugpatch.rs
index 0159307..4560c9a 100644
--- a/src/bin/git-sugpatch.rs
+++ b/src/bin/git-sugpatch.rs
@@ -4,10 +4,9 @@ use std::process;
use exitcode;
use github_suggestion::{Client, Suggestion, SuggestionUrl};
-use github_suggestion_cli::gseprintln;
+use github_suggestion_cli::{gseprintln, is_suggestion_id, owner_repo};
use github_suggestion_cli::config::Config;
use github_suggestion_cli::error::Error;
-use github_suggestion_cli::is_suggestion_id;
fn main() {
@@ -18,7 +17,7 @@ fn main() {
Err(e) => {
gseprintln!(e);
- process::exit(exitcode::DATAERR);
+ process::exit(exitcode::CONFIG);
},
};
@@ -26,52 +25,125 @@ fn main() {
process::exit(111);
}
- let suggestions: Vec<Result<Suggestion, Error>> = config.suggestions
- .iter()
- .map(|s| {
- let suggestion = if is_suggestion_id(s)? {
- let client = Client::new(
- &config.github_token,
- &config.owner,
- &config.repo,
- ).unwrap();
-
- client.fetch(&s).unwrap()
- } else {
- let url: SuggestionUrl = args[1].parse().unwrap();
-
- let client = Client::new(
- &config.github_token,
- &url.owner,
- &url.repo,
- ).unwrap();
-
- client.fetch(&url.comment_id).unwrap()
+ // let mut owner_repo_error: owner_repo::Error;
+ // let o_r = match config.o_r {
+ // Ok(o_r) => o_r,
+ // Err(e @ owner_repo::Error::NoRemote(_)) => owner_repo_error = e,
+ // Err(e) => {
+ // gseprintln!(e);
+ //
+ // process::exit(111);
+ // },
+ // };
+
+ for suggestion_arg in config.suggestions {
+ let suggestion = if match is_suggestion_id(&suggestion_arg) {
+ Ok(p) => p,
+ Err(e) => {
+ gseprintln!(e);
+
+ process::exit(exitcode::SOFTWARE);
+ }
+ } {
+ // let o_r = match config.o_r {
+ // Ok(o_r) => o_r,
+ // Err(e) => {
+ // gseprintln!(e);
+ //
+ // process::exit(exitcode::CONFIG);
+ // }
+ // };
+ // let o_r = match owner_repo {
+ // Ok(o_r) => o_r,
+ // Err(e) => {
+ // gseprintln!(e);
+ //
+ // process::exit(exitcode::CONFIG);
+ // },
+ // };
+ // if let owner_repo::Error::NoRemote(e) = owner_repo_error {
+ // gseprintln!(e);
+ //
+ // process::exit(exitcode::CONFIG);
+ // }
+ let o_r = match &config.o_r {
+ Ok(o_r) => o_r,
+ Err(e) => {
+ gseprintln!(e);
+ process::exit(exitcode::CONFIG);
+ },
};
- Ok(suggestion)
- })
- .collect();
+ let client = Client::new(
+ &config.github_token,
+ &o_r.owner,
+ &o_r.repo,
+ ).unwrap();
- let errors: Vec<&Error> = suggestions.iter()
- .filter(|r| r.is_err())
+ client.fetch(&suggestion_arg).unwrap()
+ } else {
+ let url: SuggestionUrl = args[1].parse().unwrap();
- // We know these `Results` are `Err`s.
- .map(|r| r.as_ref().err().unwrap())
- .collect();
+ let client = Client::new(
+ &config.github_token,
+ &url.owner,
+ &url.repo,
+ ).unwrap();
- if !errors.is_empty() {
- for error in errors {
- eprintln!("error: {}", error);
- }
+ client.fetch(&url.comment_id).unwrap()
+ };
- return;
+ print!("{}", suggestion.diff().unwrap());
}
- suggestions
- .iter()
-
- // We've already checked for `Err`s above.
- .map(|r| r.as_ref().unwrap())
- .for_each(|s| print!("{}", s.diff().unwrap()));
+ // let suggestions: Vec<Result<Suggestion, Error>> = config.suggestions
+ // .iter()
+ // .map(|s| {
+ // let suggestion = if is_suggestion_id(s)? {
+ // let o_r = owner_repo?;
+ //
+ // let client = Client::new(
+ // &config.github_token,
+ // &o_r.owner,
+ // &o_r.repo,
+ // ).unwrap();
+ //
+ // client.fetch(&s).unwrap()
+ // } else {
+ // let url: SuggestionUrl = args[1].parse().unwrap();
+ //
+ // let client = Client::new(
+ // &config.github_token,
+ // &url.owner,
+ // &url.repo,
+ // ).unwrap();
+ //
+ // client.fetch(&url.comment_id).unwrap()
+ // };
+ //
+ // Ok(suggestion)
+ // })
+ // .collect();
+ //
+ // let errors: Vec<&Error> = suggestions.iter()
+ // .filter(|r| r.is_err())
+ //
+ // // We know these `Results` are `Err`s.
+ // .map(|r| r.as_ref().err().unwrap())
+ // .collect();
+ //
+ // if !errors.is_empty() {
+ // for error in errors {
+ // eprintln!("error: {}", error);
+ // }
+ //
+ // return;
+ // }
+ //
+ // suggestions
+ // .iter()
+ //
+ // // We've already checked for `Err`s above.
+ // .map(|r| r.as_ref().unwrap())
+ // .for_each(|s| print!("{}", s.diff().unwrap()));
}
diff --git a/src/config.rs b/src/config.rs
index 7fe9695..bbee2c9 100644
--- a/src/config.rs
+++ b/src/config.rs
@@ -27,8 +27,7 @@ pub enum Error {
#[derive(Debug)]
pub struct Config {
pub github_token: String,
- pub owner: String,
- pub repo: String,
+ pub o_r: Result<OwnerRepo, owner_repo::Error>,
pub suggestions: Vec<String>,
}
@@ -46,12 +45,11 @@ impl Config {
let o_r = OwnerRepo::from_remote(
Self::remote(&opt_matches, &git_config)?.as_deref(),
- )?;
+ );
Ok(Config {
github_token: Self::github_token(&opt_matches, &git_config)?,
- owner: o_r.owner,
- repo: o_r.repo,
+ o_r: o_r,
suggestions: opt_matches.free,
})
}
diff --git a/src/error.rs b/src/error.rs
index bcca3e1..3df5de7 100644
--- a/src/error.rs
+++ b/src/error.rs
@@ -1,11 +1,16 @@
use regex;
use thiserror::Error;
+use crate::owner_repo;
+
#[derive(Debug, Error)]
pub enum Error {
#[error("Unable to parse regex")]
Regex(#[from] regex::Error),
+
+ #[error(transparent)]
+ NoRemote(#[from] owner_repo::Error),
}
diff --git a/src/lib.rs b/src/lib.rs
index 0a8ab13..af0e7bb 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -2,8 +2,7 @@
pub mod config;
pub mod error;
-
-pub(crate) mod owner_repo;
+pub mod owner_repo;
mod arg;