diff options
author | Teddy Wing | 2020-07-29 01:04:23 +0200 |
---|---|---|
committer | Teddy Wing | 2020-07-29 01:04:23 +0200 |
commit | eeaa7d3aaec7a9344d06e4e616da595658280dac (patch) | |
tree | fb172ecd7bbfcb7e3eb8f24257d147f3761ec457 /src/suggestion.rs | |
parent | 9430c40a245a5bf9bfb138679a51e48dc629e710 (diff) | |
download | git-suggestion-eeaa7d3aaec7a9344d06e4e616da595658280dac.tar.bz2 |
Suggestion.suggestion_with_line_ending: Return `Result`
Remove the `unwrap` in this method and return a `Result`.
Diffstat (limited to 'src/suggestion.rs')
-rw-r--r-- | src/suggestion.rs | 16 |
1 files changed, 11 insertions, 5 deletions
diff --git a/src/suggestion.rs b/src/suggestion.rs index baf41d8..8482d23 100644 --- a/src/suggestion.rs +++ b/src/suggestion.rs @@ -23,6 +23,9 @@ pub enum Error { #[error("{0} is not valid UTF-8")] InvalidUtf8(String), + + #[error("Regex error: {0}")] + Regex(#[from] regex::Error), } #[derive(Debug, PartialEq)] @@ -95,17 +98,20 @@ impl Suggestion { ) } - fn suggestion_with_line_ending(&self, line_ending: &LineEnding) -> String { - let re = Regex::new(r"(?s).*(?-s)```\s*suggestion.*\n").unwrap(); + fn suggestion_with_line_ending( + &self, + line_ending: &LineEnding, + ) -> Result<String, Error> { + let re = Regex::new(r"(?s).*(?-s)```\s*suggestion.*\n")?; let s = re.replace(&self.comment, ""); let s = s.replace("```", ""); // Suggestion blocks use CRLF by default. if *line_ending == LineEnding::Lf { - return s.replace('\r', ""); + return Ok(s.replace('\r', "")); } - s + Ok(s) } pub fn apply(&self) -> Result<(), Error> { @@ -145,7 +151,7 @@ impl Suggestion { write!( writer, "{}", - self.suggestion_with_line_ending(&line_ending), + self.suggestion_with_line_ending(&line_ending).unwrap(), ).unwrap(); } else if self.original_start_line.is_none() || line_number < self.original_start_line.unwrap() |