Skip to content

Commit

Permalink
(feat) refactor render context [#42]
Browse files Browse the repository at this point in the history
Signed-off-by: Ning Sun <[email protected]>
  • Loading branch information
sunng87 committed Sep 7, 2016
1 parent ce36751 commit 453bfa0
Showing 1 changed file with 23 additions and 23 deletions.
46 changes: 23 additions & 23 deletions src/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,11 +192,8 @@ impl<'a> RenderContext<'a> {
self.local_variables = new_map;
}

pub fn get_local_var(&self, name: &String) -> &Json {
match self.local_variables.get(name) {
Some(j) => j,
None => &self.default_var,
}
pub fn get_local_var(&self, name: &String) -> Option<&Json> {
self.local_variables.get(name)
}

pub fn writer(&mut self) -> &mut Write {
Expand Down Expand Up @@ -361,22 +358,25 @@ impl Parameter {
-> Result<ContextJson, RenderError> {
match self {
&Parameter::Name(ref name) => {
if name.starts_with("@") {
Ok(ContextJson {
path: None,
value: rc.get_local_var(&name).clone(),
})
} else {
let path = if name.starts_with("../") {
rc.get_local_path_root()
} else {
rc.get_path()
};
Ok(ContextJson {
path: Some(name.to_owned()),
value: ctx.navigate(path, name).clone(),
})
}
Ok(rc.get_local_var(&name).map_or_else(|| {
let path = if name.starts_with("../") {
rc.get_local_path_root()
} else {
rc.get_path()
};
ContextJson {
path: Some(name.to_owned()),
value: ctx.navigate(path, name)
.clone(),
}

},
|v| {
ContextJson {
path: None,
value: v.clone(),
}
}))
}
&Parameter::Literal(ref j) => {
Ok(ContextJson {
Expand Down Expand Up @@ -594,12 +594,12 @@ fn test_render_context_promotion_and_demotion() {

render_context.promote_local_vars();

assert_eq!(render_context.get_local_var(&"@../index".to_string()),
assert_eq!(render_context.get_local_var(&"@../index".to_string()).unwrap(),
&0usize.to_json());

render_context.demote_local_vars();

assert_eq!(render_context.get_local_var(&"@index".to_string()),
assert_eq!(render_context.get_local_var(&"@index".to_string()).unwrap(),
&0usize.to_json());
}

Expand Down

0 comments on commit 453bfa0

Please sign in to comment.