2 stable releases

1.0.1 Mar 20, 2024

#1441 in Text processing

Download history 28/week @ 2024-08-26 3/week @ 2024-09-02 15/week @ 2024-09-16 54/week @ 2024-09-23 17/week @ 2024-09-30 2/week @ 2024-10-07 61/week @ 2024-10-14 44/week @ 2024-10-21 6/week @ 2024-10-28 2/week @ 2024-11-04 27/week @ 2024-11-18 13/week @ 2024-11-25 21/week @ 2024-12-02 46/week @ 2024-12-09

107 downloads per month
Used in 4 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