tiny-error 1.0.0

A small crate for simple error handling
Documentation
  • Coverage
  • 100%
    3 out of 3 items documented1 out of 3 items with examples
  • Size
  • Source code size: 15.62 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 1.3 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 9s Average build duration of successful builds.
  • all releases: 9s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • Sapiet1/tiny-error
    0 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • Sapiet1

tiny-error

A tiny crate for error handling. It is able to convert items of the Error trait into their messages, allowing for easy propagation.

Examples

use tiny_error::ErrorMessage;

use std::{
    env,
    fs,
    path::PathBuf,
};

fn main() -> Result<(), ErrorMessage> {
    // Text when failed:
    // Error: Invalid input
    // Correct Usage: `[crate_name] example/file/path.txt`
    let path = get_path()?;
    // Text when failed: 
    // Error: No such file or directory (os error 2)
    let file = fs::read_to_string(path)?;

    Ok(())
}

// Gets the first argument passed. If none or more were, returns an
// `ErrorMessage`.
fn get_path() -> Result<PathBuf, ErrorMessage> {
    let mut args = env::args().skip(1);
    let arg = args.next().filter(|_| args.next().is_none());    

    arg
        .map(|input| input.into())
        .ok_or_else(|| ErrorMessage::new(
            "Invalid input\n\
             Correct Usage: `[crate_name] example/file/path.txt`"
        ))
}