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

View File

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

View File

@ -1,7 +1,9 @@
use std::path::Path; use std::path::Path;
use std::fs::{File, metadata}; use std::fs::File;
use std::io::Read; use std::io::Read;
use utils::{PathExt};
pub static INDEX: &'static [u8] = include_bytes!("index.hbs"); pub static INDEX: &'static [u8] = include_bytes!("index.hbs");
pub static CSS: &'static [u8] = include_bytes!("book.css"); pub static CSS: &'static [u8] = include_bytes!("book.css");
pub static JS: &'static [u8] = include_bytes!("book.js"); pub static JS: &'static [u8] = include_bytes!("book.js");
@ -35,26 +37,14 @@ impl Theme {
}; };
// Check if the given path exists // Check if the given path exists
// Hacky way to check if the path exists... Until PathExt moves to stable if !src.exists() || !src.is_dir() {
match metadata(&src) { return theme
Err(_) => return theme,
Ok(f) => {
if !f.is_dir() {
return theme;
}
},
} }
let src = src.join("theme"); let src = src.join("theme");
// If src does exist, check if there is a theme directory in it // 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 if !src.exists() || !src.is_dir() {
match metadata(&src) { return theme
Err(_) => return theme,
Ok(f) => {
if !f.is_dir() {
return theme;
}
}
} }
// Check for individual files if they exist // 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); constructed_path.push(&dir);
debug!("[*]: {:?}", constructed_path); debug!("[*]: {:?}", constructed_path);
// Check if path exists if !constructed_path.exists() || !constructed_path.is_dir() {
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() {
try!(fs::create_dir(&constructed_path)); try!(fs::create_dir(&constructed_path));
debug!("[*]: Directory created {:?}", constructed_path); debug!("[*]: Directory created {:?}", constructed_path);
} else { } else {
debug!("[*]: Directory exists {:?}", constructed_path); debug!("[*]: Directory exists {:?}", constructed_path);
continue continue
} }
},
}
} }
debug!("[*]: Constructed path: {:?}", constructed_path); debug!("[*]: Constructed path: {:?}", constructed_path);
@ -122,7 +113,7 @@ pub fn create_file(path: &Path) -> Result<File, Box<Error>> {
try!(create_path(p)); try!(create_path(p));
} }
debug!("[*]: Create file: {}", path); debug!("[*]: Create file: {:?}", path);
let f = try!(File::create(path)); let f = try!(File::create(path));
Ok(f) Ok(f)