The easiest and most intuitive error handling solution. (no dependencies, about 150 lines pure codes)
| Docs |
toml
[dependencies]
utils_results = "4.0.0"
First, You should make your own an error set.
rust
err! {
BrokenHeader => "broken header."
AnotherHeader => "not matched header."
FileNotFound => "file not found."
EmptyArgument => "empty argument."
UnexpectedEof => "unexpected eof."
OutOfBounds => "index out of bounds."
NotMatched => "btw not matched."
}
And just errbang!
rust
errbang!(err::BrokenHeader)
```rust
fn foo() -> Result
fn main() -> Result<()> {
let isbar_zero = foo()?;
Ok(())
}
rust
errbang!(err::MyError1);
errbang!(err::MyError2, "cannot find.");
errbang!(err::MyError3, "{} is {}", "bar", 2);
| Result
[src/main.rs 40:1] unexpected eof. bar is 2
```rust use utils_results::*;
err! { One => "this error is first one." Two => "this error is second one." Three => "this error is third one." Well => "is this?" }
fn aaa() -> Result
fn bbb() -> Result
fn ccc() -> ResultSend
fn main() -> Result<()> { let c = errextract!(ccc(), err::Well => 127); eprintln!("1/{} is cosmological constant.", c); Ok(()) } ```
| Result
Error:
[src/main.rs 11:12] this error is first one. 1.error bang! <err::One> aaa()
⎺↴
[src/main.rs 14:13] this error is second one. 2.two <- one. <err::Two> bbb()
⎺↴
[src/main.rs 18:8] this error is third one. 3.three <- two. <err::Three>
If the matching error be changed,
rust
// Well to Three
let c = errextract!(ccc(), err::Three => 127);
| Result
1/127 is cosmological constant.
Any type of error can be converted into our Master Error. (non panic unwraping)
rust
// example
let num_read = errcast!(file.read(&mut buf), err::ReadErr, "this is {} data.", "meta");
rust
let file = errcast!(File::open("test"), err::FileOpenError)
rust
// Master `Result` can take any errors(dyn error)
let file = File::open("test")?;
But, errcast -> errextract combo is always good choice.
```rust
fn exe(path: &str) -> Result
fn main() -> Result<()> {
/// non panic unwraping
/// and specific error can return
/// matching block
let num = errextract!(exe(path),
err::FileOpenError => 0);
/// other errors will go out -> Result
Ok(())
}
Well, we can also handle io::Error more idiomatic way.
io::Error
```rust
io_err! { // io::ErrorKind => err::MyError UnexpectedEof => err::MyError1 Interrupted => err::MyError2 NotFound => err::MyError3 // ... }
Declare matching macro and just handle that.<br>
rust
iotoerr!(file.seek(SeekFrom::End(0)))?;
errtoio!(my_seek(0))?;
```rust
/// Master Result
pub type Result
```rust pub use utils_results::*;
Result
rust
// to floating Result
resultcast!(handle.join().unwrap())?;