2个稳定版本

1.0.1 2024年3月20日

#1003 in 文本处理

Download history 3/week @ 2024-04-12 4/week @ 2024-04-19 4/week @ 2024-04-26 1/week @ 2024-05-03 2/week @ 2024-05-17 11/week @ 2024-05-24 11/week @ 2024-05-31 8/week @ 2024-06-07 6/week @ 2024-06-14 7/week @ 2024-06-21 6/week @ 2024-06-28 13/week @ 2024-07-05 8/week @ 2024-07-12 6/week @ 2024-07-19 36/week @ 2024-07-26

64 每月下载量
用于 3 crates

MIT 许可证

22KB
353

utf8-command

docs.rs Crates.io

为Rust提供UTF-8解码的std::process::Output


lib.rs:

提供了Utf8Output类型,它是std::process::Output的UTF-8解码变体(由std::process::Command::output产生)。

通过TryIntoTryFrom特性从Output构造Utf8Output

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(""),
    },
);

错误信息将包括关于失败解码的流的详细信息,以及输出(无效的UTF-8字节用U+FFFD替换字符替换)

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 �(\""
);

无运行时依赖