Crates.io | utils-results |
lib.rs | utils-results |
version | 5.3.0 |
source | src |
created_at | 2021-07-27 19:19:21.225518 |
updated_at | 2021-10-18 00:07:31.90093 |
description | The easiest and most intuitive error handling solution. |
homepage | https://github.com/just-do-halee/utils-results |
repository | https://github.com/just-do-halee/utils-results |
max_upload_size | |
id | 428069 |
size | 39,730 |
The easiest and most intuitive error handling solution.
| Docs | Latest Note |
[dependencies]
utils_results = "5.3.0"
Disable default feature(allocator is needed).
[dependencies]
utils_results = { version = "5.3.0", default-features = false }
First, You should make your own an error set.
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!
errbang!(err::BrokenHeader)
fn foo() -> Result<bool> { // Our Master Result Type
let bar = 2;
match bar {
0 => Ok(true),
1 => Ok(false),
_ => errbang!(err::NotMatched, "{} is {}", "bar", bar),
}
}
fn main() -> Result<()> {
let _is_bar_zero = foo()?;
Ok(())
}
errbang!("error.");
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 <err::UnexpectedEof>
unwrapping error input data. also can easily compare them.
fn foo() -> Result<()> {
// example
return errbang!(err::Bar, "this is input.");
}
assert_eq!(
errunwrap!(foo(), err::Bar), "this is input."
);
anyhow
).if you follow below rules, you can easily debug all your projects.
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<usize> {
return errbang!(err::One, "{}.error bang!", 1);
}
fn bbb() -> Result<usize> {
let n = errcast!(aaa(), err::Two, "{}.two <- one.", 2);
Ok(n)
}
fn ccc() -> Result<usize> {
Ok(errcast!(bbb(), err::Three, "{}.three <- two.", 3))
}
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 has changed,
// 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)
// example
let num_read = errcast!(file.read(&mut buf), err::ReadErr, "this is {} data.", "meta");
let file = errcast!(File::open("test"), err::FileOpenError)
// Master `Result` can take any errors
let file = File::open("test")?;
// if cfg!(no_std),
let file = io_to_err!(File::open("test"))?;
But, errcast -> errextract combo is always good choice.
fn exe(path: &str) -> Result<usize> {
let file = errcast!(File::open("test"), err::FileOpenError);
// .....
// ...
Ok(num)
}
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<T>
Ok(())
}
Well, we can also handle io::Error more idiomatic way. (feature = "std")
io::Error
io_err! {
// io::ErrorKind => err::MyError
UnexpectedEof => err::MyError1
Interrupted => err::MyError2
NotFound => err::MyError3
// ...
}
Declare matching macro and just handle that.
io_to_err!(file.seek(SeekFrom::End(0)))?;
err_to_io!(my_seek(0))?;
anyhow
Result./// Master Result
pub type Result<T> = anyhow::Result<T>;
pub use utils_results::*;
Result
// to our Master Result
resultcast!(handle.join().unwrap())?;