diff options
author | Teddy Wing | 2020-07-25 22:51:22 +0200 |
---|---|---|
committer | Teddy Wing | 2020-07-25 22:51:22 +0200 |
commit | b4a7561ccc1ed7dab6cd2c786902dec8c4ad9be6 (patch) | |
tree | 34a4d4eee09ed4c74b05a093280bb4c5b81b347f | |
parent | a0ca545e790219015ef0e02069f6ba4258510b54 (diff) | |
download | git-suggestion-b4a7561ccc1ed7dab6cd2c786902dec8c4ad9be6.tar.bz2 |
Suggestion.apply_to(): Read from given reader, not original file
We can't read from the original file, because we want to be able to use
that as the writer in `apply()`.
Previously, this would result in the original file simply being
truncated, as we weren't reading from the correct file.
To fix this, we need to give `apply_to()` both a reader and a writer.
Now, in `apply()`, we can pass it the temporary file to read, and the
original to write to.
Used a `Path` as an input to `apply_to()` instead of a `Read`er because
I got an error that didn't really make sense to me when I did so:
error[E0275]: overflow evaluating the requirement `&tokio_reactor::poll_evented::PollEvented<_>: std::io::Read`
|
= help: consider adding a `#![recursion_limit="256"]` attribute to your crate (`git_suggested_patch`)
= note: required because of the requirements on the impl of `std::io::Read` for `&tokio_reactor::poll_evented::PollEvented<tokio_reactor::poll_evented::PollEvented<_>>`
= note: required because of the requirements on the impl of `std::io::Read` for `&tokio_reactor::poll_evented::PollEvented<tokio_reactor::poll_evented::PollEvented<tokio_reactor::poll_evented::PollEvented<_>>>`
A search for the error message turned up a few active issues:
- https://github.com/rust-lang/rust/issues/57854
- https://github.com/rust-lang/rust/issues/62755
- https://github.com/rust-lang/rust/issues/39959
The above issues indicate that this could come from having a variable
with an inferred type. However, for the moment the issue isn't resolved,
and the error message doesn't indicate where the problem is actually
coming from. As I was unable to track down the source of the problem, I
decided not to bother using a `Read` argument.
-rw-r--r-- | src/lib.rs | 16 |
1 files changed, 9 insertions, 7 deletions
@@ -9,6 +9,7 @@ pub use crate::url::SuggestionUrl; use std::fs; use std::fs::{File, OpenOptions}; use std::io::{BufRead, BufReader, Write}; +use std::path::Path; use git2::Repository; use github_rs::client::{Executor, Github}; @@ -132,14 +133,15 @@ impl Suggestion { .truncate(true) .open(repo_root.join(&self.path)).unwrap(); - self.apply_to(&mut original) + self.apply_to(new.path(), &mut original) } - fn apply_to<W: Write>(&self, writer: &mut W) ->Result<(), Error> { - let repo = Repository::open(".").unwrap(); - let repo_root = repo.workdir().unwrap(); - - let original = File::open(repo_root.join(&self.path)).unwrap(); + fn apply_to<P: AsRef<Path>, W: Write>( + &self, + from: P, + writer: &mut W, + ) -> Result<(), Error> { + let original = File::open(from).unwrap(); let reader = BufReader::new(original); for (i, line) in reader.lines().enumerate() { @@ -346,7 +348,7 @@ mod tests { "#; let mut actual = Cursor::new(Vec::new()); - suggestion.apply_to(&mut actual).unwrap(); + suggestion.apply_to(temp.path(), &mut actual).unwrap(); assert_eq!( std::str::from_utf8(&actual.into_inner()).unwrap(), |