Skip to content

A Rust library that provides a simple error

License

Unknown and 2 other licenses found

Licenses found

Unknown
LICENSE.md
Apache-2.0
LICENSE-APACHE
MIT
LICENSE-MIT
Notifications You must be signed in to change notification settings

dz-at-tc/simple-error

 
 

simple-error

crates.io Documentation Build Status Coverage Status

simple-error is a Rust library that provides a simple Error type backed by a String. It is best used when all you care about the error is an error string.

Documentation

Usage

To use simple-error, first add this to your Cargo.toml:

[dependencies]
simple-error = "0.2"

Then add this to your crate root:

#[macro_use]
extern crate simple_error;

use simple_error::SimpleError;

Now you can use simple-error in different ways:

You can use it simply as a string error type:

fn do_foo() -> Result<(), SimpleError> {
    SimpleError::new("cannot do foo")
}

You can use it to replace all error types if you only care about a string description:

fn do_bar() -> Result<(), SimpleError> {
    SimpleError::from(std::io::Error::new(std::io::ErrorKind::Other, "oh no"))
}

Or you can chain all the errors, and get a complete error description at the top level:

fn find_tv_remote() -> Result<(), SimpleError> {
    try_with!(std::fs::File::open("remotefile"), "failed to open remote file");
    Ok(())
}

fn turn_on_tv() -> Result<(), std::io::Error> {
    Ok(())
}

fn watch_tv() -> Result<(), SimpleError> {
    try_with!(find_tv_remote(), "tv remote not found");
    try_with!(turn_on_tv(), "cannot turn on tv");
    Ok(())
}

fn study() -> Result<(), SimpleError> {
    Ok(())
}

fn run() -> Result<(), SimpleError> {
    try_with!(study(), "cannot study");
    try_with!(watch_tv(), "cannot watch tv");
    Ok(())
}

fn main() {
    if let Err(e) = run() {
        println!("{}", e);
    }
}
// This prints out "cannot watch tv, tv remote not found, failed to open remote file, Text file busy" if the error is text file busy.

About

A Rust library that provides a simple error

Resources

License

Unknown and 2 other licenses found

Licenses found

Unknown
LICENSE.md
Apache-2.0
LICENSE-APACHE
MIT
LICENSE-MIT

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Rust 100.0%