1use std::{
2 error::Error,
3 fmt::{Display, Formatter, Result as FmtResult},
4};
5
6#[derive(Clone, Debug)]
7pub struct GuardError;
8
9impl Error for GuardError {}
10
11impl Display for GuardError {
12 fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
13 f.write_str("guard condition failed")
14 }
15}
16
17pub fn guard(boolean: bool) -> Result<(), GuardError> {
18 if boolean {
19 Ok(())
20 } else {
21 Err(GuardError {})
22 }
23}
24
25#[cfg(test)]
26mod tests {
27 use super::GuardError;
28 use std::fmt::Debug;
29
30 static_assertions::assert_impl_all!(GuardError: Clone, Debug, Send, Sync);
31
32 #[test]
33 fn success() {
34 assert!(super::guard(true).is_ok());
35 }
36
37 #[test]
38 fn failure() {
39 assert!(super::guard(false).is_err());
40 }
41}