@@ -4,10 +4,10 @@ use compiler::TemplateCompiler;
44use error:: Error :: * ;
55use error:: * ;
66use instruction:: { Instruction , PathSlice , PathStep } ;
7- use serde_json:: Value ;
87use std:: collections:: HashMap ;
98use std:: fmt:: Write ;
109use std:: slice;
10+ use value:: Value ;
1111use ValueFormatter ;
1212
1313/// Enum defining the different kinds of records on the context stack.
@@ -64,17 +64,23 @@ impl<'render, 'template> RenderContext<'render, 'template> {
6464 let mut current = object;
6565 for step in path. iter ( ) {
6666 if let PathStep :: Index ( _, n) = step {
67- if let Some ( next) = current. get ( n) {
68- current = next;
69- continue ;
67+ if let Value :: Array ( array) = current {
68+ if let Some ( next) = array. get ( * n) {
69+ current = next;
70+ continue ;
71+ }
7072 }
7173 }
7274
7375 let step: & str = & * step;
7476
75- match current. get ( step) {
76- Some ( next) => current = next,
77- None => return Err ( lookup_error ( self . original_text , step, path, current) ) ,
77+ match & current {
78+ Value :: Object ( map) => match map. get ( step) {
79+ Some ( next) => current = next,
80+ None => return Err ( lookup_error ( self . original_text , step, path, current) ) ,
81+ } ,
82+
83+ _ => return Err ( lookup_error ( self . original_text , step, path, current) ) ,
7884 }
7985 }
8086 Ok ( current)
@@ -222,12 +228,12 @@ impl<'template> Template<'template> {
222228 let ( index, length) = render_context. lookup_index ( ) ?;
223229 index == ( length - 1 )
224230 }
225- "@root" => self . value_is_truthy ( render_context. lookup_root ( ) ?, path ) ? ,
231+ "@root" => self . value_is_truthy ( render_context. lookup_root ( ) ?) ,
226232 other => panic ! ( "Unknown keyword {}" , other) , // This should have been caught by the parser.
227233 }
228234 } else {
229235 let value_to_render = render_context. lookup ( path) ?;
230- self . value_is_truthy ( value_to_render, path ) ?
236+ self . value_is_truthy ( value_to_render)
231237 } ;
232238 if * negate {
233239 truthy = !truthy;
@@ -325,21 +331,16 @@ impl<'template> Template<'template> {
325331 Ok ( ( ) )
326332 }
327333
328- fn value_is_truthy ( & self , value : & Value , path : PathSlice ) -> Result < bool > {
329- let truthy = match value {
334+ fn value_is_truthy ( & self , value : & Value ) -> bool {
335+ match value {
330336 Value :: Null => false ,
331- Value :: Bool ( b) => * b,
332- Value :: Number ( n) => match n. as_f64 ( ) {
333- Some ( float) => float != 0.0 ,
334- None => {
335- return Err ( truthiness_error ( self . original_text , path) ) ;
336- }
337- } ,
337+ Value :: Boolean ( b) => * b,
338+ Value :: Integer ( i) => * i != 0 ,
339+ Value :: Float ( f) => * f != 0.0 ,
338340 Value :: String ( s) => !s. is_empty ( ) ,
339341 Value :: Array ( arr) => !arr. is_empty ( ) ,
340342 Value :: Object ( _) => true ,
341- } ;
342- Ok ( truthy)
343+ }
343344 }
344345}
345346
@@ -382,7 +383,8 @@ mod test {
382383 nested : NestedContext { value : 10 } ,
383384 escapes : "1:< 2:> 3:& 4:' 5:\" " ,
384385 } ;
385- :: serde_json:: to_value ( & ctx) . unwrap ( )
386+
387+ Value :: serialize_from ( & ctx) . unwrap ( )
386388 }
387389
388390 fn other_templates ( ) -> HashMap < & ' static str , Template < ' static > > {
@@ -807,7 +809,7 @@ mod test {
807809 fn test_root_print ( ) {
808810 let template = compile ( "{ @root }" ) ;
809811 let context = "Hello World!" ;
810- let context = :: serde_json :: to_value ( & context) . unwrap ( ) ;
812+ let context = Value :: serialize_from ( & context) . unwrap ( ) ;
811813 let template_registry = other_templates ( ) ;
812814 let formatter_registry = formatters ( ) ;
813815 let string = template
@@ -825,7 +827,7 @@ mod test {
825827 fn test_root_branch ( ) {
826828 let template = compile ( "{{ if @root }}Hello World!{{ endif }}" ) ;
827829 let context = true ;
828- let context = :: serde_json :: to_value ( & context) . unwrap ( ) ;
830+ let context = Value :: serialize_from ( & context) . unwrap ( ) ;
829831 let template_registry = other_templates ( ) ;
830832 let formatter_registry = formatters ( ) ;
831833 let string = template
@@ -843,7 +845,7 @@ mod test {
843845 fn test_root_iterate ( ) {
844846 let template = compile ( "{{ for a in @root }}{ a }{{ endfor }}" ) ;
845847 let context = vec ! [ "foo" , "bar" ] ;
846- let context = :: serde_json :: to_value ( & context) . unwrap ( ) ;
848+ let context = Value :: serialize_from ( & context) . unwrap ( ) ;
847849 let template_registry = other_templates ( ) ;
848850 let formatter_registry = formatters ( ) ;
849851 let string = template
@@ -861,7 +863,7 @@ mod test {
861863 fn test_number_truthiness_zero ( ) {
862864 let template = compile ( "{{ if @root }}truthy{{else}}not truthy{{ endif }}" ) ;
863865 let context = 0 ;
864- let context = :: serde_json :: to_value ( & context) . unwrap ( ) ;
866+ let context = Value :: serialize_from ( & context) . unwrap ( ) ;
865867 let template_registry = other_templates ( ) ;
866868 let formatter_registry = formatters ( ) ;
867869 let string = template
@@ -879,7 +881,7 @@ mod test {
879881 fn test_number_truthiness_one ( ) {
880882 let template = compile ( "{{ if @root }}truthy{{else}}not truthy{{ endif }}" ) ;
881883 let context = 1 ;
882- let context = :: serde_json :: to_value ( & context) . unwrap ( ) ;
884+ let context = Value :: serialize_from ( & context) . unwrap ( ) ;
883885 let template_registry = other_templates ( ) ;
884886 let formatter_registry = formatters ( ) ;
885887 let string = template
@@ -902,7 +904,7 @@ mod test {
902904
903905 let template = compile ( "{ foo.1 }{ foo.0 }" ) ;
904906 let context = Context { foo : ( 123 , 456 ) } ;
905- let context = :: serde_json :: to_value ( & context) . unwrap ( ) ;
907+ let context = Value :: serialize_from ( & context) . unwrap ( ) ;
906908 let template_registry = other_templates ( ) ;
907909 let formatter_registry = formatters ( ) ;
908910 let string = template
@@ -928,7 +930,7 @@ mod test {
928930 foo. insert ( "0" , 123 ) ;
929931 foo. insert ( "1" , 456 ) ;
930932 let context = Context { foo } ;
931- let context = :: serde_json :: to_value ( & context) . unwrap ( ) ;
933+ let context = Value :: serialize_from ( & context) . unwrap ( ) ;
932934 let template_registry = other_templates ( ) ;
933935 let formatter_registry = formatters ( ) ;
934936 let string = template
0 commit comments