Some docs

This commit is contained in:
Richard Dodd
2018-09-29 15:06:23 +01:00
parent 3f0f739e17
commit da65fe5a52
4 changed files with 35 additions and 1 deletions
+10
View File
@@ -10,18 +10,27 @@ use std::cell::RefCell;
use std::fmt::Display;
use std::thread;
/// A type to collect errors together and format them.
///
/// Dropping this object will cause a panic. It must be consumed using `check`.
///
/// References can be shared since this type uses run-time exclusive mut checking.
#[derive(Default)]
pub struct Ctxt {
// The contents will be set to `None` during checking. This is so that checking can be
// enforced.
errors: RefCell<Option<Vec<String>>>,
}
impl Ctxt {
/// Create a new context object
pub fn new() -> Self {
Ctxt {
errors: RefCell::new(Some(Vec::new())),
}
}
/// Add an error to the context object
pub fn error<T: Display>(&self, msg: T) {
self.errors
.borrow_mut()
@@ -30,6 +39,7 @@ impl Ctxt {
.push(msg.to_string());
}
/// Consume this object, producing a formatted error string if there are errors.
pub fn check(self) -> Result<(), String> {
let mut errors = self.errors.borrow_mut().take().unwrap();
match errors.len() {