use scope::{ScopeData, ScopeValue, NO_VALUE};
use std::rc::Rc;
use std::cell::{Ref, RefMut, RefCell};
use std::io::Write;
use ast::{TransformedNode, AstLocation};
use std::path::PathBuf;
use std::fmt::Debug;
use errors::Result;
pub trait Transform: Debug {
fn transform(&self, ctx: TransformContext)
-> Result<Option<TransformedNode>>;
fn location(&self) -> AstLocation;
}
#[derive(Clone)]
pub struct TransformContext {
pub scope: Rc<RefCell<ScopeData>>,
pub path: Rc<PathBuf>
}
impl TransformContext {
pub fn mut_scope(&self) -> RefMut<ScopeData> {
self.scope.borrow_mut()
}
pub fn ref_scope(&self) -> Ref<ScopeData> {
self.scope.borrow()
}
}
pub trait TransformResult {
fn return_value(&self) -> ScopeValue {
ScopeValue::None(&NO_VALUE)
}
fn render(&self, ctx: RenderContext, buf: &mut Write) -> Result<()>;
}
#[derive(Debug)]
pub struct RenderConfig {
pub pretty: bool,
pub indent_str: &'static str,
pub base_path: PathBuf,
pub line_ending: &'static [u8],
pub comments: bool
}
impl RenderConfig {
pub fn indent(&self) -> bool {
self.pretty
}
}
#[derive(Clone, Copy)]
pub struct RenderContext<'a> {
pub config: &'a RenderConfig,
pub indent_level: isize,
}
impl<'a> RenderContext<'a> {
pub fn write_indent(&self, buf: &mut Write) -> ::std::io::Result<usize> {
if self.config.indent() {
let mut written = 0;
#[allow(unused_variables)]
for i in 0..self.indent_level {
written += buf.write(self.config.indent_str.as_ref())?;
}
return Ok(written)
}
Ok(0)
}
pub fn increase_indent(&self) -> Self {
RenderContext {
indent_level: self.indent_level + 1,
.. *self
}
}
}