Skip to content

Commit 0932bfd

Browse files
committed
Big clean-up, reduced the unwrap's to 2 in the library part! Closes #36 The unwraps where replaced with more diverse error messages #14
1 parent b6034e8 commit 0932bfd

File tree

6 files changed

+166
-75
lines changed

6 files changed

+166
-75
lines changed

book-example/src/cli/init.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ by appending a path to the command:
3636
mdbook init path/to/book
3737
```
3838

39-
#### --theme
39+
## --theme
4040

4141
When you use the `--theme` argument, the default theme will be copied into a directory
4242
called `theme` in your source directory so that you can modify it.

src/book/bookconfig.rs

Lines changed: 39 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use std::path::{Path, PathBuf};
88
pub struct BookConfig {
99
pub title: String,
1010
pub author: String,
11+
root: PathBuf,
1112
dest: PathBuf,
1213
src: PathBuf,
1314
pub indent_spaces: i32,
@@ -16,10 +17,11 @@ pub struct BookConfig {
1617

1718

1819
impl BookConfig {
19-
pub fn new() -> Self {
20+
pub fn new(root: &Path) -> Self {
2021
BookConfig {
2122
title: String::new(),
2223
author: String::new(),
24+
root: root.to_owned(),
2325
dest: PathBuf::from("book"),
2426
src: PathBuf::from("src"),
2527
indent_spaces: 4, // indentation used for SUMMARY.md
@@ -42,32 +44,50 @@ impl BookConfig {
4244

4345
debug!("[*]: Reading config");
4446
let mut data = String::new();
45-
config_file.read_to_string(&mut data).unwrap();
4647

47-
// Convert to JSON
48-
let config = Json::from_str(&data).unwrap();
49-
50-
// Extract data
51-
52-
debug!("[*]: Extracting data from config");
53-
// Title & author
54-
if let Some(a) = config.find_path(&["title"]) { self.title = a.to_string().replace("\"", "") }
55-
if let Some(a) = config.find_path(&["author"]) { self.author = a.to_string().replace("\"", "") }
56-
57-
// Destination
58-
if let Some(a) = config.find_path(&["dest"]) {
59-
let dest = PathBuf::from(&a.to_string().replace("\"", ""));
48+
// Just return if an error occured.
49+
// I would like to propagate the error, but I have to return `&self`
50+
match config_file.read_to_string(&mut data) {
51+
Err(_) => return self,
52+
_ => {},
53+
}
6054

61-
// If path is relative make it absolute from the parent directory of src
62-
if dest.is_relative() {
63-
let dest = &self.get_src().parent().unwrap().join(&dest);
64-
self.set_dest(dest);
55+
// Convert to JSON
56+
if let Ok(config) = Json::from_str(&data) {
57+
// Extract data
58+
59+
debug!("[*]: Extracting data from config");
60+
// Title & author
61+
if let Some(a) = config.find_path(&["title"]) { self.title = a.to_string().replace("\"", "") }
62+
if let Some(a) = config.find_path(&["author"]) { self.author = a.to_string().replace("\"", "") }
63+
64+
// Destination
65+
if let Some(a) = config.find_path(&["dest"]) {
66+
let dest = PathBuf::from(&a.to_string().replace("\"", ""));
67+
68+
// If path is relative make it absolute from the parent directory of src
69+
match dest.is_relative() {
70+
true => {
71+
let dest = self.get_root().join(&dest).to_owned();
72+
self.set_dest(&dest);
73+
},
74+
false => { self.set_dest(&dest); },
75+
}
6576
}
6677
}
6778

6879
self
6980
}
7081

82+
pub fn get_root(&self) -> &Path {
83+
&self.root
84+
}
85+
86+
pub fn set_root(&mut self, root: &Path) -> &mut Self {
87+
self.root = root.to_owned();
88+
self
89+
}
90+
7191
pub fn get_dest(&self) -> &Path {
7292
&self.dest
7393
}

src/book/mdbook.rs

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::path::{Path, PathBuf};
1+
use std::path::Path;
22
use std::fs::{self, File, metadata};
33
use std::io::Write;
44
use std::error::Error;
@@ -12,7 +12,6 @@ use renderer::HtmlHandlebars;
1212

1313
pub struct MDBook {
1414
config: BookConfig,
15-
pub root: PathBuf,
1615
pub content: Vec<BookItem>,
1716
renderer: Box<Renderer>,
1817
}
@@ -39,9 +38,8 @@ impl MDBook {
3938
}
4039

4140
MDBook {
42-
root: root.to_path_buf(),
4341
content: vec![],
44-
config: BookConfig::new()
42+
config: BookConfig::new(root)
4543
.set_src(&root.join("src"))
4644
.set_dest(&root.join("book"))
4745
.to_owned(),
@@ -106,7 +104,7 @@ impl MDBook {
106104
// There is a very high chance that the error is due to the fact that
107105
// the directory / file does not exist
108106
debug!("[*]: {:?} does not exist, trying to create directory", dest);
109-
fs::create_dir(&dest).unwrap();
107+
try!(fs::create_dir(&dest));
110108
},
111109
Ok(_) => { /* If there is no error, the directory / file does exist */ }
112110
}
@@ -117,7 +115,7 @@ impl MDBook {
117115
// There is a very high chance that the error is due to the fact that
118116
// the directory / file does not exist
119117
debug!("[*]: {:?} does not exist, trying to create directory", src);
120-
fs::create_dir(&src).unwrap();
118+
try!(fs::create_dir(&src));
121119
},
122120
Ok(_) => { /* If there is no error, the directory / file does exist */ }
123121
}
@@ -128,7 +126,7 @@ impl MDBook {
128126
// There is a very high chance that the error is due to the fact that
129127
// the directory / file does not exist
130128
debug!("[*]: {:?} does not exist, trying to create SUMMARY.md", src.join("SUMMARY.md"));
131-
Ok(File::create(&src.join("SUMMARY.md")).unwrap())
129+
Ok(try!(File::create(&src.join("SUMMARY.md"))))
132130
},
133131
Ok(_) => {
134132
/* If there is no error, the directory / file does exist */
@@ -143,7 +141,7 @@ impl MDBook {
143141
try!(writeln!(f, ""));
144142
try!(writeln!(f, "- [Chapter 1](./chapter_1.md)"));
145143

146-
let mut chapter_1 = File::create(&src.join("chapter_1.md")).unwrap();
144+
let mut chapter_1 = try!(File::create(&src.join("chapter_1.md")));
147145
try!(writeln!(chapter_1, "# Chapter 1"));
148146
}
149147

@@ -181,7 +179,7 @@ impl MDBook {
181179
// There is a very high chance that the error is due to the fact that
182180
// the directory / file does not exist
183181
debug!("[*]: {:?} does not exist, trying to create directory", theme_dir);
184-
fs::create_dir(&theme_dir).unwrap();
182+
try!(fs::create_dir(&theme_dir));
185183
},
186184
Ok(_) => { /* If there is no error, the directory / file does exist */ }
187185
}
@@ -226,7 +224,8 @@ impl MDBook {
226224
/// of the current working directory by using a relative path instead of an absolute path.
227225
228226
pub fn read_config(mut self) -> Self {
229-
self.config.read_config(&self.root);
227+
let root = self.config.get_root().to_owned();
228+
self.config.read_config(&root);
230229
self
231230
}
232231

@@ -256,7 +255,16 @@ impl MDBook {
256255
}
257256

258257
pub fn set_dest(mut self, dest: &Path) -> Self {
259-
self.config.set_dest(&self.root.join(dest));
258+
259+
// Handle absolute and relative paths
260+
match dest.is_absolute() {
261+
true => { self.config.set_dest(dest); },
262+
false => {
263+
let dest = self.config.get_root().join(dest).to_owned();
264+
self.config.set_dest(&dest);
265+
}
266+
}
267+
260268
self
261269
}
262270

@@ -265,7 +273,16 @@ impl MDBook {
265273
}
266274

267275
pub fn set_src(mut self, src: &Path) -> Self {
268-
self.config.set_src(&self.root.join(src));
276+
277+
// Handle absolute and relative paths
278+
match src.is_absolute() {
279+
true => { self.config.set_src(src); },
280+
false => {
281+
let src = self.config.get_root().join(src).to_owned();
282+
self.config.set_src(&src);
283+
}
284+
}
285+
269286
self
270287
}
271288

src/renderer/html_handlebars/hbs_renderer.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,11 @@ impl Renderer for HtmlHandlebars {
7272

7373
// Remove content from previous file and render content for this one
7474
data.remove("path");
75-
data.insert("path".to_string(), item.path.to_str().unwrap().to_json());
75+
match item.path.to_str() {
76+
Some(p) => { data.insert("path".to_string(), p.to_json()); },
77+
None => return Err(Box::new(io::Error::new(io::ErrorKind::Other, "Could not convert path to str"))),
78+
}
79+
7680

7781
// Remove content from previous file and render content for this one
7882
data.remove("content");
@@ -144,7 +148,10 @@ fn make_data(book: BookItems, config: &BookConfig) -> Result<BTreeMap<String,Jso
144148
let mut chapter = BTreeMap::new();
145149
chapter.insert("section".to_string(), section.to_json());
146150
chapter.insert("name".to_string(), item.name.to_json());
147-
chapter.insert("path".to_string(), item.path.to_str().unwrap().to_json());
151+
match item.path.to_str() {
152+
Some(p) => { chapter.insert("path".to_string(), p.to_json()); },
153+
None => return Err(Box::new(io::Error::new(io::ErrorKind::Other, "Could not convert path to str"))),
154+
}
148155

149156
chapters.push(chapter);
150157
}

0 commit comments

Comments
 (0)