BooleanErrors

Trait BooleanErrors 

Source
pub trait BooleanErrors {
    // Required methods
    fn error_if_false(
        self,
        context: impl AsRef<str>,
    ) -> Result<bool, ErrorMessage>;
    fn error_if_true(
        self,
        context: impl AsRef<str>,
    ) -> Result<bool, ErrorMessage>;
    fn error_dyn_if_false(
        self,
        context: impl FnOnce() -> String,
    ) -> Result<bool, ErrorMessage>;
    fn error_dyn_if_true(
        self,
        context: impl FnOnce() -> String,
    ) -> Result<bool, ErrorMessage>;
}
Expand description

This trait allows one to turn bools into ErrorMessages, when they have a certain value.

This allows for cool code like this:

use errors_with_context::{BooleanErrors, ErrorMessage};
let path = Path::new("test.file");
path.exists()
    .error_if_false("Expected file to exist!")?;

or with more dynamic context:

use errors_with_context::prelude::*;
let path = Path::new("test.file");
path.exists()
    .error_dyn_if_false(|| format!("Expected file '{}' to exist!", path.display()))?;

Very useful, when doing lots of checks that aren’t immediately errors.

Required Methods§

Source

fn error_if_false(self, context: impl AsRef<str>) -> Result<bool, ErrorMessage>

If the bool is true, return it Ok(bool).
If the bool is false, return an Err(ErrorMessage) with the provided context string..

use errors_with_context::prelude::*;
let path = Path::new("test.file");
path.exists()
    .error_if_false("Expected file to exist!")?;
Source

fn error_if_true(self, context: impl AsRef<str>) -> Result<bool, ErrorMessage>

If the bool is false, return it wrapped in a Ok(bool).
If the bool is true, return an Err(ErrorMessage) with the provided context string.

use errors_with_context::prelude::*;
let path = Path::new("test.file");
path.exists()
    .not()
    .error_if_true("Expected file to exist!")?;
Source

fn error_dyn_if_false( self, context: impl FnOnce() -> String, ) -> Result<bool, ErrorMessage>

If the bool is true, return it Ok(bool).
If the bool is false, compute the context and return an Err(ErrorMessage).

use errors_with_context::prelude::*;
let path = Path::new("test.file");
path.exists()
    .error_dyn_if_false(|| format!("Expected file '{}' to exist!", path.display()))?;
Source

fn error_dyn_if_true( self, context: impl FnOnce() -> String, ) -> Result<bool, ErrorMessage>

If the bool is false, return it Ok(bool).
If the bool is true, compute the context and return an Err(ErrorMessage).

use errors_with_context::prelude::*;
let path = Path::new("test.file");
path.exists()
    .not()
    .error_dyn_if_true(|| format!("Expected file '{}' to exist!", path.display()))?;

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 BooleanErrors for bool

Source§

fn error_if_false(self, context: impl AsRef<str>) -> Result<bool, ErrorMessage>

Source§

fn error_if_true(self, context: impl AsRef<str>) -> Result<bool, ErrorMessage>

Source§

fn error_dyn_if_false( self, context: impl FnOnce() -> String, ) -> Result<bool, ErrorMessage>

Source§

fn error_dyn_if_true( self, context: impl FnOnce() -> String, ) -> Result<bool, ErrorMessage>

Implementors§