diff options
| author | Teddy Wing | 2020-07-19 23:08:45 +0200 | 
|---|---|---|
| committer | Teddy Wing | 2020-07-19 23:08:45 +0200 | 
| commit | d65b1ab87e4d3dbd7bf7ade653dd64dc5b715368 (patch) | |
| tree | 6debc9869ba2b3aa9808904f5f27fd25ebda6f1c | |
| parent | f87ad1230864c271a4e60ad3b6ff3abea950568d (diff) | |
| download | git-suggestion-d65b1ab87e4d3dbd7bf7ade653dd64dc5b715368.tar.bz2 | |
Extract diff hunk and comment body from GitHub response
Split the `Suggestion` struct into a new `Client` struct for the GitHub
request, and `Suggestion` for the required response data.
| -rw-r--r-- | Cargo.lock | 15 | ||||
| -rw-r--r-- | Cargo.toml | 1 | ||||
| -rw-r--r-- | src/lib.rs | 41 | 
3 files changed, 46 insertions, 11 deletions
| @@ -210,6 +210,7 @@ name = "git-suggested-patch"  version = "0.0.1"  dependencies = [   "github-rs", + "serde",   "serde_json",   "thiserror",  ] @@ -640,6 +641,20 @@ name = "serde"  version = "1.0.114"  source = "registry+https://github.com/rust-lang/crates.io-index"  checksum = "5317f7588f0a5078ee60ef675ef96735a1442132dc645eb1d12c018620ed8cd3" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde_derive" +version = "1.0.114" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2a0be94b04690fbaed37cddffc5c134bf537c8e3329d53e982fe04c374978f8e" +dependencies = [ + "proc-macro2", + "quote", + "syn", +]  [[package]]  name = "serde_json" @@ -5,5 +5,6 @@ edition = "2018"  [dependencies]  github-rs = "0.7.0" +serde = { version = "1.0.114", features = ["derive"] }  serde_json = "1.0.56"  thiserror = "1.0.20" @@ -2,6 +2,7 @@  use github_rs::client::{Executor, Github}; +use serde::Deserialize;  use serde_json::Value;  use thiserror::Error; @@ -10,24 +11,27 @@ use thiserror::Error;  pub enum Error {      #[error("GitHub client error: {0}")]      Github(String), + +    #[error("Unable to deserialize")] +    Deserialize(#[from] serde_json::error::Error),  } -pub struct Suggestion<'a> { +pub struct Client<'a> {      client: Github,      owner: &'a str,      repo: &'a str,  } -impl<'a> Suggestion<'a> { +impl<'a> Client<'a> {      pub fn new(token: &str, owner: &'a str, repo: &'a str) -> Self {          let client = Github::new(&token).unwrap(); -        Suggestion { client, owner, repo } +        Client { client, owner, repo }      } -    pub fn fetch(&self, id: &str) -> Result<(), Error> { -        let comment = self.client +    pub fn fetch(&self, id: &str) -> Result<Suggestion, Error> { +        let response = self.client              .get()              .repos()              .owner(self.owner) @@ -37,16 +41,28 @@ impl<'a> Suggestion<'a> {              .id(id)              .execute::<Value>(); -        match comment { -            Ok((_, _, json)) => { -                println!("{:?}", json); +        match response { +            Ok((_, _, Some(json))) => { +                let suggestion = serde_json::from_value(json)?; -                Ok(()) +                Ok(suggestion)              }, +            Ok((_, _, None)) => Err(Error::Github("no response".to_owned())),              Err(e) => Err(Error::Github(e.to_string())),          }      } +} +#[derive(Debug, Deserialize)] +pub struct Suggestion { +    #[serde(rename = "diff_hunk")] +    diff: String, + +    #[serde(rename = "body")] +    suggestion: String, +} + +impl Suggestion {      pub fn patch(&self) {      }  } @@ -57,11 +73,14 @@ mod tests {      #[test]      fn suggestion_fetch_gets_pull_request_comment() { -        let suggestion = Suggestion::new( +        let client = Client::new(              env!("GITHUB_TOKEN"),              "cli",              "cli",          ); -        suggestion.fetch("438947607").unwrap(); + +        let suggestion = client.fetch("438947607").unwrap(); + +        println!("{:?}", suggestion);      }  } | 
