1
1
use std:: collections:: BTreeMap ;
2
2
use std:: path:: Path ;
3
3
4
- use handlebars:: { Context , Handlebars , Helper , Output , RenderContext , RenderError , Renderable } ;
4
+ use handlebars:: {
5
+ Context , Handlebars , Helper , Output , RenderContext , RenderError , RenderErrorReason , Renderable ,
6
+ } ;
5
7
6
8
use crate :: utils;
7
9
use log:: { debug, trace} ;
@@ -26,9 +28,9 @@ impl Target {
26
28
) -> Result < Option < StringMap > , RenderError > {
27
29
match * self {
28
30
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
+ } ) ?;
32
34
33
35
if previous_path == base_path {
34
36
return Ok ( Some ( current_item. clone ( ) ) ) ;
@@ -54,15 +56,18 @@ fn find_chapter(
54
56
debug ! ( "Get data from context" ) ;
55
57
56
58
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
+ } )
59
62
} ) ?;
60
63
61
64
let base_path = rc
62
65
. evaluate ( ctx, "@root/path" ) ?
63
66
. as_json ( )
64
67
. 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
+ } ) ?
66
71
. replace ( '\"' , "" ) ;
67
72
68
73
if !rc. evaluate ( ctx, "@root/is_index" ) ?. is_missing ( ) {
@@ -108,7 +113,7 @@ fn find_chapter(
108
113
}
109
114
110
115
fn render (
111
- _h : & Helper < ' _ , ' _ > ,
116
+ _h : & Helper < ' _ > ,
112
117
r : & Handlebars < ' _ > ,
113
118
ctx : & Context ,
114
119
rc : & mut RenderContext < ' _ , ' _ > ,
@@ -122,7 +127,9 @@ fn render(
122
127
. evaluate ( ctx, "@root/path" ) ?
123
128
. as_json ( )
124
129
. 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
+ } ) ?
126
133
. replace ( '\"' , "" ) ;
127
134
128
135
context. insert (
@@ -132,32 +139,38 @@ fn render(
132
139
133
140
chapter
134
141
. 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
+ } )
136
145
. map ( |name| context. insert ( "title" . to_owned ( ) , json ! ( name) ) ) ?;
137
146
138
147
chapter
139
148
. 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
+ } )
141
152
. and_then ( |p| {
142
153
Path :: new ( p)
143
154
. with_extension ( "html" )
144
155
. 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
+ } )
146
159
. map ( |p| context. insert ( "link" . to_owned ( ) , json ! ( p. replace( '\\' , "/" ) ) ) )
147
160
} ) ?;
148
161
149
162
trace ! ( "Render template" ) ;
150
163
151
164
let t = _h
152
165
. 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 ( ) ) ) ?;
154
167
let local_ctx = Context :: wraps ( & context) ?;
155
168
let mut local_rc = rc. clone ( ) ;
156
169
t. render ( r, & local_ctx, & mut local_rc, out)
157
170
}
158
171
159
172
pub fn previous (
160
- _h : & Helper < ' _ , ' _ > ,
173
+ _h : & Helper < ' _ > ,
161
174
r : & Handlebars < ' _ > ,
162
175
ctx : & Context ,
163
176
rc : & mut RenderContext < ' _ , ' _ > ,
@@ -173,7 +186,7 @@ pub fn previous(
173
186
}
174
187
175
188
pub fn next (
176
- _h : & Helper < ' _ , ' _ > ,
189
+ _h : & Helper < ' _ > ,
177
190
r : & Handlebars < ' _ > ,
178
191
ctx : & Context ,
179
192
rc : & mut RenderContext < ' _ , ' _ > ,
0 commit comments