diff --git a/src/uu/sort/src/sort.rs b/src/uu/sort/src/sort.rs index 9aae23bb8..abba1e63b 100644 --- a/src/uu/sort/src/sort.rs +++ b/src/uu/sort/src/sort.rs @@ -45,7 +45,7 @@ use std::path::Path; use std::path::PathBuf; use std::str::Utf8Error; use unicode_width::UnicodeWidthStr; -use uucore::error::{set_exit_code, UError, UResult, USimpleError, UUsageError}; +use uucore::error::{set_exit_code, strip_errno, UError, UResult, USimpleError, UUsageError}; use uucore::parse_size::{parse_size, ParseSizeError}; use uucore::version_cmp::version_cmp; use uucore::InvalidEncodingHandling; @@ -197,27 +197,17 @@ impl Display for SortError { Ok(()) } } - SortError::OpenFailed { path, error } => write!( - f, - "open failed: {}: {}", - path, - strip_errno(&error.to_string()) - ), + SortError::OpenFailed { path, error } => { + write!(f, "open failed: {}: {}", path, strip_errno(error)) + } SortError::ParseKeyError { key, msg } => { write!(f, "failed to parse key `{}`: {}", key, msg) } - SortError::ReadFailed { path, error } => write!( - f, - "cannot read: {}: {}", - path, - strip_errno(&error.to_string()) - ), + SortError::ReadFailed { path, error } => { + write!(f, "cannot read: {}: {}", path, strip_errno(error)) + } SortError::OpenTmpFileFailed { error } => { - write!( - f, - "failed to open temporary file: {}", - strip_errno(&error.to_string()) - ) + write!(f, "failed to open temporary file: {}", strip_errno(error)) } SortError::CompressProgExecutionFailed { code } => { write!(f, "couldn't execute compress program: errno {}", code) @@ -1814,11 +1804,6 @@ fn print_sorted<'a, T: Iterator>>( } } -/// Strips the trailing " (os error XX)" from io error strings. -fn strip_errno(err: &str) -> &str { - &err[..err.find(" (os error ").unwrap_or(err.len())] -} - fn open(path: impl AsRef) -> UResult> { let path = path.as_ref(); if path == "-" { diff --git a/src/uucore/src/lib/mods/error.rs b/src/uucore/src/lib/mods/error.rs index c2b3069c8..6af934d3e 100644 --- a/src/uucore/src/lib/mods/error.rs +++ b/src/uucore/src/lib/mods/error.rs @@ -409,6 +409,15 @@ impl Display for UIoError { } } +/// Strip the trailing " (os error XX)" from io error strings. +pub fn strip_errno(err: &std::io::Error) -> String { + let mut msg = err.to_string(); + if let Some(pos) = msg.find(" (os error ") { + msg.drain(pos..); + } + msg +} + /// Enables the conversion from [`std::io::Error`] to [`UError`] and from [`std::io::Result`] to /// [`UResult`]. pub trait FromIo {