[][src]Crate resiter

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

Re-exports

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 oks::GetOks;
pub use onerr::OnErrDo;
pub use onok::OnOkDo;
pub use unwrap::UnwrapWithExt;
pub use while_ok::WhileOk;

Modules

and_then
errors
filter
filter_map
flat_map
flatten
map
oks
onerr
onok
prelude

Prelude

unwrap
while_ok

Traits

GetErr
GetOk
Process

Extend any Iterator with a process method, equivalent to a fallible for_each.