@@ -4,7 +4,6 @@ use std::collections::BTreeMap;
44use serde_json;
55use handlebars:: { Context , Handlebars , Helper , RenderContext , RenderError , Renderable } ;
66
7-
87type StringMap = BTreeMap < String , String > ;
98
109/// Target for `find_chapter`.
@@ -15,22 +14,23 @@ enum Target {
1514
1615impl Target {
1716 /// Returns target if found.
18- fn find ( & self ,
19- base_path : & String ,
20- current_path : & String ,
21- current_item : & StringMap ,
22- previous_item : & StringMap ,
23- ) -> Result < Option < StringMap > , RenderError > {
17+ fn find (
18+ & self ,
19+ base_path : & String ,
20+ current_path : & String ,
21+ current_item : & StringMap ,
22+ previous_item : & StringMap ,
23+ ) -> Result < Option < StringMap > , RenderError > {
2424 match self {
2525 & Target :: Next => {
26- let previous_path = previous_item. get ( "path" ) . ok_or_else ( || {
27- RenderError :: new ( "No path found for chapter in JSON data ")
28- } ) ?;
26+ let previous_path = previous_item
27+ . get ( " path")
28+ . ok_or_else ( || RenderError :: new ( "No path found for chapter in JSON data" ) ) ?;
2929
3030 if previous_path == base_path {
3131 return Ok ( Some ( current_item. clone ( ) ) ) ;
3232 }
33- } ,
33+ }
3434
3535 & Target :: Previous => {
3636 if current_path == base_path {
@@ -43,21 +43,18 @@ impl Target {
4343 }
4444}
4545
46- fn find_chapter (
47- rc : & mut RenderContext ,
48- target : Target
49- ) -> Result < Option < StringMap > , RenderError > {
46+ fn find_chapter ( rc : & mut RenderContext , target : Target ) -> Result < Option < StringMap > , RenderError > {
5047 debug ! ( "Get data from context" ) ;
5148
52- let chapters = rc. evaluate_absolute ( "chapters" ) . and_then ( |c| {
49+ let chapters = rc. evaluate_absolute ( "chapters" , true ) . and_then ( |c| {
5350 serde_json:: value:: from_value :: < Vec < StringMap > > ( c. clone ( ) )
5451 . map_err ( |_| RenderError :: new ( "Could not decode the JSON data" ) )
5552 } ) ?;
5653
57- let base_path = rc. evaluate_absolute ( "path" ) ?
58- . as_str ( )
59- . ok_or_else ( || RenderError :: new ( "Type error for `path`, string expected" ) ) ?
60- . replace ( "\" " , "" ) ;
54+ let base_path = rc. evaluate_absolute ( "path" , true ) ?
55+ . as_str ( )
56+ . ok_or_else ( || RenderError :: new ( "Type error for `path`, string expected" ) ) ?
57+ . replace ( "\" " , "" ) ;
6158
6259 let mut previous: Option < StringMap > = None ;
6360
@@ -78,7 +75,7 @@ fn find_chapter(
7875 }
7976 }
8077
81- Ok ( None )
78+ Ok ( None )
8279}
8380
8481fn render (
@@ -91,18 +88,21 @@ fn render(
9188
9289 let mut context = BTreeMap :: new ( ) ;
9390
94- chapter. get ( "name" )
95- . ok_or_else ( || RenderError :: new ( "No title found for chapter in JSON data" ) )
96- . map ( |name| context. insert ( "title" . to_owned ( ) , json ! ( name) ) ) ?;
97-
98- chapter. get ( "path" )
99- . ok_or_else ( || RenderError :: new ( "No path found for chapter in JSON data" ) )
100- . and_then ( |p| {
101- Path :: new ( p) . with_extension ( "html" )
102- . to_str ( )
103- . ok_or_else ( || RenderError :: new ( "Link could not be converted to str" ) )
104- . map ( |p| context. insert ( "link" . to_owned ( ) , json ! ( p. replace( "\\ " , "/" ) ) ) )
105- } ) ?;
91+ chapter
92+ . get ( "name" )
93+ . ok_or_else ( || RenderError :: new ( "No title found for chapter in JSON data" ) )
94+ . map ( |name| context. insert ( "title" . to_owned ( ) , json ! ( name) ) ) ?;
95+
96+ chapter
97+ . get ( "path" )
98+ . ok_or_else ( || RenderError :: new ( "No path found for chapter in JSON data" ) )
99+ . and_then ( |p| {
100+ Path :: new ( p)
101+ . with_extension ( "html" )
102+ . to_str ( )
103+ . ok_or_else ( || RenderError :: new ( "Link could not be converted to str" ) )
104+ . map ( |p| context. insert ( "link" . to_owned ( ) , json ! ( p. replace( "\\ " , "/" ) ) ) )
105+ } ) ?;
106106
107107 trace ! ( "Render template" ) ;
108108
@@ -138,14 +138,14 @@ pub fn next(_h: &Helper, r: &Handlebars, rc: &mut RenderContext) -> Result<(), R
138138
139139#[ cfg( test) ]
140140mod tests {
141- use super :: * ;
141+ use super :: * ;
142142
143- static TEMPLATE : & ' static str =
144- "{{#previous}}{{title}}: {{link}}{{/previous}}|{{#next}}{{title}}: {{link}}{{/next}}" ;
143+ static TEMPLATE : & ' static str =
144+ "{{#previous}}{{title}}: {{link}}{{/previous}}|{{#next}}{{title}}: {{link}}{{/next}}" ;
145145
146- #[ test]
147- fn test_next_previous ( ) {
148- let data = json ! ( {
146+ #[ test]
147+ fn test_next_previous ( ) {
148+ let data = json ! ( {
149149 "name" : "two" ,
150150 "path" : "two.path" ,
151151 "chapters" : [
@@ -164,18 +164,19 @@ mod tests {
164164 ]
165165 } ) ;
166166
167- let mut h = Handlebars :: new ( ) ;
168- h. register_helper ( "previous" , Box :: new ( previous) ) ;
169- h. register_helper ( "next" , Box :: new ( next) ) ;
167+ let mut h = Handlebars :: new ( ) ;
168+ h. register_helper ( "previous" , Box :: new ( previous) ) ;
169+ h. register_helper ( "next" , Box :: new ( next) ) ;
170170
171- assert_eq ! (
172- h. template_render( TEMPLATE , & data) . unwrap( ) ,
173- "one: one.html|three: three.html" ) ;
174- }
171+ assert_eq ! (
172+ h. render_template( TEMPLATE , & data) . unwrap( ) ,
173+ "one: one.html|three: three.html"
174+ ) ;
175+ }
175176
176- #[ test]
177- fn test_first ( ) {
178- let data = json ! ( {
177+ #[ test]
178+ fn test_first ( ) {
179+ let data = json ! ( {
179180 "name" : "one" ,
180181 "path" : "one.path" ,
181182 "chapters" : [
@@ -194,17 +195,18 @@ mod tests {
194195 ]
195196 } ) ;
196197
197- let mut h = Handlebars :: new ( ) ;
198- h. register_helper ( "previous" , Box :: new ( previous) ) ;
199- h. register_helper ( "next" , Box :: new ( next) ) ;
200-
201- assert_eq ! (
202- h. template_render( TEMPLATE , & data) . unwrap( ) ,
203- "|two: two.html" ) ;
204- }
205- #[ test]
206- fn test_last ( ) {
207- let data = json ! ( {
198+ let mut h = Handlebars :: new ( ) ;
199+ h. register_helper ( "previous" , Box :: new ( previous) ) ;
200+ h. register_helper ( "next" , Box :: new ( next) ) ;
201+
202+ assert_eq ! (
203+ h. render_template( TEMPLATE , & data) . unwrap( ) ,
204+ "|two: two.html"
205+ ) ;
206+ }
207+ #[ test]
208+ fn test_last ( ) {
209+ let data = json ! ( {
208210 "name" : "three" ,
209211 "path" : "three.path" ,
210212 "chapters" : [
@@ -223,12 +225,13 @@ mod tests {
223225 ]
224226 } ) ;
225227
226- let mut h = Handlebars :: new ( ) ;
227- h. register_helper ( "previous" , Box :: new ( previous) ) ;
228- h. register_helper ( "next" , Box :: new ( next) ) ;
228+ let mut h = Handlebars :: new ( ) ;
229+ h. register_helper ( "previous" , Box :: new ( previous) ) ;
230+ h. register_helper ( "next" , Box :: new ( next) ) ;
229231
230- assert_eq ! (
231- h. template_render( TEMPLATE , & data) . unwrap( ) ,
232- "two: two.html|" ) ;
233- }
232+ assert_eq ! (
233+ h. render_template( TEMPLATE , & data) . unwrap( ) ,
234+ "two: two.html|"
235+ ) ;
236+ }
234237}
0 commit comments