rep/
lib.rs

1//! Rep is a small tool for checking representation/class invariants
2
3pub use rep_derive::*;
4pub use rep_derive::check_rep;
5pub use log::Level::Error;
6pub use log::{error, log_enabled};
7
8/// A trait for representation checking
9pub trait CheckRep {
10	/// Returns true if representation is correct, false otherwise
11    fn is_correct(&self) -> bool {
12    	self.correctness().is_ok()
13    }
14
15    /// Returns Ok if representation is correct, vector of errors otherwise
16    fn correctness(&self) -> Result<(), Vec<String>> {
17    	if self.is_correct() {
18    		Ok(())
19    	} else {
20    		Err(vec![])
21    	}
22    }
23
24    /// Asserts that self is correct
25	fn check_rep(&self) {
26		match self.correctness() {
27			Ok(_) => {}
28			Err(errors) => {
29				if log_enabled!(Error) {
30					for error in errors {
31						error!("representation invariant violated: {:?}", error);
32					}
33				} else {
34					if errors.len() > 0 {
35						panic!("representation invariant violated: {:?}", errors);	
36					} else {
37						panic!("representation invariant violated");	
38					}
39				}
40			}
41		}
42	}
43}
44
45/// A trait for adding extra rep-checking functionality to a data structure with `CheckRep` implemented
46pub trait CustomCheckRep {
47	/// Returns true if representation is correct, false otherwise
48    fn c_is_correct(&self) -> bool {
49    	self.c_correctness().is_ok()
50    }
51
52    /// Returns Ok if representation is correct, vector of errors otherwise
53    fn c_correctness(&self) -> Result<(), Vec<String>> {
54    	if self.c_is_correct() {
55    		Ok(())
56    	} else {
57    		Err(vec![])
58    	}
59    }
60}