Added error-chain to the renderer module
This commit is contained in:
parent
1356e0f068
commit
487f5ce339
|
@ -99,6 +99,9 @@ pub mod errors {
|
||||||
error_chain!{
|
error_chain!{
|
||||||
foreign_links {
|
foreign_links {
|
||||||
Io(::std::io::Error);
|
Io(::std::io::Error);
|
||||||
|
HandlebarsRender(::handlebars::RenderError);
|
||||||
|
HandlebarsTemplate(::handlebars::TemplateError);
|
||||||
|
Utf8(::std::string::FromUtf8Error);
|
||||||
}
|
}
|
||||||
|
|
||||||
errors {
|
errors {
|
||||||
|
|
|
@ -4,12 +4,12 @@ use book::MDBook;
|
||||||
use book::bookitem::{BookItem, Chapter};
|
use book::bookitem::{BookItem, Chapter};
|
||||||
use utils;
|
use utils;
|
||||||
use theme::{self, Theme};
|
use theme::{self, Theme};
|
||||||
|
use errors::*;
|
||||||
use regex::{Regex, Captures};
|
use regex::{Regex, Captures};
|
||||||
|
|
||||||
use std::ascii::AsciiExt;
|
use std::ascii::AsciiExt;
|
||||||
use std::path::{Path, PathBuf};
|
use std::path::{Path, PathBuf};
|
||||||
use std::fs::{self, File};
|
use std::fs::{self, File};
|
||||||
use std::error::Error;
|
|
||||||
use std::io::{self, Read};
|
use std::io::{self, Read};
|
||||||
use std::collections::BTreeMap;
|
use std::collections::BTreeMap;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
@ -28,7 +28,7 @@ impl HtmlHandlebars {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn render_item(&self, item: &BookItem, mut ctx: RenderItemContext, print_content: &mut String)
|
fn render_item(&self, item: &BookItem, mut ctx: RenderItemContext, print_content: &mut String)
|
||||||
-> Result<(), Box<Error>> {
|
-> Result<()> {
|
||||||
// FIXME: This should be made DRY-er and rely less on mutable state
|
// FIXME: This should be made DRY-er and rely less on mutable state
|
||||||
match *item {
|
match *item {
|
||||||
BookItem::Chapter(_, ref ch) |
|
BookItem::Chapter(_, ref ch) |
|
||||||
|
@ -88,7 +88,7 @@ impl HtmlHandlebars {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Create an index.html from the first element in SUMMARY.md
|
/// Create an index.html from the first element in SUMMARY.md
|
||||||
fn render_index(&self, book: &MDBook, ch: &Chapter, destination: &Path) -> Result<(), Box<Error>> {
|
fn render_index(&self, book: &MDBook, ch: &Chapter, destination: &Path) -> Result<()> {
|
||||||
debug!("[*]: index.html");
|
debug!("[*]: index.html");
|
||||||
|
|
||||||
let mut content = String::new();
|
let mut content = String::new();
|
||||||
|
@ -129,7 +129,7 @@ impl HtmlHandlebars {
|
||||||
rendered
|
rendered
|
||||||
}
|
}
|
||||||
|
|
||||||
fn copy_static_files(&self, book: &MDBook, theme: &Theme) -> Result<(), Box<Error>> {
|
fn copy_static_files(&self, book: &MDBook, theme: &Theme) -> Result<()> {
|
||||||
book.write_file("book.js", &theme.js)?;
|
book.write_file("book.js", &theme.js)?;
|
||||||
book.write_file("book.css", &theme.css)?;
|
book.write_file("book.css", &theme.css)?;
|
||||||
book.write_file("favicon.png", &theme.favicon)?;
|
book.write_file("favicon.png", &theme.favicon)?;
|
||||||
|
@ -180,7 +180,7 @@ impl HtmlHandlebars {
|
||||||
|
|
||||||
/// Helper function to write a file to the build directory, normalizing
|
/// Helper function to write a file to the build directory, normalizing
|
||||||
/// the path to be relative to the book root.
|
/// the path to be relative to the book root.
|
||||||
fn write_custom_file(&self, custom_file: &Path, book: &MDBook) -> Result<(), Box<Error>> {
|
fn write_custom_file(&self, custom_file: &Path, book: &MDBook) -> Result<()> {
|
||||||
let mut data = Vec::new();
|
let mut data = Vec::new();
|
||||||
let mut f = File::open(custom_file)?;
|
let mut f = File::open(custom_file)?;
|
||||||
f.read_to_end(&mut data)?;
|
f.read_to_end(&mut data)?;
|
||||||
|
@ -216,7 +216,7 @@ impl HtmlHandlebars {
|
||||||
|
|
||||||
/// Copy across any additional CSS and JavaScript files which the book
|
/// Copy across any additional CSS and JavaScript files which the book
|
||||||
/// has been configured to use.
|
/// has been configured to use.
|
||||||
fn copy_additional_css_and_js(&self, book: &MDBook) -> Result<(), Box<Error>> {
|
fn copy_additional_css_and_js(&self, book: &MDBook) -> Result<()> {
|
||||||
let custom_files = book.get_additional_css().iter().chain(
|
let custom_files = book.get_additional_css().iter().chain(
|
||||||
book.get_additional_js()
|
book.get_additional_js()
|
||||||
.iter(),
|
.iter(),
|
||||||
|
@ -232,7 +232,7 @@ impl HtmlHandlebars {
|
||||||
|
|
||||||
|
|
||||||
impl Renderer for HtmlHandlebars {
|
impl Renderer for HtmlHandlebars {
|
||||||
fn render(&self, book: &MDBook) -> Result<(), Box<Error>> {
|
fn render(&self, book: &MDBook) -> Result<()> {
|
||||||
debug!("[fn]: render");
|
debug!("[fn]: render");
|
||||||
let mut handlebars = Handlebars::new();
|
let mut handlebars = Handlebars::new();
|
||||||
|
|
||||||
|
@ -258,9 +258,7 @@ impl Renderer for HtmlHandlebars {
|
||||||
|
|
||||||
debug!("[*]: Check if destination directory exists");
|
debug!("[*]: Check if destination directory exists");
|
||||||
if fs::create_dir_all(&destination).is_err() {
|
if fs::create_dir_all(&destination).is_err() {
|
||||||
return Err(Box::new(
|
bail!("Unexpected error when constructing destination path");
|
||||||
io::Error::new(io::ErrorKind::Other, "Unexpected error when constructing destination path"),
|
|
||||||
));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for (i, item) in book.iter().enumerate() {
|
for (i, item) in book.iter().enumerate() {
|
||||||
|
@ -301,7 +299,7 @@ impl Renderer for HtmlHandlebars {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn make_data(book: &MDBook) -> Result<serde_json::Map<String, serde_json::Value>, Box<Error>> {
|
fn make_data(book: &MDBook) -> Result<serde_json::Map<String, serde_json::Value>> {
|
||||||
debug!("[fn]: make_data");
|
debug!("[fn]: make_data");
|
||||||
|
|
||||||
let mut data = serde_json::Map::new();
|
let mut data = serde_json::Map::new();
|
||||||
|
|
|
@ -2,8 +2,8 @@ pub use self::html_handlebars::HtmlHandlebars;
|
||||||
|
|
||||||
mod html_handlebars;
|
mod html_handlebars;
|
||||||
|
|
||||||
use std::error::Error;
|
use errors::*;
|
||||||
|
|
||||||
pub trait Renderer {
|
pub trait Renderer {
|
||||||
fn render(&self, book: &::book::MDBook) -> Result<(), Box<Error>>;
|
fn render(&self, book: &::book::MDBook) -> Result<()>;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue