랭킹으로 돌아가기
Easily assign underlying errors into domain-specific errors while adding context
rusterror-handling
주요 지표
스타 성장
스타
1.9k
포크
70
주간 성장
—
이슈
81
5001k1.5k
2019년 2월2020년 4월2021년 7월2022년 10월2024년 1월2025년 4월2026년 7월
아티팩트crates.io
cargo add snafuREADME
SNAFU
Situation Normal: All Fouled Up
SNAFU is a library to easily assign underlying errors into domain-specific errors while adding context.
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"
}
Please see the documentation and the user's guide for a full description.
관련 저장소