Skip to content

Commit 78e1897

Browse files
committed
Remove code that has better equivalent in std
Path_Ext has been stabilized in the Standard Library, the temporary copy I had can go. I found a fs::create_dir_all method that does exactly what create_path was doing, but better... create_path is thus replaced with that.
1 parent d000fc8 commit 78e1897

File tree

4 files changed

+63
-89
lines changed

4 files changed

+63
-89
lines changed

src/book/mdbook.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use std::process::Command;
99
use {BookConfig, BookItem, theme, parse, utils};
1010
use book::BookItems;
1111
use renderer::{Renderer, HtmlHandlebars};
12-
use utils::{PathExt, create_path};
12+
1313

1414
pub struct MDBook {
1515
config: BookConfig,
@@ -96,7 +96,7 @@ impl MDBook {
9696
debug!("[fn]: init");
9797

9898
if !self.config.get_root().exists() {
99-
create_path(self.config.get_root()).unwrap();
99+
fs::create_dir_all(self.config.get_root()).unwrap();
100100
output!("{:?} created", self.config.get_root());
101101
}
102102

src/renderer/html_handlebars/hbs_renderer.rs

Lines changed: 48 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use book::bookitem::BookItem;
88
use {utils, theme};
99

1010
use std::path::{Path, PathBuf};
11-
use std::fs::File;
11+
use std::fs::{self, File};
1212
use std::error::Error;
1313
use std::io::{self, Read, Write};
1414
use std::collections::BTreeMap;
@@ -50,7 +50,7 @@ impl Renderer for HtmlHandlebars {
5050

5151
// Check if dest directory exists
5252
debug!("[*]: Check if destination directory exists");
53-
if let Err(_) = utils::create_path(book.get_dest()) {
53+
if let Err(_) = fs::create_dir_all(book.get_dest()) {
5454
return Err(Box::new(io::Error::new(io::ErrorKind::Other, "Unexpected error when constructing destination path")))
5555
}
5656

@@ -159,42 +159,69 @@ impl Renderer for HtmlHandlebars {
159159

160160
debug!("[*] Copy static files");
161161
// JavaScript
162-
let mut js_file = try!(File::create(book.get_dest().join("book.js")));
162+
let mut js_file = if let Ok(f) = File::create(book.get_dest().join("book.js")) { f } else {
163+
return Err(Box::new(io::Error::new(io::ErrorKind::Other, "Could not create book.js")))
164+
};
163165
try!(js_file.write_all(&theme.js));
164166

165167
// Css
166-
let mut css_file = try!(File::create(book.get_dest().join("book.css")));
168+
let mut css_file = if let Ok(f) = File::create(book.get_dest().join("book.css")) { f } else {
169+
return Err(Box::new(io::Error::new(io::ErrorKind::Other, "Could not create book.css")))
170+
};
167171
try!(css_file.write_all(&theme.css));
168172

169173
// JQuery local fallback
170-
let mut jquery = try!(File::create(book.get_dest().join("jquery.js")));
174+
let mut jquery = if let Ok(f) = File::create(book.get_dest().join("jquery.js")) { f } else {
175+
return Err(Box::new(io::Error::new(io::ErrorKind::Other, "Could not create jquery.js")))
176+
};
171177
try!(jquery.write_all(&theme.jquery));
172178

179+
// syntax highlighting
180+
let mut highlight_css = if let Ok(f) = File::create(book.get_dest().join("highlight.css")) { f } else {
181+
return Err(Box::new(io::Error::new(io::ErrorKind::Other, "Could not create highlight.css")))
182+
};
183+
try!(highlight_css.write_all(&theme.highlight_css));
184+
185+
let mut tomorrow_night_css = if let Ok(f) = File::create(book.get_dest().join("tomorrow-night.css")) { f } else {
186+
return Err(Box::new(io::Error::new(io::ErrorKind::Other, "Could not create tomorrow-night.css")))
187+
};
188+
try!(tomorrow_night_css.write_all(&theme.tomorrow_night_css));
189+
190+
let mut highlight_js = if let Ok(f) = File::create(book.get_dest().join("highlight.js")) { f } else {
191+
return Err(Box::new(io::Error::new(io::ErrorKind::Other, "Could not create highlight.js")))
192+
};
193+
try!(highlight_js.write_all(&theme.highlight_js));
194+
173195
// Font Awesome local fallback
174-
let mut font_awesome = try!(utils::create_file(&book.get_dest().join("_FontAwesome/css/font-awesome").with_extension("css")));
196+
let mut font_awesome = if let Ok(f) = utils::create_file(&book.get_dest().join("_FontAwesome/css/font-awesome.css")) { f } else {
197+
return Err(Box::new(io::Error::new(io::ErrorKind::Other, "Could not create font-awesome.css")))
198+
};
175199
try!(font_awesome.write_all(theme::FONT_AWESOME));
176-
let mut font_awesome = try!(utils::create_file(&book.get_dest().join("_FontAwesome/fonts/fontawesome-webfont.eot")));
200+
let mut font_awesome = if let Ok(f) = utils::create_file(&book.get_dest().join("_FontAwesome/fonts/fontawesome-webfont.eot")) { f } else {
201+
return Err(Box::new(io::Error::new(io::ErrorKind::Other, "Could not create fontawesome-webfont.eot")))
202+
};
177203
try!(font_awesome.write_all(theme::FONT_AWESOME_EOT));
178-
let mut font_awesome = try!(utils::create_file(&book.get_dest().join("_FontAwesome/fonts/fontawesome-webfont.svg")));
204+
let mut font_awesome = if let Ok(f) = utils::create_file(&book.get_dest().join("_FontAwesome/fonts/fontawesome-webfont.svg")) { f } else {
205+
return Err(Box::new(io::Error::new(io::ErrorKind::Other, "Could not create fontawesome-webfont.svg")))
206+
};
179207
try!(font_awesome.write_all(theme::FONT_AWESOME_SVG));
180-
let mut font_awesome = try!(utils::create_file(&book.get_dest().join("_FontAwesome/fonts/fontawesome-webfont.ttf")));
208+
let mut font_awesome = if let Ok(f) = utils::create_file(&book.get_dest().join("_FontAwesome/fonts/fontawesome-webfont.ttf")) { f } else {
209+
return Err(Box::new(io::Error::new(io::ErrorKind::Other, "Could not create fontawesome-webfont.ttf")))
210+
};
181211
try!(font_awesome.write_all(theme::FONT_AWESOME_TTF));
182-
let mut font_awesome = try!(utils::create_file(&book.get_dest().join("_FontAwesome/fonts/fontawesome-webfont.woff")));
212+
let mut font_awesome = if let Ok(f) = utils::create_file(&book.get_dest().join("_FontAwesome/fonts/fontawesome-webfont.woff")) { f } else {
213+
return Err(Box::new(io::Error::new(io::ErrorKind::Other, "Could not create fontawesome-webfont.woff")))
214+
};
183215
try!(font_awesome.write_all(theme::FONT_AWESOME_WOFF));
184-
let mut font_awesome = try!(utils::create_file(&book.get_dest().join("_FontAwesome/fonts/fontawesome-webfont.woff2")));
216+
let mut font_awesome = if let Ok(f) = utils::create_file(&book.get_dest().join("_FontAwesome/fonts/fontawesome-webfont.woff2")) { f } else {
217+
return Err(Box::new(io::Error::new(io::ErrorKind::Other, "Could not create fontawesome-webfont.woff2")))
218+
};
185219
try!(font_awesome.write_all(theme::FONT_AWESOME_WOFF2));
186-
let mut font_awesome = try!(utils::create_file(&book.get_dest().join("_FontAwesome/fonts/FontAwesome.ttf")));
220+
let mut font_awesome = if let Ok(f) = utils::create_file(&book.get_dest().join("_FontAwesome/fonts/FontAwesome.ttf")) { f } else {
221+
return Err(Box::new(io::Error::new(io::ErrorKind::Other, "Could not create FontAwesome.ttf")))
222+
};
187223
try!(font_awesome.write_all(theme::FONT_AWESOME_TTF));
188224

189-
// syntax highlighting
190-
let mut highlight_css = try!(File::create(book.get_dest().join("highlight.css")));
191-
try!(highlight_css.write_all(&theme.highlight_css));
192-
let mut tomorrow_night_css = try!(File::create(book.get_dest().join("tomorrow-night.css")));
193-
try!(tomorrow_night_css.write_all(&theme.tomorrow_night_css));
194-
let mut highlight_js = try!(File::create(book.get_dest().join("highlight.js")));
195-
try!(highlight_js.write_all(&theme.highlight_js));
196-
197-
198225
// Copy all remaining files
199226
try!(utils::copy_files_except_ext(book.get_src(), book.get_dest(), true, &["md"]));
200227

src/theme/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ use std::path::Path;
22
use std::fs::File;
33
use std::io::Read;
44

5-
use utils::{PathExt};
65

76
pub static INDEX: &'static [u8] = include_bytes!("index.hbs");
87
pub static CSS: &'static [u8] = include_bytes!("book.css");

src/utils/mod.rs

Lines changed: 13 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,12 @@
11
extern crate pulldown_cmark;
22

3-
use std::path::{Path, PathBuf, Component};
3+
use std::path::{Path, Component};
44
use std::error::Error;
5+
use std::io;
56
use std::fs::{self, metadata, File};
67

78
use self::pulldown_cmark::{Parser, html, Options, OPTION_ENABLE_TABLES, OPTION_ENABLE_FOOTNOTES};
89

9-
/// This is copied from the rust source code until Path_ Ext stabilizes.
10-
/// You can use it, but be aware that it will be removed when those features go to rust stable
11-
pub trait PathExt {
12-
fn exists(&self) -> bool;
13-
fn is_file(&self) -> bool;
14-
fn is_dir(&self) -> bool;
15-
}
16-
17-
impl PathExt for Path {
18-
fn exists(&self) -> bool {
19-
metadata(self).is_ok()
20-
}
21-
22-
fn is_file(&self) -> bool {
23-
metadata(self).map(|s| s.is_file()).unwrap_or(false)
24-
}
25-
26-
fn is_dir(&self) -> bool {
27-
metadata(self).map(|s| s.is_dir()).unwrap_or(false)
28-
}
29-
}
30-
3110
/// Takes a path and returns a path containing just enough `../` to point to the root of the given path.
3211
///
3312
/// This is mostly interesting for a relative path to point back to the directory from where the
@@ -65,46 +44,7 @@ pub fn path_to_root(path: &Path) -> String {
6544
})
6645
}
6746

68-
/// This function checks for every component in a path if the directory exists,
69-
/// if it does not it is created.
70-
71-
pub fn create_path(path: &Path) -> Result<(), Box<Error>> {
72-
debug!("[fn]: create_path");
73-
74-
// Create directories if they do not exist
75-
let mut constructed_path = PathBuf::new();
76-
77-
for component in path.components() {
7847

79-
let dir;
80-
match component {
81-
Component::Normal(_) => { dir = PathBuf::from(component.as_os_str()); },
82-
Component::RootDir => {
83-
debug!("[*]: Root directory");
84-
// This doesn't look very compatible with Windows...
85-
constructed_path.push("/");
86-
continue
87-
},
88-
_ => continue,
89-
}
90-
91-
constructed_path.push(&dir);
92-
debug!("[*]: {:?}", constructed_path);
93-
94-
if !constructed_path.exists() || !constructed_path.is_dir() {
95-
try!(fs::create_dir(&constructed_path));
96-
debug!("[*]: Directory created {:?}", constructed_path);
97-
} else {
98-
debug!("[*]: Directory exists {:?}", constructed_path);
99-
continue
100-
}
101-
102-
}
103-
104-
debug!("[*]: Constructed path: {:?}", constructed_path);
105-
106-
Ok(())
107-
}
10848

10949
/// This function creates a file and returns it. But before creating the file it checks every
11050
/// directory in the path to see if it exists, and if it does not it will be created.
@@ -114,11 +54,19 @@ pub fn create_file(path: &Path) -> Result<File, Box<Error>> {
11454

11555
// Construct path
11656
if let Some(p) = path.parent() {
117-
try!(create_path(p));
57+
debug!("Parent directory is: {:?}", p);
58+
59+
try!(fs::create_dir_all(p));
11860
}
11961

12062
debug!("[*]: Create file: {:?}", path);
121-
let f = try!(File::create(path));
63+
let f = match File::create(path) {
64+
Ok(f) => f,
65+
Err(e) => {
66+
debug!("File::create: {}", e);
67+
return Err(Box::new(io::Error::new(io::ErrorKind::Other, format!("{}", e))))
68+
},
69+
};
12270

12371
Ok(f)
12472
}
@@ -172,7 +120,7 @@ pub fn copy_files_except_ext(from: &Path, to: &Path, recursive: bool, ext_blackl
172120
if let Some(ext) = entry.path().extension() {
173121
if ext_blacklist.contains(&ext.to_str().unwrap()) { continue }
174122
debug!("[*] creating path for file: {:?}", &to.join(entry.path().file_name().expect("a file should have a file name...")));
175-
//try!(create_path(&to.join(entry.path())));
123+
176124
output!("[*] copying file: {:?}\n to {:?}", entry.path(), &to.join(entry.path().file_name().expect("a file should have a file name...")));
177125
try!(fs::copy(entry.path(), &to.join(entry.path().file_name().expect("a file should have a file name..."))));
178126
}

0 commit comments

Comments
 (0)