1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
//! The context of a single execution.
//!
//! Is used to accumulate errors.
//!
//! This is preferred over results, since it permits reporting complex errors and their
//! corresponding locations.

use errors::{Error, Result};
use std::cell::{BorrowError, Ref, RefCell};
use std::fmt;
use std::path::Path;
use std::rc::Rc;
use std::result;
use {ErrorPos, Filesystem, Handle};

pub enum ContextItem {
    /// A positional error.
    ErrorPos(ErrorPos, String),
    /// A positional information string.
    InfoPos(ErrorPos, String),
}

/// Context for a single reproto run.
pub struct Context {
    /// Filesystem abstraction.
    filesystem: Box<Filesystem>,
    /// Collected context errors.
    errors: Rc<RefCell<Vec<ContextItem>>>,
}

/// A reporter that processes the given error for the context.
///
/// Converting the reporter into an `ErrorKind` causes it to accumulate the errors to the `Context`.
pub struct Reporter<'a> {
    ctx: &'a Context,
    errors: Vec<ContextItem>,
}

impl<'a> Reporter<'a> {
    pub fn err<P: Into<ErrorPos>, E: fmt::Display>(mut self, pos: P, error: E) -> Self {
        self.errors
            .push(ContextItem::ErrorPos(pos.into(), error.to_string()));

        self
    }

    pub fn info<P: Into<ErrorPos>, I: fmt::Display>(mut self, pos: P, info: I) -> Self {
        self.errors
            .push(ContextItem::InfoPos(pos.into(), info.to_string()));

        self
    }

    /// Close the reporter, saving any reported errors to the context.
    pub fn close(self) -> Option<Error> {
        if self.errors.is_empty() {
            return None;
        }

        let ctx = self.ctx;

        let mut errors = ctx.errors
            .try_borrow_mut()
            .expect("exclusive mutable access");

        errors.extend(self.errors);
        Some(Error::new("Error in Context"))
    }

    /// Check if reporter is empty.
    pub fn is_empty(&self) -> bool {
        self.errors.is_empty()
    }
}

impl<'a> From<Reporter<'a>> for Error {
    fn from(reporter: Reporter<'a>) -> Error {
        reporter.close();
        Error::new("Error in Context")
    }
}

impl Context {
    /// Create a new context with the given filesystem.
    pub fn new(filesystem: Box<Filesystem>) -> Context {
        Context {
            filesystem: filesystem,
            errors: Rc::new(RefCell::new(vec![])),
        }
    }

    /// Modify the existing context with a reference to the given errors.
    pub fn with_errors(self, errors: Rc<RefCell<Vec<ContextItem>>>) -> Context {
        Context {
            errors: errors,
            ..self
        }
    }

    /// Retrieve the filesystem abstraction.
    pub fn filesystem(&self, root: Option<&Path>) -> Result<Box<Handle>> {
        self.filesystem.open_root(root)
    }

    /// Build a handle that can be used in conjunction with Result#map_err.
    pub fn report(&self) -> Reporter {
        Reporter {
            ctx: self,
            errors: Vec::new(),
        }
    }

    /// Iterate over all reporter errors.
    pub fn errors(&self) -> result::Result<Ref<Vec<ContextItem>>, BorrowError> {
        self.errors.try_borrow()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use fs::CapturingFilesystem;
    use object::{BytesObject, Object};
    use pos::Pos;
    use std::rc::Rc;
    use std::result;
    use std::sync::Arc;

    #[test]
    fn test_handle() {
        let object = BytesObject::new("test".to_string(), Arc::new(Vec::new()));

        let pos: Pos = (Rc::new(object.clone_object()), 0usize, 0usize).into();
        let other_pos: Pos = (Rc::new(object.clone_object()), 0usize, 0usize).into();

        let ctx = Context::new(Box::new(CapturingFilesystem::new()));

        let result: result::Result<(), &str> = Err("nope");

        let a: Result<()> = result.map_err(|e| {
            ctx.report()
                .err(pos, e)
                .err(other_pos, "previously reported here")
                .into()
        });

        let e = a.unwrap_err();

        match e {
            ref e if e.message() == "Error in Context" => {}
            ref other => panic!("unexpected: {:?}", other),
        }

        assert_eq!(2, ctx.errors().unwrap().len());
    }
}