snafu

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

  • Owner: shepmaster/snafu
  • Platform:
  • License:: Apache License 2.0
  • Category::
  • Topic:
  • Like:
    0
      Compare:

Github stars Tracking Chart

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.

Main metrics

Overview
Name With Ownershepmaster/snafu
Primary LanguageRust
Program languageRust (Language Count: 2)
Platform
License:Apache License 2.0
所有者活动
Created At2019-01-27 15:56:35
Pushed At2024-11-19 17:21:04
Last Commit At
Release Count48
Last Release Name0.8.5 (Posted on )
First Release Name0.1.0 (Posted on )
用户参与
Stargazers Count1.6k
Watchers Count17
Fork Count62
Commits Count724
Has Issues Enabled
Issues Count223
Issue Open Count79
Pull Requests Count219
Pull Requests Open Count6
Pull Requests Close Count15
项目设置
Has Wiki Enabled
Is Archived
Is Fork
Is Locked
Is Mirror
Is Private