2 stable releases

1.0.1 Mar 20, 2024

#1290 in Text processing

Download history 12/week @ 2024-07-14 2/week @ 2024-07-21 49/week @ 2024-07-28 15/week @ 2024-08-04 2/week @ 2024-08-11 22/week @ 2024-08-25 8/week @ 2024-09-01 1/week @ 2024-09-08 14/week @ 2024-09-15 49/week @ 2024-09-22 22/week @ 2024-09-29 3/week @ 2024-10-06 51/week @ 2024-10-13 47/week @ 2024-10-20 13/week @ 2024-10-27

116 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