Skip to content

Commit

Permalink
(refactor) remove duplicate code between features
Browse files Browse the repository at this point in the history
Signed-off-by: Ning Sun <sunng@about.me>
  • Loading branch information
sunng87 committed Sep 24, 2016
1 parent d8b12e4 commit 25ea472
Showing 1 changed file with 2 additions and 31 deletions.
33 changes: 2 additions & 31 deletions src/helpers/helper_lookup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ use render::{RenderContext, RenderError, Helper};
pub struct LookupHelper;

impl HelperDef for LookupHelper {
#[cfg(all(feature = "rustc_ser_type", not(feature = "serde_type")))]
fn call(&self,
_: &Context,
h: &Helper,
Expand All @@ -35,42 +34,14 @@ impl HelperDef for LookupHelper {
.and_then(|u| v.get(u))
.unwrap_or(&null)
}
#[cfg(all(feature = "rustc_ser_type", not(feature = "serde_type")))]
&Json::Object(ref m) => {
index.value()
.as_string()
.and_then(|k| m.get(k))
.unwrap_or(&null)
}
_ => &null,
};
let r = value.render();
try!(rc.writer.write(r.into_bytes().as_ref()));
Ok(())
}

#[cfg(feature = "serde_type")]
fn call(&self,
_: &Context,
h: &Helper,
_: &Registry,
rc: &mut RenderContext)
-> Result<(), RenderError> {
let collection_value = try!(h.param(0).ok_or_else(|| {
RenderError::new("Param not found for helper \"lookup\"")
}));
let index = try!(h.param(1).ok_or_else(|| {
RenderError::new("Insufficient params for helper \"lookup\"")
}));

let null = Json::Null;
let value = match collection_value.value() {
&Json::Array(ref v) => {
index.value()
.as_u64()
.and_then(|u| Some(u as usize))
.and_then(|u| v.get(u))
.unwrap_or(&null)
}
#[cfg(feature = "serde_type")]
&Json::Object(ref m) => {
index.value()
.as_str()
Expand Down

0 comments on commit 25ea472

Please sign in to comment.