Crate skip_error[−][src]
Expand description
This crate provides a single macro to help skipping a error in a loop, possibly logging it.
For example, imagine you have some code like this.
for string_number in &["1", "2", "three", "4"] {
let number: u32 = match string_number.parse() {
Ok(n) => n,
Err(e) => continue,
};
}
Then you can use the macro skip_error!
to write like this.
for string_number in &["1", "2", "three", "4"] {
let number: u32 = skip_error!(string_number.parse());
}
Or even better, use the trait SkipError
that extends Iterator
and do
the following (essentially equivalent to Iterator::flatten()
but see
below for logging abilities).
use skip_error::SkipError;
let numbers: Vec<u32> = ["1", "2", "three", "4"]
.into_iter()
.map(|string_number| string_number.parse())
.skip_error()
.collect();
Logging
If you want the error to be logged, you can use the feature log
or the
feature tracing
(see Features). See skip_error_and_log!
and SkipError::skip_error_and_log()
for more information.
Features
log
: emit log message with the standardstd::log
macro. Disabled by default.tracing
: emit traces with thetracing::trace
macro. Disabled by default. If bothlog
andtracing
are enabled, thenlog
will be ignored sincetracing
is configured in a compatibility mode with standardlog
.
Macros
skip_error
returns the value of a Result
or continues a loop.
skip_error_and_debug
returns the value of a Result
or log with tracing::Level::DEBUG
and continues the loop.
skip_error_and_error
returns the value of a Result
or log with tracing::Level::ERROR
and continues the loop.
skip_error_and_info
returns the value of a Result
or log with tracing::Level::INFO
and continues the loop.
skip_error_and_log
returns the value of a Result
or log and continues
the loop.
skip_error_and_trace
returns the value of a Result
or log with tracing::Level::TRACE
and continues the loop.
skip_error_and_warn
returns the value of a Result
or log with tracing::Level::WARN
and continues the loop.
Structs
An iterator that ignore errors
Traits
Trait to extend any Iterator
where the Iterator::Item
is a Result
.
This allows to skip errors and keep only the Ok()
values.