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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
//

//! resiter
//!
//! This crate helps iterating over `Iterator<Item = Result<O, E>>`.
//! All these things are trivial to build yourself, but why invest the effort if you can use a
//! crate for this?
//!
//! # Contributions welcome
//!
//! If you have _anything_ that might fit the scope of this crate, don't hesitate opening a
//! pull-request for it! This is considered a toolkit and convenience-crate, so everything which
//! might fit its scope should be merged!
//!
//! # Dependencies and Feature-gates
//!
//! If a feature of this crate uses external dependencies, it should be hidden behind a feature
//! gate. The crate itself should be usable without any dependencies besides `std`!
//!
//! # Features
//!
//! Features included in this crate:
//!
//! * Unwrap `Result<O, E>`s inside an Iterator
//! * Select only `Err(_)`s from the Iterator
//! * Select only `Ok(_)`s from the Iterator
//! * Do something in the `Err(_)` case, but don't change the error-object
//! * Do something in the `Ok(_)` case, but don't change the ok-object
//!
//! # Usecase
//!
//! * Consuming iterators until an error occurs
//!
//! ```
//! use std::str::FromStr;
//! use resiter::errors::*;
//!
//! let _ : Option<::std::num::ParseIntError> = ["1", "2", "foo", "4", "5"]
//!     .into_iter()
//!     .map(|e| usize::from_str(e))
//!     .errors()
//!     .next(); // "4" and "5" will never be processed by the iterator
//! ```
//!
//! * Consuming iterators and collect all errors
//!
//! ```
//! use std::str::FromStr;
//! use resiter::errors::*;
//!
//! let len = ["1", "2", "foo", "4", "5"]
//!     .into_iter()
//!     .map(|e| usize::from_str(e))
//!     .errors()
//!     .collect::<Vec<::std::num::ParseIntError>>()
//!     .len();
//! assert_eq!(len, 1);
//! ```
//!
//! * Consuming iterators and collect all oks
//!
//! ```
//! use std::str::FromStr;
//! use resiter::oks::*;
//!
//! let len = ["1", "2", "foo", "4", "5"]
//!     .into_iter()
//!     .map(|e| usize::from_str(e))
//!     .oks() // Could also be done with .filter_map(Result::ok)
//!     .collect::<Vec<_>>()
//!     .len();
//! assert_eq!(len, 4);
//! ```
//!
//! * Printing errors / oks
//!
//! ```
//! use std::str::FromStr;
//! use resiter::oks::*;
//! use resiter::onerr::*;
//! use resiter::onok::*;
//!
//! let len = ["1", "2", "foo", "4", "5"]
//!     .into_iter()
//!     .map(|e| usize::from_str(e))
//!     .on_err(|e| println!("Error happened: {:?}", e)) // ::std::process::exit(1) possible
//!     .on_ok(|o| println!("Parsed : '{}'", o))
//!     .oks()
//!     .collect::<Vec<_>>()
//!     .len();
//! assert_eq!(len, 4);
//! ```
//!
//! * Transforming oks
//!
//! ```
//! use std::str::FromStr;
//! use resiter::map::*;
//!
//! let doubles = ["1", "2", "foo", "4", "5"]
//!     .into_iter()
//!     .map(|e| usize::from_str(e))
//!     .map_ok(|i| 2*i)
//!     .collect::<Vec<_>>();
//! assert_eq!(doubles[0], Ok(2));
//! assert_eq!(doubles[1], Ok(4));
//! ```
//!
//! * Transforming errors
//!
//! ```
//! use std::str::FromStr;
//! use resiter::map::*;
//!
//! let doubles = ["1", "2", "foo", "4", "5"]
//!     .into_iter()
//!     .map(|e| usize::from_str(e))
//!     .map_err(|e| format!("{:?}", e))
//!     .collect::<Vec<_>>();
//! assert_eq!(doubles[2], Err("ParseIntError { kind: InvalidDigit }".to_string()));
//! ```
//!
//! * Filtering oks (leaving errors as is)
//!
//! ```
//! use std::str::FromStr;
//! use resiter::filter::*;
//!
//! let doubles = ["1", "2", "foo", "4", "5"]
//!     .into_iter()
//!     .map(|e| usize::from_str(e))
//!     .filter_ok(|i| i%2 == 0)
//!     .collect::<Vec<_>>();
//! assert_eq!(doubles.len(), 3);
//! assert_eq!(doubles[0], Ok(2));
//! ```
//!
//! * Filtering errors (leaving oks as is)
//!
//! ```
//! use std::str::FromStr;
//! use resiter::filter::*;
//!
//! let doubles = ["1", "2", "foo", "4", "5"]
//!     .into_iter()
//!     .map(|e| usize::from_str(e))
//!     .filter_err(|_| false) // filter out all errors
//!     .collect::<Vec<_>>();
//! assert_eq!(doubles.len(), 4);
//! assert_eq!(doubles[2], Ok(4));
//! ```
//!
//! * Stopping the iteration on the first error
//!
//! ```
//! use std::str::FromStr;
//! use resiter::while_ok::*;
//!
//! let res = ["1", "2", "foo", "4", "5"]
//!     .into_iter()
//!     .map(|e| usize::from_str(e))
//!     .while_ok(|i| {
//!         println!("{} is a usize", i);
//!     });
//! if res.is_err() {
//!     println!("An error occured");
//! }
//! ```
//!
//! # License
//!
//! MPL 2.0
//!

#![cfg_attr(not(test), no_std)]

pub mod and_then;
pub mod errors;
pub mod filter;
pub mod filter_map;
pub mod flat_map;
pub mod flatten;
pub mod map;
pub mod ok_or_else;
pub mod oks;
pub mod onerr;
pub mod onok;
pub mod prelude;
pub mod try_filter;
pub mod try_filter_map;
pub mod try_map;
pub mod unwrap;
mod util;
pub mod while_ok;

pub use and_then::AndThen;
pub use errors::GetErrors;
pub use filter::Filter;
pub use filter_map::FilterMap;
pub use flat_map::FlatMap;
pub use flatten::Flatten;
pub use map::Map;
pub use ok_or_else::{IterInnerOkOrElse, ResultOptionExt};
pub use oks::GetOks;
pub use onerr::OnErrDo;
pub use onok::OnOkDo;
pub use try_filter::TryFilter;
pub use try_filter_map::TryFilterMap;
pub use try_map::TryMap;
pub use unwrap::UnwrapWithExt;
pub use util::{GetErr, GetOk, Process};
pub use while_ok::WhileOk;