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
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
}
}
}