Skip to main content

Crate idc

Crate idc 

Source
Expand description

§idc: A simple crate for error propagation

Idc is a simple crate for propagating errors that implement std’s Error trait.
Idc also supports no_std with the same functionality, but you have to provide a global allocator and disable default features.

§examples:

  1. propagating multiple different errors:
use std::fs;
use idc::*;
use serde_json::Value;

fn main() -> Result<()> {
    let foo = fs::read_to_string("foo.json").context("failed to read foo.", "maybe it doesn't exist?".into())?;
    let json: Value = serde_json::from_str(&foo).context("failed to turn foo into json.", "make sure foo.json is valid json.".into())?;
    println!("{}", json["important item"]);
    Ok(())
}
  1. returning an one-time error:
use std::env;
use idc::*;

fn main() -> Result<()> {
    let args: Vec<String> = env::args().collect();
    if args.len() < 2 {
        bail!("no argument provided!");
    }
    //...
    Ok(())
}

Macros§

bail
Macro for one time errors.

Structs§

Error
An error type for errors that implement std’s Error trait.

Traits§

Context
Trait for adding context and an optional hint to errors.

Type Aliases§

Result
Shoter version of Result<T, idc::Error>.