46 个版本

0.8.4 2024年7月8日
0.8.2 2024年3月11日
0.8.0 2023年12月30日
0.7.5 2023年7月9日
0.2.0 2019年3月26日

#42 in Rust 模式

Download history 234016/week @ 2024-05-04 260656/week @ 2024-05-11 279852/week @ 2024-05-18 275453/week @ 2024-05-25 364994/week @ 2024-06-01 364362/week @ 2024-06-08 336027/week @ 2024-06-15 359773/week @ 2024-06-22 345997/week @ 2024-06-29 390824/week @ 2024-07-06 381607/week @ 2024-07-13 393707/week @ 2024-07-20 404418/week @ 2024-07-27 378883/week @ 2024-08-03 434194/week @ 2024-08-10 364454/week @ 2024-08-17

1,647,026 每月下载量
用于 1,186 个crate(466 个直接使用)

MIT/Apache

115KB
2K SLoC

SNAFU

Situation Normal: All Fouled Up

crates.io Documentation Build Status

SNAFU 是一个易于将底层错误分配到特定领域错误的库,同时添加上下文。

use snafu::prelude::*;
use std::{fs, io, path::PathBuf};

#[derive(Debug, Snafu)]
enum Error {
    #[snafu(display("Unable to read configuration from {}", path.display()))]
    ReadConfiguration { source: io::Error, path: PathBuf },

    #[snafu(display("Unable to write result to {}", path.display()))]
    WriteResult { source: io::Error, path: PathBuf },
}

type Result<T, E = Error> = std::result::Result<T, E>;

fn process_data() -> Result<()> {
    let path = "config.toml";
    let configuration = fs::read_to_string(path).context(ReadConfigurationSnafu { path })?;
    let path = unpack_config(&configuration);
    fs::write(&path, b"My complex calculation").context(WriteResultSnafu { path })?;
    Ok(())
}

fn unpack_config(data: &str) -> &str {
    "/some/path/that/does/not/exist"
}

请参阅文档用户指南以获取完整描述。

依赖项

~0.3–1.4MB
~31K SLoC