snafu

Easily assign underlying errors into domain-specific errors while adding context

Github星跟蹤圖

SNAFU

Situation Normal: All Fouled Up

crates.io
Documentation
Build Status

SNAFU is a library to easily assign underlying errors into
domain-specific errors while adding context.

use snafu::{ResultExt, Snafu};
use std::{fs, io, path::PathBuf};

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

    #[snafu(display("Unable to write result to {}: {}", path.display(), source))]
    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(ReadConfiguration { path })?;
    let path = unpack_config(&configuration);
    fs::write(&path, b"My complex calculation").context(WriteResult { path })?;
    Ok(())
}

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

Please see the documentation and the user's guide for
a full description.

主要指標

概覽
名稱與所有者shepmaster/snafu
主編程語言Rust
編程語言Rust (語言數: 2)
平台
許可證Apache License 2.0
所有者活动
創建於2019-01-27 23:56:35
推送於2025-10-23 02:30:01
最后一次提交
發布數52
最新版本名稱0.8.9 (發布於 )
第一版名稱0.1.0 (發布於 )
用户参与
星數1.8k
關注者數16
派生數68
提交數772
已啟用問題?
問題數240
打開的問題數88
拉請求數234
打開的拉請求數7
關閉的拉請求數17
项目设置
已啟用Wiki?
已存檔?
是復刻?
已鎖定?
是鏡像?
是私有?