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§
Sourcefn error_if_false(self, context: impl AsRef<str>) -> Result<bool, ErrorMessage>
fn error_if_false(self, context: impl AsRef<str>) -> Result<bool, ErrorMessage>
Sourcefn error_if_true(self, context: impl AsRef<str>) -> Result<bool, ErrorMessage>
fn error_if_true(self, context: impl AsRef<str>) -> Result<bool, ErrorMessage>
Sourcefn error_dyn_if_false(
self,
context: impl FnOnce() -> String,
) -> Result<bool, ErrorMessage>
fn error_dyn_if_false( self, context: impl FnOnce() -> String, ) -> Result<bool, ErrorMessage>
Sourcefn error_dyn_if_true(
self,
context: impl FnOnce() -> String,
) -> Result<bool, ErrorMessage>
fn error_dyn_if_true( self, context: impl FnOnce() -> String, ) -> Result<bool, ErrorMessage>
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.