aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--Cargo.lock15
-rw-r--r--Cargo.toml1
-rw-r--r--src/lib.rs41
3 files changed, 46 insertions, 11 deletions
diff --git a/Cargo.lock b/Cargo.lock
index a1940f2..c8695c5 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -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"
diff --git a/Cargo.toml b/Cargo.toml
index 2c40820..651abdc 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -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"
diff --git a/src/lib.rs b/src/lib.rs
index c0aeb3b..9343b1f 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -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);
}
}