use doc::Object;
use query::Query;
use value::Set;
use value::fields::{Key, Path, Segment};
#[derive(Debug)]
pub struct Context<'v> {
incl: &'v mut Set<Object>,
kind: Key,
path: Path,
query: Option<&'v Query>,
}
impl<'v> Context<'v> {
pub fn new(
kind: Key,
query: Option<&'v Query>,
included: &'v mut Set<Object>,
) -> Self {
Context {
kind,
query,
incl: included,
path: Path::new(),
}
}
pub fn field(&self, name: &str) -> bool {
self.query
.and_then(|q| q.fields.get(&self.kind))
.map_or(true, |f| f.contains(name))
}
pub fn fork(&mut self, kind: Key, key: &Key) -> Context {
Context {
kind,
incl: self.incl,
path: self.path.join(key),
query: self.query,
}
}
pub fn include(&mut self, value: Object) -> bool {
self.incl.insert(value)
}
pub fn included(&self) -> bool {
self.query.map_or(false, |q| q.include.contains(&self.path))
}
}