1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-28 11:37:44 +00:00

Move strip_errno to libcore

This commit is contained in:
Jan Verbeek 2021-08-26 22:42:07 +02:00 committed by Michael Debertol
parent d285472210
commit 1c05183083
2 changed files with 17 additions and 23 deletions

View file

@ -45,7 +45,7 @@ use std::path::Path;
use std::path::PathBuf; use std::path::PathBuf;
use std::str::Utf8Error; use std::str::Utf8Error;
use unicode_width::UnicodeWidthStr; 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::parse_size::{parse_size, ParseSizeError};
use uucore::version_cmp::version_cmp; use uucore::version_cmp::version_cmp;
use uucore::InvalidEncodingHandling; use uucore::InvalidEncodingHandling;
@ -197,27 +197,17 @@ impl Display for SortError {
Ok(()) Ok(())
} }
} }
SortError::OpenFailed { path, error } => write!( SortError::OpenFailed { path, error } => {
f, write!(f, "open failed: {}: {}", path, strip_errno(error))
"open failed: {}: {}", }
path,
strip_errno(&error.to_string())
),
SortError::ParseKeyError { key, msg } => { SortError::ParseKeyError { key, msg } => {
write!(f, "failed to parse key `{}`: {}", key, msg) write!(f, "failed to parse key `{}`: {}", key, msg)
} }
SortError::ReadFailed { path, error } => write!( SortError::ReadFailed { path, error } => {
f, write!(f, "cannot read: {}: {}", path, strip_errno(error))
"cannot read: {}: {}", }
path,
strip_errno(&error.to_string())
),
SortError::OpenTmpFileFailed { error } => { SortError::OpenTmpFileFailed { error } => {
write!( write!(f, "failed to open temporary file: {}", strip_errno(error))
f,
"failed to open temporary file: {}",
strip_errno(&error.to_string())
)
} }
SortError::CompressProgExecutionFailed { code } => { SortError::CompressProgExecutionFailed { code } => {
write!(f, "couldn't execute compress program: errno {}", code) write!(f, "couldn't execute compress program: errno {}", code)
@ -1814,11 +1804,6 @@ fn print_sorted<'a, T: Iterator<Item = &'a Line<'a>>>(
} }
} }
/// 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<OsStr>) -> UResult<Box<dyn Read + Send>> { fn open(path: impl AsRef<OsStr>) -> UResult<Box<dyn Read + Send>> {
let path = path.as_ref(); let path = path.as_ref();
if path == "-" { if path == "-" {

View file

@ -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 /// Enables the conversion from [`std::io::Error`] to [`UError`] and from [`std::io::Result`] to
/// [`UResult`]. /// [`UResult`].
pub trait FromIo<T> { pub trait FromIo<T> {