utils_results/
lib.rs

1/*
2    .. + lib.rs + ..
3    Copyright 2021 Hwakyeom Kim(=just-do-halee)
4*/
5
6//! First, You should make your own an error set.
7//! # Example
8//! ```ignore
9//! err! {
10//!      BrokenHeader => "broken header."
11//!      AnotherHeader => "not matched header."
12//!      FileNotFound => "file not found."
13//!      EmptyArgument => "empty argument."
14//!      UnexpectedEof => "unexpected eof."
15//!      OutOfBounds => "index out of bounds."
16//!      NotMatched => "btw not matched."
17//! }
18//! ```
19//! And just errbang!
20//! ```ignore
21//! errbang!(err::BrokenHeader);
22//! ```
23//! # More Examples
24//! ```ignore
25//! fn foo() -> Result<bool> { // Our Master Result Type
26//!     let bar = 2;
27//!     match bar {
28//!         0 => Ok(true),
29//!         1 => Ok(false),
30//!         _ => errbang!(err::NotMatched, "{} is {}", "bar", bar),
31//!     }
32//! }
33//! fn main() -> Result<()> {
34//!     let _is_bar_zero = foo()?;
35//!     Ok(())
36//! }
37//! ```
38//! ```ignore
39//! errbang!("error.");
40//! errbang!(err::MyError1);
41//! errbang!(err::MyError2, "cannot find.");
42//! errbang!(err::MyError3, "{} is {}", "bar", 2);
43//! ```
44//!
45//! | Result
46//! ```text
47//! [src/main.rs 40:1] unexpected eof. bar is 2 <err::UnexpectedEof>
48//! ```
49//!
50//! unwrapping error input data. also can easily compare them.
51//! ```ignore
52//! fn foo() -> Result<()> {
53//!     // example
54//!     return errbang!(err::Bar, "this is input.");
55//! }
56//!
57//! assert_eq!(
58//!    errunwrap!(foo(), err::Bar), "this is input."
59//! );
60//! ```
61//!
62//! # ***Important***
63//!
64//! - 1. One result type(`anyhow`).
65//! - 2. All casted errors have their own chaining error' information(all the previous errors).
66//!
67//! if you follow below rules, you can easily debug all your projects.
68//!
69//! ### errbang -> errcast -> errcast -> ... ->  errcast -> errextract  
70//!   
71//! ---
72//!   
73//! ## Quick Overview
74//!
75//! ```ignore
76//! use utils_results::*;
77//!
78//! err! {
79//!     One => "this error is first one."
80//!     Two => "this error is second one."
81//!     Three => "this error is third one."
82//!     Well => "is this?"
83//! }
84//!
85//!
86//! fn aaa() -> Result<usize> {
87//!     return errbang!(err::One, "{}.error bang!", 1);
88//! }
89//!
90//! fn bbb() -> Result<usize> {
91//!     let n = errcast!(aaa(), err::Two, "{}.two <- one.", 2);
92//!     Ok(n)
93//! }
94//!
95//! fn ccc() -> Result<usize> {
96//!     Ok(errcast!(bbb(), err::Three, "{}.three <- two.", n))
97//! }
98//!
99//!
100//! fn main() -> Result<()> {
101//!     let c = errextract!(ccc(), err::Well => 127);
102//!     eprintln!("1/{} is cosmological constant.", c);
103//!     Ok(())
104//! }
105//! ```
106//!
107//! | Result
108//! ```text
109//! Error:
110//! [src/main.rs 11:12] this error is first one. 1.error bang! <err::One> aaa()
111//!                     ⎺↴
112//! [src/main.rs 14:13] this error is second one. 2.two <- one. <err::Two> bbb()
113//!                     ⎺↴
114//! [src/main.rs 18:8] this error is third one. 3.three <- two. <err::Three>
115//! ```
116//! If the matching error be changed,
117//! ```ignore
118//! // Well to Three
119//! let c = errextract!(ccc(), err::Three => 127);
120//! ```
121//! | Result
122//! ```text
123//! 1/127 is cosmological constant.
124//! ```
125//!
126//! ---
127//!
128//! # ***errcast***
129//! Any type of error can be converted into our Master Error.
130//!
131//!
132//! ```ignore
133//! // example
134//! // <Unwraped Ok> = errcast!(<Any Result>, <Master Err>, <Optional,..>);
135//! let num_read = errcast!(file.read(&mut buf), err::ReadErr, "this is {} data.", "meta");
136//! ```
137//! ---
138//! # Simply just do this!
139//!
140//! ```ignore
141//! let file = errcast!(File::open("test"), err::FileOpenError)
142//! ```
143//! ## or...
144//! ```ignore
145//! // Master `Result` can take any errors
146//! let file = File::open("test")?;
147//!
148//! // if cfg!(no_std),
149//! let file = io_to_err!(File::open("test"))?;
150//! ```
151//! But, *errcast* -> ***errextract*** combo is always good choice.
152//!
153//! ```ignore
154//! fn exe(path: &str) -> Result<usize> {
155//!     let file = errcast!(File::open("test"), err::FileOpenError);
156//!     // .....
157//!     // ...
158//!     Ok(num)
159//! }
160//!
161//! fn main() -> Result<()> {
162//!     /// non panic unwraping
163//!     /// and specific error can return
164//!     /// matching block
165//!     let num = errextract!(exe(path),
166//!         err::FileOpenError => 0);
167//!     /// other errors will go out -> Result<T>
168//!
169//!     Ok(())
170//! }
171//! ```
172//! ---
173//! # ***Master Result***
174//! * Please use our Master ***Result***\<T\>
175//! instead std::result::Result or io::Result etc..  
176//! * this is `anyhow` Result.<br>
177//! ---
178//! ###### ***utils-results/lib.rs*** Definition
179//! ```ignore
180//! /// Master Result
181//! pub type Result<T> = anyhow::Result<T>;
182//! ```
183
184//! ---
185//! ### just put this in your project.
186//! ```ignore
187//! pub use utils_results::*;
188//! ```
189
190//! ## You can also convert any type of `Result`
191//! ```ignore
192//! // to our Master Result
193//! resultcast!(handle.join().unwrap())?;
194//! ```
195
196#![cfg_attr(all(not(feature = "std"), not(test)), no_std)]
197
198extern crate anyhow;
199/// Master Result
200pub use anyhow::{Error, Result};
201
202#[macro_use]
203mod macros;
204
205extern crate alloc;
206
207#[doc(hidden)]
208pub mod private {
209    pub use alloc::{format, string::String};
210    pub use anyhow::{Error, Result};
211    #[cfg(not(feature = "std"))]
212    pub use core::fmt;
213    #[cfg(feature = "std")]
214    pub use std::fmt;
215}