OptionExt

Trait OptionExt 

Source
pub trait OptionExt<T> {
    // Required methods
    fn ok_or_user_err<S: Into<Cow<'static, str>>>(
        self,
        msg: S,
        advice: &'static [&'static str],
    ) -> Result<T, Error>;
    fn ok_or_system_err<S: Into<Cow<'static, str>>>(
        self,
        msg: S,
        advice: &'static [&'static str],
    ) -> Result<T, Error>;
}
Expand description

Extension trait for Option to convert None values into user-friendly error types.

§Examples

use human_errors::OptionExt;
 
// Converts a `None` value into a user-caused error with the provided message and advice.
let value = None::<i32>.ok_or_user_err(
   "No value was provided.",
   &["Please provide a valid integer value."],
);
 
// Converts a `None` value into a system-caused error with the provided message and advice.
let value = None::<i32>.ok_or_system_err(
  "No value was provided.",
  &["Please check your system configuration."],
);

Required Methods§

Source

fn ok_or_user_err<S: Into<Cow<'static, str>>>( self, msg: S, advice: &'static [&'static str], ) -> Result<T, Error>

Converts an Option<T> into a Result<T, Error>, returning a user-caused error with the provided message and advice if the option is None.

§Examples
use human_errors::OptionExt;
 
let value = None::<i32>.ok_or_user_err(
  "No value was provided.",
  &["Please provide a valid integer value."],
);
Source

fn ok_or_system_err<S: Into<Cow<'static, str>>>( self, msg: S, advice: &'static [&'static str], ) -> Result<T, Error>

Converts an Option<T> into a Result<T, Error>, returning a system-caused error with the provided message and advice if the option is None.

§Examples
use human_errors::OptionExt;
 
let value = None::<i32>.ok_or_system_err(
  "No value was provided.",
  &["Please check your system configuration."], 
);

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl<T> OptionExt<T> for Option<T>

Source§

fn ok_or_user_err<S: Into<Cow<'static, str>>>( self, msg: S, advice: &'static [&'static str], ) -> Result<T, Error>

Source§

fn ok_or_system_err<S: Into<Cow<'static, str>>>( self, msg: S, advice: &'static [&'static str], ) -> Result<T, Error>

Implementors§