use std::{
error::Error,
fmt::{Display, Formatter, Result as FmtResult},
};
#[derive(Clone, Debug)]
pub struct GuardError;
impl Error for GuardError {}
impl Display for GuardError {
fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
f.write_str("guard condition failed")
}
}
pub fn guard(boolean: bool) -> Result<(), GuardError> {
if boolean {
Ok(())
} else {
Err(GuardError {})
}
}
#[cfg(test)]
mod tests {
use super::GuardError;
use std::fmt::Debug;
static_assertions::assert_impl_all!(GuardError: Clone, Debug, Send, Sync);
#[test]
fn success() {
assert!(super::guard(true).is_ok());
}
#[test]
fn failure() {
assert!(super::guard(false).is_err());
}
}