use errors::Result;
use std::cell::{BorrowError, Ref, RefCell};
use std::path::Path;
use std::rc::Rc;
use std::result;
use {Diagnostics, Filesystem, Handle};
#[derive(Debug)]
pub enum ContextItem {
Diagnostics { diagnostics: Diagnostics },
}
#[derive(Clone)]
pub struct Context {
filesystem: Rc<Box<Filesystem>>,
items: Rc<RefCell<Vec<ContextItem>>>,
}
impl Context {
pub fn new(filesystem: Box<Filesystem>) -> Context {
Context {
filesystem: Rc::new(filesystem),
items: Rc::new(RefCell::new(vec![])),
}
}
pub fn with_items(self, items: Rc<RefCell<Vec<ContextItem>>>) -> Context {
Context { items, ..self }
}
pub fn map_filesystem<F>(self, map: F) -> Self
where
F: FnOnce(Rc<Box<Filesystem>>) -> Box<Filesystem>,
{
Context {
filesystem: Rc::new(map(self.filesystem)),
..self
}
}
pub fn filesystem(&self, root: Option<&Path>) -> Result<Box<Handle>> {
self.filesystem.open_root(root)
}
pub fn diagnostics(&self, diagnostics: Diagnostics) -> Result<()> {
self.items
.try_borrow_mut()
.map_err(|_| "no mutable access to context")?
.push(ContextItem::Diagnostics { diagnostics });
Ok(())
}
pub fn items(&self) -> result::Result<Ref<Vec<ContextItem>>, BorrowError> {
self.items.try_borrow()
}
pub fn has_diagnostics(&self) -> Result<bool> {
Ok(self.items
.try_borrow()
.map_err(|_| "immutable access to context")?
.iter()
.any(|item| match *item {
ContextItem::Diagnostics { ref diagnostics } => diagnostics.has_errors(),
}))
}
pub fn clear(&self) -> Result<()> {
self.items
.try_borrow_mut()
.map_err(|_| "no mutable access to context")?
.clear();
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;
use {CapturingFilesystem, Diagnostics, Source, Span};
#[test]
fn test_handle() {
let ctx = Context::new(Box::new(CapturingFilesystem::new()));
let mut diag = Diagnostics::new(Source::empty("test"));
diag.err(Span::empty(), "nope");
diag.err(Span::empty(), "previously reported here");
ctx.diagnostics(diag).expect("bad diagnostic");
assert_eq!(1, ctx.items().unwrap().len());
}
}