aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTeddy Wing2020-07-21 01:27:37 +0200
committerTeddy Wing2020-07-21 01:32:41 +0200
commit1e883be33f61818e941dd2a147ce1bba85d178ce (patch)
treee002fea57732f01644a9e9a662d2dfbd94b84c91
parentd65b1ab87e4d3dbd7bf7ade653dd64dc5b715368 (diff)
downloadgit-suggestion-1e883be33f61818e941dd2a147ce1bba85d178ce.tar.bz2
Suggesstion: Implement `patch` to generate a patch for the suggestion
Still working on it, but have an initial draft working. There's no file name in the diff hunk, so I'm assuming I'm going to have to add one later. Build a patch from the diff hunk and suggestion comment. 1. Remove `-` lines 2. Change `+` lines to ` ` 3. Change last line to `-` 4. Append suggestion to the diff with a `+` prefix
-rw-r--r--Cargo.lock43
-rw-r--r--Cargo.toml1
-rw-r--r--src/lib.rs62
3 files changed, 105 insertions, 1 deletions
diff --git a/Cargo.lock b/Cargo.lock
index c8695c5..ce31c89 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -16,6 +16,15 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ee2a4ec343196209d6594e19543ae87a39f96d5534d7174822a3ad825dd6ed7e"
[[package]]
+name = "aho-corasick"
+version = "0.7.13"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "043164d8ba5c4c3035fec9bbee8647c0261d788f3474306f93bb65901cae0e86"
+dependencies = [
+ "memchr",
+]
+
+[[package]]
name = "autocfg"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -210,6 +219,7 @@ name = "git-suggested-patch"
version = "0.0.1"
dependencies = [
"github-rs",
+ "regex",
"serde",
"serde_json",
"thiserror",
@@ -414,6 +424,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "60302e4db3a61da70c0cb7991976248362f30319e88850c487b9b95bbf059e00"
[[package]]
+name = "memchr"
+version = "2.3.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "3728d817d99e5ac407411fa471ff9800a778d88a24685968b36824eaf4bee400"
+
+[[package]]
name = "memoffset"
version = "0.5.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -551,6 +567,24 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "41cc0f7e4d5d4544e8861606a285bb08d3e70712ccc7d2b84d7c0ccfaf4b05ce"
[[package]]
+name = "regex"
+version = "1.3.9"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "9c3780fcf44b193bc4d09f36d2a3c87b251da4a046c87795a0d35f4f927ad8e6"
+dependencies = [
+ "aho-corasick",
+ "memchr",
+ "regex-syntax",
+ "thread_local",
+]
+
+[[package]]
+name = "regex-syntax"
+version = "0.6.18"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "26412eb97c6b088a6997e05f69403a802a92d520de2f8e63c2b65f9e0f47c4e8"
+
+[[package]]
name = "ring"
version = "0.14.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -729,6 +763,15 @@ dependencies = [
]
[[package]]
+name = "thread_local"
+version = "1.0.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "d40c6d1b69745a6ec6fb1ca717914848da4b44ae29d9b3080cbee91d72a69b14"
+dependencies = [
+ "lazy_static",
+]
+
+[[package]]
name = "time"
version = "0.1.43"
source = "registry+https://github.com/rust-lang/crates.io-index"
diff --git a/Cargo.toml b/Cargo.toml
index 651abdc..f00d14f 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -5,6 +5,7 @@ edition = "2018"
[dependencies]
github-rs = "0.7.0"
+regex = "1.3.9"
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 9343b1f..839ad0b 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -2,6 +2,7 @@
use github_rs::client::{Executor, Github};
+use regex::Regex;
use serde::Deserialize;
use serde_json::Value;
use thiserror::Error;
@@ -63,7 +64,31 @@ pub struct Suggestion {
}
impl Suggestion {
- pub fn patch(&self) {
+ pub fn patch(&self) -> String {
+ let mut diff: Vec<_> = self.diff.lines()
+ .filter(|l| !l.starts_with("-"))
+ .map(|l| {
+ if l.starts_with("+") {
+ return l.replacen("+", " ", 1);
+ }
+
+ l.to_owned()
+ })
+ .collect();
+
+ let last = diff.len() - 1;
+ diff[last] = diff.last().unwrap()
+ .replacen(" ", "-", 1);
+
+ diff.push(self.suggestion());
+
+ diff.join("\n")
+ }
+
+ fn suggestion(&self) -> String {
+ let re = Regex::new(r"(?s).*```\s*suggestion\n").unwrap();
+ let s = re.replace(&self.suggestion, "+");
+ s.replace("```", "")
}
}
@@ -83,4 +108,39 @@ mod tests {
println!("{:?}", suggestion);
}
+
+ #[test]
+ fn suggestion_patch_generates_patch() {
+ let suggestion = Suggestion {
+ diff: r#"@@ -1, 9 +1, 11 @@
+ package command
+
+ import (
++ "bufio" // used to input comment
+ "errors"
+ "fmt"
+ "io"
++ "os" // used to input comment"#.to_owned(),
+ suggestion: r#"It's ok to leave these uncommented
+
+```suggestion
+ "os"
+```"#.to_owned(),
+ };
+
+ assert_eq!(
+ suggestion.patch(),
+ r#"@@ -1, 9 +1, 11 @@
+ package command
+
+ import (
+ "bufio" // used to input comment
+ "errors"
+ "fmt"
+ "io"
+- "os" // used to input comment
++ "os"
+"#,
+ );
+ }
}