resiter_dpc_tmp/
lib.rs

1//
2// This Source Code Form is subject to the terms of the Mozilla Public
3// License, v. 2.0. If a copy of the MPL was not distributed with this
4// file, You can obtain one at http://mozilla.org/MPL/2.0/.
5//
6
7//! resiter
8//!
9//! This crate helps iterating over `Iterator<Item = Result<O, E>>`.
10//! All these things are trivial to build yourself, but why invest the effort if you can use a
11//! crate for this?
12//!
13//! # Contributions welcome
14//!
15//! If you have _anything_ that might fit the scope of this crate, don't hesitate opening a
16//! pull-request for it! This is considered a toolkit and convenience-crate, so everything which
17//! might fit its scope should be merged!
18//!
19//! # Dependencies and Feature-gates
20//!
21//! If a feature of this crate uses external dependencies, it should be hidden behind a feature
22//! gate. The crate itself should be usable without any dependencies besides `std`!
23//!
24//! # Features
25//!
26//! Features included in this crate:
27//!
28//! * Unwrap `Result<O, E>`s inside an Iterator
29//! * Select only `Err(_)`s from the Iterator
30//! * Select only `Ok(_)`s from the Iterator
31//! * Do something in the `Err(_)` case, but don't change the error-object
32//! * Do something in the `Ok(_)` case, but don't change the ok-object
33//!
34//! # Usecase
35//!
36//! * Consuming iterators until an error occurs
37//!
38//! ```
39//! # fn main() {
40//! use std::str::FromStr;
41//! use resiter::errors::*;
42//!
43//! let _ : Option<::std::num::ParseIntError> = ["1", "2", "foo", "4", "5"]
44//!     .into_iter()
45//!     .map(|e| usize::from_str(e))
46//!     .errors()
47//!     .next(); // "4" and "5" will never be processed by the iterator
48//! # }
49//! ```
50//!
51//! * Consuming iterators and collect all errors
52//!
53//! ```
54//! # fn main() {
55//! use std::str::FromStr;
56//! use resiter::errors::*;
57//!
58//! let len = ["1", "2", "foo", "4", "5"]
59//!     .into_iter()
60//!     .map(|e| usize::from_str(e))
61//!     .errors()
62//!     .collect::<Vec<::std::num::ParseIntError>>()
63//!     .len();
64//! assert_eq!(len, 1);
65//! # }
66//! ```
67//!
68//! * Consuming iterators and collect all oks
69//!
70//! ```
71//! # fn main() {
72//! use std::str::FromStr;
73//! use resiter::oks::*;
74//!
75//! let len = ["1", "2", "foo", "4", "5"]
76//!     .into_iter()
77//!     .map(|e| usize::from_str(e))
78//!     .oks() // Could also be done with .filter_map(Result::ok)
79//!     .collect::<Vec<_>>()
80//!     .len();
81//! assert_eq!(len, 4);
82//! # }
83//! ```
84//!
85//! * Printing errors / oks
86//!
87//! ```
88//! # fn main() {
89//! use std::str::FromStr;
90//! use resiter::oks::*;
91//! use resiter::onerr::*;
92//! use resiter::onok::*;
93//!
94//! let len = ["1", "2", "foo", "4", "5"]
95//!     .into_iter()
96//!     .map(|e| usize::from_str(e))
97//!     .on_err(|e| println!("Error happened: {:?}", e)) // ::std::process::exit(1) possible
98//!     .on_ok(|o| println!("Parsed : '{}'", o))
99//!     .oks()
100//!     .collect::<Vec<_>>()
101//!     .len();
102//! assert_eq!(len, 4);
103//! # }
104//! ```
105//!
106//! * Transforming oks
107//!
108//! ```
109//! # fn main() {
110//! use std::str::FromStr;
111//! use resiter::map::*;
112//!
113//! let doubles = ["1", "2", "foo", "4", "5"]
114//!     .into_iter()
115//!     .map(|e| usize::from_str(e))
116//!     .map_ok(|i| 2*i)
117//!     .collect::<Vec<_>>();
118//! assert_eq!(doubles[0], Ok(2));
119//! assert_eq!(doubles[1], Ok(4));
120//! # }
121//! ```
122//!
123//! * Transforming errors
124//!
125//! ```
126//! # fn main() {
127//! use std::str::FromStr;
128//! use resiter::map::*;
129//!
130//! let doubles = ["1", "2", "foo", "4", "5"]
131//!     .into_iter()
132//!     .map(|e| usize::from_str(e))
133//!     .map_err(|e| format!("{:?}", e))
134//!     .collect::<Vec<_>>();
135//! assert_eq!(doubles[2], Err("ParseIntError { kind: InvalidDigit }".to_string()));
136//! # }
137//! ```
138//!
139//! * Filtering oks (leaving errors as is)
140//!
141//! ```
142//! # fn main() {
143//! use std::str::FromStr;
144//! use resiter::filter::*;
145//!
146//! let doubles = ["1", "2", "foo", "4", "5"]
147//!     .into_iter()
148//!     .map(|e| usize::from_str(e))
149//!     .filter_ok(|i| i%2 == 0)
150//!     .collect::<Vec<_>>();
151//! assert_eq!(doubles.len(), 3);
152//! assert_eq!(doubles[0], Ok(2));
153//! # }
154//! ```
155//!
156//! * Filtering errors (leaving oks as is)
157//!
158//! ```
159//! # fn main() {
160//! use std::str::FromStr;
161//! use resiter::filter::*;
162//!
163//! let doubles = ["1", "2", "foo", "4", "5"]
164//!     .into_iter()
165//!     .map(|e| usize::from_str(e))
166//!     .filter_err(|_| false) // filter out all errors
167//!     .collect::<Vec<_>>();
168//! assert_eq!(doubles.len(), 4);
169//! assert_eq!(doubles[2], Ok(4));
170//! # }
171//! ```
172//!
173//! * Stopping the iteration on the first error
174//!
175//! ```
176//! # fn main() -> () {
177//! use std::str::FromStr;
178//! use resiter::while_ok::*;
179//!
180//! let res = ["1", "2", "foo", "4", "5"]
181//!     .into_iter()
182//!     .map(|e| usize::from_str(e))
183//!     .while_ok(|i| {
184//!         println!("{} is a usize", i);
185//!     });
186//! if res.is_err() {
187//!     println!("An error occured");
188//! }
189//! # }
190//! ```
191//!
192//! # License
193//!
194//! MPL 2.0
195//!
196
197pub mod and_then;
198pub mod errors;
199pub mod filter;
200pub mod filter_map;
201pub mod flat_map;
202pub mod flatten;
203pub mod map;
204pub mod oks;
205pub mod onerr;
206pub mod onok;
207pub mod prelude;
208pub mod unwrap;
209mod util;
210pub mod while_ok;
211
212pub use and_then::AndThen;
213pub use errors::GetErrors;
214pub use filter::Filter;
215pub use filter_map::FilterMap;
216pub use flat_map::FlatMap;
217pub use flatten::Flatten;
218pub use map::Map;
219pub use oks::GetOks;
220pub use onerr::OnErrDo;
221pub use onok::OnOkDo;
222pub use unwrap::UnwrapWithExt;
223pub use util::{GetErr, GetOk, Process};
224pub use while_ok::WhileOk;