Patchwork D8870: rhg: extract function handle_stdout_error

login
register
mail settings
Submitter phabricator
Date Aug. 3, 2020, 2:27 p.m.
Message ID <differential-rev-PHID-DREV-w5o5zz3ujrui6i3ivuxa-req@mercurial-scm.org>
Download mbox | patch
Permalink /patch/46967/
State Superseded
Headers show

Comments

phabricator - Aug. 3, 2020, 2:27 p.m.
acezar created this revision.
Herald added a reviewer: hg-reviewers.
Herald added a subscriber: mercurial-patches.

REVISION SUMMARY
  Avoid repeating the logic of handling stdout write errors.

REPOSITORY
  rHG Mercurial

BRANCH
  default

REVISION DETAIL
  https://phab.mercurial-scm.org/D8870

AFFECTED FILES
  rust/rhg/src/ui.rs

CHANGE DETAILS




To: acezar, #hg-reviewers
Cc: mercurial-patches, mercurial-devel

Patch

diff --git a/rust/rhg/src/ui.rs b/rust/rhg/src/ui.rs
--- a/rust/rhg/src/ui.rs
+++ b/rust/rhg/src/ui.rs
@@ -34,38 +34,23 @@ 
     pub fn write_stdout(&self, bytes: &[u8]) -> Result<(), UiError> {
         let mut stdout = self.stdout.lock();
 
-        self.write_stream(&mut stdout, bytes)
-            .or_else(|e| self.handle_stdout_error(e))?;
-
-        stdout.flush().or_else(|e| self.handle_stdout_error(e))
-    }
+        stdout
+            .write_all(bytes)
+            .or_else(|e| handle_stdout_error(e))?;
 
-    /// Sometimes writing to stdout is not possible, try writing to stderr to
-    /// signal that failure, otherwise just bail.
-    fn handle_stdout_error(&self, error: io::Error) -> Result<(), UiError> {
-        self.write_stderr(
-            &[b"abort: ", error.to_string().as_bytes(), b"\n"].concat(),
-        )?;
-        Err(UiError::StdoutError(error))
+        stdout.flush().or_else(|e| handle_stdout_error(e))
     }
 
     /// Write bytes to stderr
     pub fn write_stderr(&self, bytes: &[u8]) -> Result<(), UiError> {
         let mut stderr = self.stderr.lock();
 
-        self.write_stream(&mut stderr, bytes)
-            .or_else(|e| Err(UiError::StderrError(e)))?;
+        stderr
+            .write_all(bytes)
+            .or_else(|e| handle_stderr_error(e))?;
 
         stderr.flush().or_else(|e| Err(UiError::StderrError(e)))
     }
-
-    fn write_stream(
-        &self,
-        stream: &mut impl Write,
-        bytes: &[u8],
-    ) -> Result<(), io::Error> {
-        stream.write_all(bytes)
-    }
 }
 
 /// A buffered stdout writer for faster batch printing operations.
@@ -81,29 +66,31 @@ 
 
     /// Write bytes to stdout buffer
     pub fn write_all(&mut self, bytes: &[u8]) -> Result<(), UiError> {
-        self.buf.write_all(bytes).or_else(|e| self.io_err(e))
+        self.buf
+            .write_all(bytes)
+            .or_else(|e| handle_stdout_error(e))
     }
 
     /// Flush bytes to stdout
     pub fn flush(&mut self) -> Result<(), UiError> {
-        self.buf.flush().or_else(|e| self.io_err(e))
-    }
-
-    fn io_err(&self, error: io::Error) -> Result<(), UiError> {
-        if let ErrorKind::BrokenPipe = error.kind() {
-            // This makes `| head` work for example
-            return Ok(());
-        }
-        let mut stderr = io::stderr();
-
-        stderr
-            .write_all(
-                &[b"abort: ", error.to_string().as_bytes(), b"\n"].concat(),
-            )
-            .map_err(|e| UiError::StderrError(e))?;
-
-        stderr.flush().map_err(|e| UiError::StderrError(e))?;
-
-        Err(UiError::StdoutError(error))
+        self.buf.flush().or_else(|e| handle_stdout_error(e))
     }
 }
+
+/// Sometimes writing to stdout is not possible, try writing to stderr to
+/// signal that failure, otherwise just bail.
+fn handle_stdout_error(error: io::Error) -> Result<(), UiError> {
+    if let ErrorKind::BrokenPipe = error.kind() {
+        // This makes `| head` work for example
+        return Ok(());
+    }
+    let mut stderr = io::stderr();
+
+    stderr
+        .write_all(&[b"abort: ", error.to_string().as_bytes(), b"\n"].concat())
+        .map_err(|e| UiError::StderrError(e))?;
+
+    stderr.flush().map_err(|e| UiError::StderrError(e))?;
+
+    Err(UiError::StdoutError(error))
+}