diff options
| -rw-r--r-- | src/bin/git-sugpatch.rs | 158 | ||||
| -rw-r--r-- | src/config.rs | 8 | ||||
| -rw-r--r-- | src/error.rs | 5 | ||||
| -rw-r--r-- | src/lib.rs | 3 | 
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),  } @@ -2,8 +2,7 @@  pub mod config;  pub mod error; - -pub(crate) mod owner_repo; +pub mod owner_repo;  mod arg; | 
