11use std:: collections:: BTreeMap ;
22use std:: path:: Path ;
33
4- use handlebars:: { Context , Handlebars , Helper , Output , RenderContext , RenderError , Renderable } ;
4+ use handlebars:: {
5+ Context , Handlebars , Helper , Output , RenderContext , RenderError , RenderErrorReason , Renderable ,
6+ } ;
57
68use crate :: utils;
79use log:: { debug, trace} ;
@@ -26,9 +28,9 @@ impl Target {
2628 ) -> Result < Option < StringMap > , RenderError > {
2729 match * self {
2830 Target :: Next => {
29- let previous_path = previous_item
30- . get ( " path" )
31- . ok_or_else ( || RenderError :: new ( "No path found for chapter in JSON data" ) ) ?;
31+ let previous_path = previous_item. get ( "path" ) . ok_or_else ( || {
32+ RenderErrorReason :: Other ( "No path found for chapter in JSON data" . to_owned ( ) )
33+ } ) ?;
3234
3335 if previous_path == base_path {
3436 return Ok ( Some ( current_item. clone ( ) ) ) ;
@@ -54,15 +56,18 @@ fn find_chapter(
5456 debug ! ( "Get data from context" ) ;
5557
5658 let chapters = rc. evaluate ( ctx, "@root/chapters" ) . and_then ( |c| {
57- serde_json:: value:: from_value :: < Vec < StringMap > > ( c. as_json ( ) . clone ( ) )
58- . map_err ( |_| RenderError :: new ( "Could not decode the JSON data" ) )
59+ serde_json:: value:: from_value :: < Vec < StringMap > > ( c. as_json ( ) . clone ( ) ) . map_err ( |_| {
60+ RenderErrorReason :: Other ( "Could not decode the JSON data" . to_owned ( ) ) . into ( )
61+ } )
5962 } ) ?;
6063
6164 let base_path = rc
6265 . evaluate ( ctx, "@root/path" ) ?
6366 . as_json ( )
6467 . as_str ( )
65- . ok_or_else ( || RenderError :: new ( "Type error for `path`, string expected" ) ) ?
68+ . ok_or_else ( || {
69+ RenderErrorReason :: Other ( "Type error for `path`, string expected" . to_owned ( ) )
70+ } ) ?
6671 . replace ( '\"' , "" ) ;
6772
6873 if !rc. evaluate ( ctx, "@root/is_index" ) ?. is_missing ( ) {
@@ -108,7 +113,7 @@ fn find_chapter(
108113}
109114
110115fn render (
111- _h : & Helper < ' _ , ' _ > ,
116+ _h : & Helper < ' _ > ,
112117 r : & Handlebars < ' _ > ,
113118 ctx : & Context ,
114119 rc : & mut RenderContext < ' _ , ' _ > ,
@@ -122,7 +127,9 @@ fn render(
122127 . evaluate ( ctx, "@root/path" ) ?
123128 . as_json ( )
124129 . as_str ( )
125- . ok_or_else ( || RenderError :: new ( "Type error for `path`, string expected" ) ) ?
130+ . ok_or_else ( || {
131+ RenderErrorReason :: Other ( "Type error for `path`, string expected" . to_owned ( ) )
132+ } ) ?
126133 . replace ( '\"' , "" ) ;
127134
128135 context. insert (
@@ -132,32 +139,38 @@ fn render(
132139
133140 chapter
134141 . get ( "name" )
135- . ok_or_else ( || RenderError :: new ( "No title found for chapter in JSON data" ) )
142+ . ok_or_else ( || {
143+ RenderErrorReason :: Other ( "No title found for chapter in JSON data" . to_owned ( ) )
144+ } )
136145 . map ( |name| context. insert ( "title" . to_owned ( ) , json ! ( name) ) ) ?;
137146
138147 chapter
139148 . get ( "path" )
140- . ok_or_else ( || RenderError :: new ( "No path found for chapter in JSON data" ) )
149+ . ok_or_else ( || {
150+ RenderErrorReason :: Other ( "No path found for chapter in JSON data" . to_owned ( ) )
151+ } )
141152 . and_then ( |p| {
142153 Path :: new ( p)
143154 . with_extension ( "html" )
144155 . to_str ( )
145- . ok_or_else ( || RenderError :: new ( "Link could not be converted to str" ) )
156+ . ok_or_else ( || {
157+ RenderErrorReason :: Other ( "Link could not be converted to str" . to_owned ( ) )
158+ } )
146159 . map ( |p| context. insert ( "link" . to_owned ( ) , json ! ( p. replace( '\\' , "/" ) ) ) )
147160 } ) ?;
148161
149162 trace ! ( "Render template" ) ;
150163
151164 let t = _h
152165 . template ( )
153- . ok_or_else ( || RenderError :: new ( "Error with the handlebars template" ) ) ?;
166+ . ok_or_else ( || RenderErrorReason :: Other ( "Error with the handlebars template" . to_owned ( ) ) ) ?;
154167 let local_ctx = Context :: wraps ( & context) ?;
155168 let mut local_rc = rc. clone ( ) ;
156169 t. render ( r, & local_ctx, & mut local_rc, out)
157170}
158171
159172pub fn previous (
160- _h : & Helper < ' _ , ' _ > ,
173+ _h : & Helper < ' _ > ,
161174 r : & Handlebars < ' _ > ,
162175 ctx : & Context ,
163176 rc : & mut RenderContext < ' _ , ' _ > ,
@@ -173,7 +186,7 @@ pub fn previous(
173186}
174187
175188pub fn next (
176- _h : & Helper < ' _ , ' _ > ,
189+ _h : & Helper < ' _ > ,
177190 r : & Handlebars < ' _ > ,
178191 ctx : & Context ,
179192 rc : & mut RenderContext < ' _ , ' _ > ,
0 commit comments