snafu

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

  • 所有者: shepmaster/snafu
  • 平台:
  • 許可證: Apache License 2.0
  • 分類:
  • 主題:
  • 喜歡:
    0
      比較:

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 15:56:35
推送於2024-11-19 17:21:04
最后一次提交
發布數48
最新版本名稱0.8.5 (發布於 )
第一版名稱0.1.0 (發布於 )
用户参与
星數1.6k
關注者數17
派生數62
提交數724
已啟用問題?
問題數223
打開的問題數79
拉請求數219
打開的拉請求數6
關閉的拉請求數15
项目设置
已啟用Wiki?
已存檔?
是復刻?
已鎖定?
是鏡像?
是私有?