1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
//! There are many errors we don't expect to occur. But what if we're wrong? We
//! don't want our programs to panic because of that. We also don't want to spend
//! so much time handling unexpected errors. That's what this crate is for. You
//! keep your unexpected errors, and don't worry about them until later.
//!
//! * This crate works in `no-std`, although most features (besides [`Exun`]) require
//! `alloc`.
//!
//! * [`Exun`] is an error type. It'll hold on to your [`Unexpected`] error if you have
//! one, so you can figure out what to do with it later. If the error is
//! [`Expected`], then it'll hold onto that too.
//!
//! * [`RawUnexpected`] bottles up all of your unexpected errors. There's also
//! [`UnexpectedError`], which implements [`Error`].
//!
//! * [`Expect`] is a type alias for [`Exun<E, RawUnexpected>`].
//!
//! * Clearly mark errors that you don't expect to occur by calling
//! `Result::unexpect`. If the error type doesn't implement `Error`, you can still
//! use `Result::unexpect_msg`, as long as it implements
//! `Debug + Display + Send + Sync + 'static`.
//!
//! ## Usage
//!
//! The only pre-requisite is Rust 1.41.1.
//!
//! For standard features:
//!
//! ```toml
//! [dependencies]
//! # ...
//! exun = "0.1"
//! ```
//!
//! The following features are enabled by default:
//!
//! * `std`: This automatically enables `alloc`. It's used for the standard
//! library's [`Error`] type. Using this type allows more errors to be converted
//! into [`Exun`] and [`RawUnexpected`] errors automatically, and it's needed for
//! `Result::unexpect`.
//!
//! * `alloc`: This is needed for [`Expect`], [`RawUnexpected`] and
//! [`UnexpectedError`], as well as `Result::unexpected_msg`.
//!
//! To disable these features:
//!
//! ```toml
//! [dependencies]
//! # ...
//! exun = { version = "0.1", default-features = false }
//! ```
//!
//! If you'd like to use `alloc` but not `std`:
//!
//! ```toml
//! [dependencies]
//! # ...
//! exun = { version = "0.1", default-features = false, features = ["alloc"] }
//! ```
//!
//! ## Examples
//!
//! ```
//! use exun::*;
//!
//! fn foo(num: &str) -> Result<i32, RawUnexpected> {
//! // we use `unexpect` to indicate that we don't expect this error to occur
//! let num = num.parse::<i32>().unexpect()?;
//! Ok(num)
//! }
//! ```
//!
//! ```
//! use std::error::Error;
//! use std::fmt::{self, Display};
//!
//! use exun::*;
//!
//! #[derive(Debug)]
//! struct NoNumberError;
//!
//! impl Display for NoNumberError {
//! fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
//! write!(f, "no number provided")
//! }
//! }
//!
//! impl Error for NoNumberError {}
//!
//! fn foo(num: Option<&str>) -> Result<i32, Expect<NoNumberError>> {
//! let num = num.ok_or(NoNumberError)?; // we expect that this may return an error
//! let num = num.parse::<i32>().unexpect()?; // but we think the number is otherwise parsable
//! Ok(num)
//! }
//! ```
//!
//! ```
//! use std::error::Error;
//! use std::fmt::{self, Display};
//! use std::num::ParseIntError;
//!
//! use exun::*;
//!
//! #[derive(Debug)]
//! struct NoNumberError;
//!
//! impl Display for NoNumberError {
//! fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
//! write!(f, "no number provided")
//! }
//! }
//!
//! impl Error for NoNumberError {}
//!
//! fn foo(num: Option<&str>) -> Result<i32, Exun<&str, ParseIntError>> {
//! // we expect it possible to not get a number, so we handle it as such
//! let num = match num {
//! Some(num) => num,
//! None => return Err(Expected("no number provided")),
//! };
//!
//! // however, we expect that the number is otherwise parsable
//! match num.parse() {
//! Ok(int) => Ok(int),
//! Err(e) => Err(Unexpected(e))
//! }
//! }
//! ```
//!
//! [`Error`]: `std::error::Error
//!
extern crate alloc;
pub use crate;
pub use ResultErrorExt;
pub use ResultMsgExt;
pub use ;
pub type Expect<E> = ;