2 releases
0.1.1 | Mar 23, 2020 |
---|---|
0.1.0 | Mar 22, 2020 |
#2138 in Data structures
12KB
181 lines
String error type.
This crate contains a type StringError
, to be used to report top-level errors to a user,
such as in a binary when executing main
.
Examples
use std::{io::Read, error::Error};
use string_err::{StringError, ResultExt};
pub fn main() -> Result<(), StringError<Box<dyn Error>>> {
let mut file = std::fs::File::open("README.md")
.map_err_msg("Could not open the file")?;
let mut contents = String::new();
file.read_to_string(&mut contents)
.map_err_msg("Could not read the file")?;
assert!( contents.starts_with("String error type.") );
// ... Do something with `num`
Ok(())
}
This example attempts to load the README.md
file, read it and make sure it starts
with the string "String error type."
(which this readme does).
See the documentation for more details on how to use
the StringError
type
lib.rs
:
String error type
This crate provides a string error type, StringError
that may have an underlying error and backtrace too.
This is useful for binaries, that need only to display the top-level
error to the user and not describe it in code, using an enum
.
It may also be used as the return type in main, provided the underlying
error type is specified as Box<dyn Error + 'static>
This crate currently uses various nightly
features to implement the string
error type. Even when the used features are stabilized, this crate will likely
keep using other nightly features, as the crate is intended for usage with only
binaries. The binaries themselves can use nightly
easily, without affecting
the libraries built along-side them.