Skip to content

Commit 152ebba

Browse files
committed
Bump serde, serde_json, and handlebars crates.
1 parent 23d25c8 commit 152ebba

File tree

7 files changed

+53
-51
lines changed

7 files changed

+53
-51
lines changed

Cargo.toml

+3-3
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ exclude = [
1616

1717
[dependencies]
1818
clap = "2.19.2"
19-
handlebars = { version = "0.23.0", features = ["serde_type"] }
20-
serde = "0.8"
21-
serde_json = "0.8"
19+
handlebars = { version = "0.25.0", features = ["serde_type"] }
20+
serde = "0.9"
21+
serde_json = "0.9"
2222
pulldown-cmark = "0.0.8"
2323
log = "0.3"
2424
env_logger = "0.3"

src/book/bookconfig.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -214,9 +214,9 @@ pub fn json_value_to_toml_value(json: serde_json::Value) -> toml::Value {
214214
match json {
215215
serde_json::Value::Null => toml::Value::String("".to_string()),
216216
serde_json::Value::Bool(x) => toml::Value::Boolean(x),
217-
serde_json::Value::I64(x) => toml::Value::Integer(x),
218-
serde_json::Value::U64(x) => toml::Value::Integer(x as i64),
219-
serde_json::Value::F64(x) => toml::Value::Float(x),
217+
serde_json::Value::Number(ref x) if x.is_i64() => toml::Value::Integer(x.as_i64().unwrap()),
218+
serde_json::Value::Number(ref x) if x.is_u64() => toml::Value::Integer(x.as_i64().unwrap()),
219+
serde_json::Value::Number(x) => toml::Value::Float(x.as_f64().unwrap()),
220220
serde_json::Value::String(x) => toml::Value::String(x),
221221
serde_json::Value::Array(x) => {
222222
toml::Value::Array(x.iter().map(|v| json_value_to_toml_value(v.to_owned())).collect())

src/book/bookitem.rs

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use serde::{Serialize, Serializer};
2+
use serde::ser::SerializeStruct;
23
use std::path::PathBuf;
34

45
#[derive(Debug, Clone)]
@@ -36,11 +37,11 @@ impl Chapter {
3637

3738

3839
impl Serialize for Chapter {
39-
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error> where S: Serializer {
40-
let mut state = try!(serializer.serialize_struct("Chapter", 2));
41-
try!(serializer.serialize_struct_elt(&mut state, "name", self.name.clone()));
42-
try!(serializer.serialize_struct_elt(&mut state, "path", self.path.clone()));
43-
serializer.serialize_struct_end(state)
40+
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> where S: Serializer {
41+
let mut struct_ = try!(serializer.serialize_struct("Chapter", 2));
42+
try!(struct_.serialize_field("name", &self.name));
43+
try!(struct_.serialize_field("path", &self.path));
44+
struct_.end()
4445
}
4546
}
4647

src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@
7070
//! Make sure to take a look at it.
7171
7272
extern crate serde;
73+
#[macro_use]
7374
extern crate serde_json;
7475
extern crate handlebars;
7576
extern crate pulldown_cmark;

src/renderer/html_handlebars/hbs_renderer.rs

+19-20
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ use std::collections::BTreeMap;
1313
use handlebars::Handlebars;
1414

1515
use serde_json;
16-
use serde_json::value::ToJson;
1716

1817

1918
pub struct HtmlHandlebars;
@@ -84,10 +83,10 @@ impl Renderer for HtmlHandlebars {
8483
// Update the context with data for this file
8584
let path = ch.path.to_str().ok_or(io::Error::new(io::ErrorKind::Other,
8685
"Could not convert path to str"))?;
87-
data.insert("path".to_owned(), path.to_json());
88-
data.insert("content".to_owned(), content.to_json());
89-
data.insert("chapter_title".to_owned(), ch.name.to_json());
90-
data.insert("path_to_root".to_owned(), utils::fs::path_to_root(&ch.path).to_json());
86+
data.insert("path".to_owned(), json!(path));
87+
data.insert("content".to_owned(), json!(content));
88+
data.insert("chapter_title".to_owned(), json!(ch.name));
89+
data.insert("path_to_root".to_owned(), json!(utils::fs::path_to_root(&ch.path)));
9190

9291
// Render the handlebars template with the data
9392
debug!("[*]: Render template");
@@ -128,9 +127,9 @@ impl Renderer for HtmlHandlebars {
128127
// Print version
129128

130129
// Update the context with data for this file
131-
data.insert("path".to_owned(), "print.md".to_json());
132-
data.insert("content".to_owned(), print_content.to_json());
133-
data.insert("path_to_root".to_owned(), utils::fs::path_to_root(Path::new("print.md")).to_json());
130+
data.insert("path".to_owned(), json!("print.md"));
131+
data.insert("content".to_owned(), json!(print_content));
132+
data.insert("path_to_root".to_owned(), json!(utils::fs::path_to_root(Path::new("print.md"))));
134133

135134
// Render the handlebars template with the data
136135
debug!("[*]: Render template");
@@ -167,12 +166,12 @@ fn make_data(book: &MDBook) -> Result<serde_json::Map<String, serde_json::Value>
167166
debug!("[fn]: make_data");
168167

169168
let mut data = serde_json::Map::new();
170-
data.insert("language".to_owned(), "en".to_json());
171-
data.insert("title".to_owned(), book.get_title().to_json());
172-
data.insert("description".to_owned(), book.get_description().to_json());
173-
data.insert("favicon".to_owned(), "favicon.png".to_json());
169+
data.insert("language".to_owned(), json!("en"));
170+
data.insert("title".to_owned(), json!(book.get_title()));
171+
data.insert("description".to_owned(), json!(book.get_description()));
172+
data.insert("favicon".to_owned(), json!("favicon.png"));
174173
if let Some(livereload) = book.get_livereload() {
175-
data.insert("livereload".to_owned(), livereload.to_json());
174+
data.insert("livereload".to_owned(), json!(livereload));
176175
}
177176

178177
let mut chapters = vec![];
@@ -183,28 +182,28 @@ fn make_data(book: &MDBook) -> Result<serde_json::Map<String, serde_json::Value>
183182

184183
match *item {
185184
BookItem::Affix(ref ch) => {
186-
chapter.insert("name".to_owned(), ch.name.to_json());
185+
chapter.insert("name".to_owned(), json!(ch.name));
187186
let path = ch.path.to_str().ok_or(io::Error::new(io::ErrorKind::Other,
188187
"Could not convert path to str"))?;
189-
chapter.insert("path".to_owned(), path.to_json());
188+
chapter.insert("path".to_owned(), json!(path));
190189
},
191190
BookItem::Chapter(ref s, ref ch) => {
192-
chapter.insert("section".to_owned(), s.to_json());
193-
chapter.insert("name".to_owned(), ch.name.to_json());
191+
chapter.insert("section".to_owned(), json!(s));
192+
chapter.insert("name".to_owned(), json!(ch.name));
194193
let path = ch.path.to_str().ok_or(io::Error::new(io::ErrorKind::Other,
195194
"Could not convert path to str"))?;
196-
chapter.insert("path".to_owned(), path.to_json());
195+
chapter.insert("path".to_owned(), json!(path));
197196
},
198197
BookItem::Spacer => {
199-
chapter.insert("spacer".to_owned(), "_spacer_".to_json());
198+
chapter.insert("spacer".to_owned(), json!("_spacer_"));
200199
},
201200

202201
}
203202

204203
chapters.push(chapter);
205204
}
206205

207-
data.insert("chapters".to_owned(), chapters.to_json());
206+
data.insert("chapters".to_owned(), json!(chapters));
208207

209208
debug!("[*]: JSON constructed");
210209
Ok(data)

src/renderer/html_handlebars/helpers/navigation.rs

+17-16
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,21 @@ use std::path::Path;
22
use std::collections::{VecDeque, BTreeMap};
33

44
use serde_json;
5-
use serde_json::value::ToJson;
6-
use handlebars::{Handlebars, RenderError, RenderContext, Helper, Context, Renderable};
5+
use handlebars::{Handlebars, RenderError, RenderContext, Helper, Renderable};
76

87

98
// Handlebars helper for navigation
109

11-
pub fn previous(c: &Context, _h: &Helper, r: &Handlebars, rc: &mut RenderContext) -> Result<(), RenderError> {
10+
pub fn previous(_h: &Helper, r: &Handlebars, rc: &mut RenderContext) -> Result<(), RenderError> {
1211
debug!("[fn]: previous (handlebars helper)");
1312

1413
debug!("[*]: Get data from context");
1514
// get value from context data
1615
// rc.get_path() is current json parent path, you should always use it like this
1716
// param is the key of value you want to display
18-
let chapters = c.navigate(rc.get_path(), &VecDeque::new(), "chapters");
17+
let chapters = rc.context().navigate(rc.get_path(), &VecDeque::new(), "chapters").to_owned();
1918

20-
let current = c.navigate(rc.get_path(), &VecDeque::new(), "path")
19+
let current = rc.context().navigate(rc.get_path(), &VecDeque::new(), "path")
2120
.to_string()
2221
.replace("\"", "");
2322

@@ -50,7 +49,7 @@ pub fn previous(c: &Context, _h: &Helper, r: &Handlebars, rc: &mut RenderContext
5049
match previous.get("name") {
5150
Some(n) => {
5251
debug!("[*]: Inserting title: {}", n);
53-
previous_chapter.insert("title".to_owned(), n.to_json())
52+
previous_chapter.insert("title".to_owned(), json!(n))
5453
},
5554
None => {
5655
debug!("[*]: No title found for chapter");
@@ -68,7 +67,7 @@ pub fn previous(c: &Context, _h: &Helper, r: &Handlebars, rc: &mut RenderContext
6867

6968
match path.to_str() {
7069
Some(p) => {
71-
previous_chapter.insert("link".to_owned(), p.replace("\\", "/").to_json());
70+
previous_chapter.insert("link".to_owned(), json!(p.replace("\\", "/")));
7271
},
7372
None => return Err(RenderError::new("Link could not be converted to str")),
7473
}
@@ -78,13 +77,14 @@ pub fn previous(c: &Context, _h: &Helper, r: &Handlebars, rc: &mut RenderContext
7877

7978
debug!("[*]: Inject in context");
8079
// Inject in current context
81-
let updated_context = c.extend(&previous_chapter);
80+
let updated_context = rc.context().extend(&previous_chapter);
8281

8382
debug!("[*]: Render template");
8483
// Render template
8584
match _h.template() {
8685
Some(t) => {
87-
try!(t.render(&updated_context, r, rc));
86+
*rc.context_mut() = updated_context;
87+
try!(t.render(r, rc));
8888
},
8989
None => return Err(RenderError::new("Error with the handlebars template")),
9090
}
@@ -108,16 +108,16 @@ pub fn previous(c: &Context, _h: &Helper, r: &Handlebars, rc: &mut RenderContext
108108

109109

110110

111-
pub fn next(c: &Context, _h: &Helper, r: &Handlebars, rc: &mut RenderContext) -> Result<(), RenderError> {
111+
pub fn next(_h: &Helper, r: &Handlebars, rc: &mut RenderContext) -> Result<(), RenderError> {
112112
debug!("[fn]: next (handlebars helper)");
113113

114114
debug!("[*]: Get data from context");
115115
// get value from context data
116116
// rc.get_path() is current json parent path, you should always use it like this
117117
// param is the key of value you want to display
118-
let chapters = c.navigate(rc.get_path(), &VecDeque::new(), "chapters");
118+
let chapters = rc.context().navigate(rc.get_path(), &VecDeque::new(), "chapters").to_owned();
119119

120-
let current = c.navigate(rc.get_path(), &VecDeque::new(), "path")
120+
let current = rc.context().navigate(rc.get_path(), &VecDeque::new(), "path")
121121
.to_string()
122122
.replace("\"", "");
123123

@@ -154,7 +154,7 @@ pub fn next(c: &Context, _h: &Helper, r: &Handlebars, rc: &mut RenderContext) ->
154154
match item.get("name") {
155155
Some(n) => {
156156
debug!("[*]: Inserting title: {}", n);
157-
next_chapter.insert("title".to_owned(), n.to_json());
157+
next_chapter.insert("title".to_owned(), json!(n));
158158
},
159159
None => return Err(RenderError::new("No title found for chapter in JSON data")),
160160
}
@@ -166,21 +166,22 @@ pub fn next(c: &Context, _h: &Helper, r: &Handlebars, rc: &mut RenderContext) ->
166166
match link.to_str() {
167167
Some(l) => {
168168
// Hack for windows who tends to use `\` as separator instead of `/`
169-
next_chapter.insert("link".to_owned(), l.replace("\\", "/").to_json());
169+
next_chapter.insert("link".to_owned(), json!(l.replace("\\", "/")));
170170
},
171171
None => return Err(RenderError::new("Link could not converted to str")),
172172
}
173173

174174
debug!("[*]: Inject in context");
175175
// Inject in current context
176-
let updated_context = c.extend(&next_chapter);
176+
let updated_context = rc.context().extend(&next_chapter);
177177

178178
debug!("[*]: Render template");
179179

180180
// Render template
181181
match _h.template() {
182182
Some(t) => {
183-
try!(t.render(&updated_context, r, rc));
183+
*rc.context_mut() = updated_context;
184+
try!(t.render(r, rc));
184185
},
185186
None => return Err(RenderError::new("Error with the handlebars template")),
186187
}

src/renderer/html_handlebars/helpers/toc.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,21 @@ use std::path::Path;
22
use std::collections::{VecDeque, BTreeMap};
33

44
use serde_json;
5-
use handlebars::{Handlebars, HelperDef, RenderError, RenderContext, Helper, Context};
5+
use handlebars::{Handlebars, HelperDef, RenderError, RenderContext, Helper};
66
use pulldown_cmark::{Parser, html, Event, Tag};
77

88
// Handlebars helper to construct TOC
99
#[derive(Clone, Copy)]
1010
pub struct RenderToc;
1111

1212
impl HelperDef for RenderToc {
13-
fn call(&self, c: &Context, _h: &Helper, _: &Handlebars, rc: &mut RenderContext) -> Result<(), RenderError> {
13+
fn call(&self, _h: &Helper, _: &Handlebars, rc: &mut RenderContext) -> Result<(), RenderError> {
1414

1515
// get value from context data
1616
// rc.get_path() is current json parent path, you should always use it like this
1717
// param is the key of value you want to display
18-
let chapters = c.navigate(rc.get_path(), &VecDeque::new(), "chapters");
19-
let current = c.navigate(rc.get_path(), &VecDeque::new(), "path").to_string().replace("\"", "");
18+
let chapters = rc.context().navigate(rc.get_path(), &VecDeque::new(), "chapters").to_owned();
19+
let current = rc.context().navigate(rc.get_path(), &VecDeque::new(), "path").to_string().replace("\"", "");
2020
try!(rc.writer.write("<ul class=\"chapter\">".as_bytes()));
2121

2222
// Decode json format

0 commit comments

Comments
 (0)