Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/uucore/src/lib/mods/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,24 @@ impl Display for UIoError {
}

/// Strip the trailing " (os error XX)" from io error strings.
///
/// Rust renders OS errors as `"No such file or directory (os error 2)"`, while GNU
/// coreutils only prints the `strerror` part. Use this when formatting a message
/// that has to match GNU's output.
///
/// # Examples
///
/// ```
/// use std::io::{Error, ErrorKind};
/// use uucore::error::strip_errno;
///
/// let err = Error::from_raw_os_error(2);
/// assert_eq!(strip_errno(&err), "No such file or directory");
///
/// // Errors without an errno are returned unchanged.
/// let err = Error::new(ErrorKind::Other, "custom failure");
/// assert_eq!(strip_errno(&err), "custom failure");
/// ```
pub fn strip_errno(err: &std::io::Error) -> String {
let mut msg = err.to_string();
if let Some(pos) = msg.find(" (os error ") {
Expand Down
Loading