resiter/
errors.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#[cfg(not(test))]
8use core::iter::*;
9#[cfg(test)]
10use std::iter::*;
11
12use util::*;
13
14pub use util::Process as Errors;
15// for backward compatibility with previous implementation
16
17/// Extension trait for `Iterator<Item = Result<T, E>>` to get all `E`s
18#[allow(clippy::type_complexity)]
19pub trait GetErrors<T, E>: Sized {
20    /// Get all errors from this `Iterator`
21    ///
22    /// ```
23    /// use std::str::FromStr;
24    /// use resiter::GetErrors;
25    ///
26    /// let res: Vec<std::num::ParseIntError> = ["1", "2", "a", "4", "b"]
27    ///     .iter()
28    ///     .map(|e| usize::from_str(e))
29    ///     .errors()
30    ///     .collect();
31    ///
32    /// assert_eq!(res.len(), 2);
33    /// ```
34    fn errors(self) -> FilterMap<Self, fn(Result<T, E>) -> Option<E>>;
35}
36
37impl<T, E, I> GetErrors<T, E> for I
38where
39    I: Iterator<Item = Result<T, E>> + Sized,
40{
41    #[allow(clippy::type_complexity)]
42    fn errors(self) -> FilterMap<Self, fn(Result<T, E>) -> Option<E>> {
43        self.filter_map(GetErr::get_err)
44    }
45}