2 stable releases

1.0.1 Mar 20, 2024

#886 in Text processing

Download history 298/week @ 2024-03-20 58/week @ 2024-03-27 118/week @ 2024-04-03 5/week @ 2024-04-10

479 downloads per month
Used in 3 crates

MIT license

22KB
353 lines

utf8-command

docs.rs Crates.io

UTF-8 decoded std::process::Output for Rust.


lib.rs:

Provides the Utf8Output type, a UTF-8-decoded variant of std::process::Output (as produced by std::process::Command::output).

Construct Utf8Output from Output via the TryInto or TryFrom traits:

let output: Utf8Output = Command::new("echo")
    .arg("puppy")
    .output()
    .unwrap()
    .try_into()
    .unwrap();
assert_eq!(
    output,
    Utf8Output {
        status: ExitStatus::default(),
        stdout: String::from("puppy\n"),
        stderr: String::from(""),
    },
);

Error messages will include information about the stream that failed to decode, as well as the output (with invalid UTF-8 bytes replaced with U+FFFD REPLACEMENT CHARACTER):

let invalid = Output {
    status: ExitStatus::default(),
    stdout: Vec::from(b"puppy doggy \xc3\x28"), // Invalid 2-byte sequence.
    stderr: Vec::from(b""),
};

let err: Result<Utf8Output, Error> = invalid.try_into();
assert_eq!(
    err.unwrap_err().to_string(),
    "Stdout contained invalid utf-8 sequence of 1 bytes from index 12: \"puppy doggy �(\""
);

No runtime deps