Clean-up: Remove all 'hacky' exist checks and replace by 'exists()'

This commit is contained in:
Mathieu David 2015-08-13 10:46:56 +02:00
parent abae21527a
commit a5aa357f57
4 changed files with 51 additions and 91 deletions

View File

@ -78,7 +78,8 @@ fn init(args: &ArgMatches) -> Result<(), Box<Error>> {
// Read answer from user and exit if it's not 'yes'
if !confirm() {
println!("\nexiting...\n");
println!("\nSkipping...\n");
println!("All done, no errors...");
::std::process::exit(0);
}
}
@ -87,9 +88,10 @@ fn init(args: &ArgMatches) -> Result<(), Box<Error>> {
try!(book.copy_theme());
println!("\nTheme copied.");
println!("");
}
println!("\nAll done, no errors...");
Ok(())
}

View File

@ -1,14 +1,12 @@
use std::path::Path;
use std::fs::{self, File, metadata};
use std::fs::{self, File};
use std::io::Write;
use std::error::Error;
use {BookConfig, BookItem};
use {BookConfig, BookItem, theme, parse};
use book::BookItems;
use parse;
use theme;
use renderer::Renderer;
use renderer::HtmlHandlebars;
use renderer::{Renderer, HtmlHandlebars};
use utils::{PathExt, create_path};
pub struct MDBook {
config: BookConfig,
@ -27,14 +25,8 @@ impl MDBook {
pub fn new(root: &Path) -> MDBook {
// Hacky way to check if the path exists... Until PathExt moves to stable
match metadata(root) {
Err(_) => panic!("Directory does not exist"),
Ok(f) => {
if !f.is_dir() {
panic!("Is not a directory");
}
}
if !root.exists() || !root.is_dir() {
output!("{:?} No directory with that name", root);
}
MDBook {
@ -95,46 +87,33 @@ impl MDBook {
debug!("[fn]: init");
if !self.config.get_root().exists() {
create_path(self.config.get_root()).unwrap();
output!("{:?} created", self.config.get_root());
}
let dest = self.config.get_dest();
let src = self.config.get_src();
// Hacky way to check if the directory exists... Until PathExt moves to stable
match metadata(&dest) {
Err(_) => {
// There is a very high chance that the error is due to the fact that
// the directory / file does not exist
if !dest.exists() {
debug!("[*]: {:?} does not exist, trying to create directory", dest);
try!(fs::create_dir(&dest));
},
Ok(_) => { /* If there is no error, the directory / file does exist */ }
}
// Hacky way to check if the directory exists... Until PathExt moves to stable
match metadata(&src) {
Err(_) => {
// There is a very high chance that the error is due to the fact that
// the directory / file does not exist
if !src.exists() {
debug!("[*]: {:?} does not exist, trying to create directory", src);
try!(fs::create_dir(&src));
},
Ok(_) => { /* If there is no error, the directory / file does exist */ }
}
// Hacky way to check if the directory exists... Until PathExt moves to stable
let summary = match metadata(&src.join("SUMMARY.md")) {
Err(_) => {
// There is a very high chance that the error is due to the fact that
// the directory / file does not exist
let summary = src.join("SUMMARY.md");
if !summary.exists() {
// Summary does not exist, create it and populate it
debug!("[*]: {:?} does not exist, trying to create SUMMARY.md", src.join("SUMMARY.md"));
Ok(try!(File::create(&src.join("SUMMARY.md"))))
},
Ok(_) => {
/* If there is no error, the directory / file does exist */
Err("SUMMARY.md does already exist")
}
};
let mut f = try!(File::create(&src.join("SUMMARY.md")));
if let Ok(mut f) = summary {
debug!("[*]: Writing to SUMMARY.md");
try!(writeln!(f, "# Summary"));
@ -143,6 +122,10 @@ impl MDBook {
let mut chapter_1 = try!(File::create(&src.join("chapter_1.md")));
try!(writeln!(chapter_1, "# Chapter 1"));
} else {
// Summary does exist, read it and create the missing files
}
return Ok(());
@ -173,15 +156,9 @@ impl MDBook {
let theme_dir = self.config.get_src().join("theme");
// Hacky way to check if the directory exists... Until PathExt moves to stable
match metadata(&theme_dir) {
Err(_) => {
// There is a very high chance that the error is due to the fact that
// the directory / file does not exist
if !theme_dir.exists() {
debug!("[*]: {:?} does not exist, trying to create directory", theme_dir);
try!(fs::create_dir(&theme_dir));
},
Ok(_) => { /* If there is no error, the directory / file does exist */ }
}
// index.hbs

View File

@ -1,7 +1,9 @@
use std::path::Path;
use std::fs::{File, metadata};
use std::fs::File;
use std::io::Read;
use utils::{PathExt};
pub static INDEX: &'static [u8] = include_bytes!("index.hbs");
pub static CSS: &'static [u8] = include_bytes!("book.css");
pub static JS: &'static [u8] = include_bytes!("book.js");
@ -35,26 +37,14 @@ impl Theme {
};
// Check if the given path exists
// Hacky way to check if the path exists... Until PathExt moves to stable
match metadata(&src) {
Err(_) => return theme,
Ok(f) => {
if !f.is_dir() {
return theme;
}
},
if !src.exists() || !src.is_dir() {
return theme
}
let src = src.join("theme");
// If src does exist, check if there is a theme directory in it
// Hacky way to check if the path exists... Until PathExt moves to stable
match metadata(&src) {
Err(_) => return theme,
Ok(f) => {
if !f.is_dir() {
return theme;
}
}
if !src.exists() || !src.is_dir() {
return theme
}
// Check for individual files if they exist

View File

@ -87,23 +87,14 @@ pub fn create_path(path: &Path) -> Result<(), Box<Error>> {
constructed_path.push(&dir);
debug!("[*]: {:?}", constructed_path);
// Check if path exists
match metadata(&constructed_path) {
// Any way to combine the Err and first Ok branch ??
Err(_) => {
try!(fs::create_dir(&constructed_path));
debug!("[*]: Directory created {:?}", constructed_path);
},
Ok(f) => {
if !f.is_dir() {
if !constructed_path.exists() || !constructed_path.is_dir() {
try!(fs::create_dir(&constructed_path));
debug!("[*]: Directory created {:?}", constructed_path);
} else {
debug!("[*]: Directory exists {:?}", constructed_path);
continue
}
},
}
}
debug!("[*]: Constructed path: {:?}", constructed_path);
@ -122,7 +113,7 @@ pub fn create_file(path: &Path) -> Result<File, Box<Error>> {
try!(create_path(p));
}
debug!("[*]: Create file: {}", path);
debug!("[*]: Create file: {:?}", path);
let f = try!(File::create(path));
Ok(f)