Skip to content

Switch from rustc_serialize to serde #177

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 3, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
14 changes: 8 additions & 6 deletions src/book/bookconfig.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use rustc_serialize::json::Json;
use serde_json;
use std::fs::File;
use std::io::Read;
use std::path::{Path, PathBuf};
Expand Down Expand Up @@ -53,23 +53,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
19 changes: 7 additions & 12 deletions src/book/bookitem.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use rustc_serialize::json::{Json, ToJson};
use serde::{Serialize, Serializer};
use std::path::PathBuf;
use std::collections::BTreeMap;

#[derive(Debug, Clone)]
pub enum BookItem {
Expand Down Expand Up @@ -36,16 +35,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
8 changes: 5 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,9 @@ use std::io::{self, Read, Write};
use std::collections::BTreeMap;

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

use serde_json;
use serde_json::value::ToJson;


pub struct HtmlHandlebars;
Expand Down Expand Up @@ -276,10 +278,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