Skip to content

Commit

Permalink
Switch from rustc_serialize to serde. Closes #18
Browse files Browse the repository at this point in the history
  • Loading branch information
azerupi committed Nov 3, 2016
1 parent 30eb857 commit 67aee5c
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 28 deletions.
5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ exclude = [

[dependencies]
clap = "2.2.1"
handlebars = "0.20.0"
rustc-serialize = "0.3.18"
handlebars = { version = "0.20.0", features = ["serde_type"] }
serde = "0.8.17"
serde_json = "0.8.3"
pulldown-cmark = "0.0.8"
log = "0.3"
env_logger = "0.3.4"
Expand Down
15 changes: 9 additions & 6 deletions src/book/bookconfig.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use rustc_serialize::json::Json;
use serde_json;
use serde_json::Value;
use std::fs::File;
use std::io::Read;
use std::path::{Path, PathBuf};
Expand Down Expand Up @@ -53,23 +54,25 @@ impl BookConfig {
}

// Convert to JSON
if let Ok(config) = Json::from_str(&data) {
if let Ok(config) = serde_json::from_str::<serde_json::Value>(&data) {
// Extract data

let config = config.as_object().unwrap();

debug!("[*]: Extracting data from config");
// Title, author, description
if let Some(a) = config.find_path(&["title"]) {
if let Some(a) = config.get("title") {
self.title = a.to_string().replace("\"", "")
}
if let Some(a) = config.find_path(&["author"]) {
if let Some(a) = config.get("author") {
self.author = a.to_string().replace("\"", "")
}
if let Some(a) = config.find_path(&["description"]) {
if let Some(a) = config.get("description") {
self.description = a.to_string().replace("\"", "")
}

// Destination
if let Some(a) = config.find_path(&["dest"]) {
if let Some(a) = config.get("dest") {
let dest = PathBuf::from(&a.to_string().replace("\"", ""));

// If path is relative make it absolute from the parent directory of src
Expand Down
18 changes: 7 additions & 11 deletions src/book/bookitem.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use rustc_serialize::json::{Json, ToJson};
use serde::{Serialize, Serializer};
use std::path::PathBuf;
use std::collections::BTreeMap;

Expand Down Expand Up @@ -36,16 +36,12 @@ impl Chapter {
}


impl ToJson for Chapter {
fn to_json(&self) -> Json {
let mut m: BTreeMap<String, Json> = BTreeMap::new();
m.insert("name".to_owned(), self.name.to_json());
m.insert("path".to_owned(),
self.path
.to_str()
.expect("Json conversion failed for path")
.to_json());
m.to_json()
impl Serialize for Chapter {
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error> where S: Serializer {
let mut state = try!(serializer.serialize_struct("Chapter", 2));
try!(serializer.serialize_struct_elt(&mut state, "name", self.name.clone()));
try!(serializer.serialize_struct_elt(&mut state, "path", self.path.clone()));
serializer.serialize_struct_end(state)
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@
//!
//! Make sure to take a look at it.

extern crate rustc_serialize;
extern crate serde;
extern crate serde_json;
extern crate handlebars;
extern crate pulldown_cmark;

Expand Down
9 changes: 6 additions & 3 deletions src/renderer/html_handlebars/hbs_renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ use std::io::{self, Read, Write};
use std::collections::BTreeMap;

use handlebars::Handlebars;
use rustc_serialize::json::{Json, ToJson};

use serde::Serialize;
use serde_json;
use serde_json::value::ToJson;


pub struct HtmlHandlebars;
Expand Down Expand Up @@ -276,10 +279,10 @@ impl Renderer for HtmlHandlebars {
}
}

fn make_data(book: &MDBook) -> Result<BTreeMap<String, Json>, Box<Error>> {
fn make_data(book: &MDBook) -> Result<serde_json::Map<String, serde_json::Value>, Box<Error>> {
debug!("[fn]: make_data");

let mut data = BTreeMap::new();
let mut data = serde_json::Map::new();
data.insert("language".to_owned(), "en".to_json());
data.insert("title".to_owned(), book.get_title().to_json());
data.insert("description".to_owned(), book.get_description().to_json());
Expand Down
7 changes: 4 additions & 3 deletions src/renderer/html_handlebars/helpers/navigation.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use std::path::Path;
use std::collections::BTreeMap;

use rustc_serialize::json::{self, ToJson};
use serde_json;
use serde_json::value::ToJson;
use handlebars::{Handlebars, RenderError, RenderContext, Helper, Context, Renderable};

// Handlebars helper for navigation
Expand All @@ -22,7 +23,7 @@ pub fn previous(c: &Context, _h: &Helper, r: &Handlebars, rc: &mut RenderContext

debug!("[*]: Decode chapters from JSON");
// Decode json format
let decoded: Vec<BTreeMap<String, String>> = match json::decode(&chapters.to_string()) {
let decoded: Vec<BTreeMap<String, String>> = match serde_json::from_str(&chapters.to_string()) {
Ok(data) => data,
Err(_) => return Err(RenderError::new("Could not decode the JSON data")),
};
Expand Down Expand Up @@ -121,7 +122,7 @@ pub fn next(c: &Context, _h: &Helper, r: &Handlebars, rc: &mut RenderContext) ->

debug!("[*]: Decode chapters from JSON");
// Decode json format
let decoded: Vec<BTreeMap<String, String>> = match json::decode(&chapters.to_string()) {
let decoded: Vec<BTreeMap<String, String>> = match serde_json::from_str(&chapters.to_string()) {
Ok(data) => data,
Err(_) => return Err(RenderError::new("Could not decode the JSON data")),
};
Expand Down
4 changes: 2 additions & 2 deletions src/renderer/html_handlebars/helpers/toc.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::path::Path;
use std::collections::BTreeMap;

use rustc_serialize::json;
use serde_json;
use handlebars::{Handlebars, HelperDef, RenderError, RenderContext, Helper, Context};
use pulldown_cmark::{Parser, html, Event, Tag};

Expand All @@ -20,7 +20,7 @@ impl HelperDef for RenderToc {
try!(rc.writer.write("<ul class=\"chapter\">".as_bytes()));

// Decode json format
let decoded: Vec<BTreeMap<String, String>> = json::decode(&chapters.to_string()).unwrap();
let decoded: Vec<BTreeMap<String, String>> = serde_json::from_str(&chapters.to_string()).unwrap();

let mut current_level = 1;

Expand Down

0 comments on commit 67aee5c

Please sign in to comment.