From a27bc498ebe99a98f8c500773b1dac3841f2d3d6 Mon Sep 17 00:00:00 2001 From: Mathieu David Date: Tue, 7 Jul 2015 02:56:19 +0200 Subject: [PATCH] Started to implement init --- .gitignore | 2 ++ src/lib.rs | 6 ++-- src/main.rs | 8 ++++++ src/mdbook.rs | 79 +++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 92 insertions(+), 3 deletions(-) create mode 100644 src/mdbook.rs diff --git a/.gitignore b/.gitignore index a9d37c56..c7418582 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ target Cargo.lock + +book-test diff --git a/src/lib.rs b/src/lib.rs index a93251b6..dbf26e55 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,3 +1,3 @@ -#[test] -fn it_works() { -} +pub mod mdbook; + +pub use mdbook::MDBook; diff --git a/src/main.rs b/src/main.rs index c120250e..9cfad581 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,6 +2,7 @@ extern crate mdbook; extern crate getopts; use std::env; +use mdbook::MDBook; const NAME: &'static str = "mdbook"; const VERSION: &'static str = "0.0.1"; @@ -108,7 +109,14 @@ fn init(args: Vec) { if matches.opt_present("help") { println!("{}", usage); + return; } + + let dir = std::env::current_dir().unwrap(); + let book = MDBook::new(); + + book.init(&dir); + } fn build(args: Vec) { diff --git a/src/mdbook.rs b/src/mdbook.rs new file mode 100644 index 00000000..e3629ec2 --- /dev/null +++ b/src/mdbook.rs @@ -0,0 +1,79 @@ +use std::path::PathBuf; +use std::fs::{self, File, metadata}; + +pub struct MDBook { + dest: PathBuf, + src: PathBuf, +} + +impl MDBook { + pub fn new() -> Self { + MDBook { + dest: PathBuf::from("book"), + src: PathBuf::from("src"), + } + } + + pub fn init(&self, dir: &PathBuf) -> Result<(),&str> { + + // Hacky way to check if the directory exists... Until PathExt moves to stable + match metadata(dir) { + Err(_) => return Err("Destination path does not exist"), + _ => {} + } + + let dest = if self.dest.is_relative() { + dir.join(&self.dest) + } else { + self.dest.clone() + }; + + let src = if self.src.is_relative() { + dir.join(&self.src) + } else { + self.src.clone() + }; + + // 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 + fs::create_dir(&dest).unwrap(); + }, + 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 + fs::create_dir(&src).unwrap(); + }, + 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(dir.join("src/SUMMARY.md")) { + Err(_) => { + // There is a very high chance that the error is due to the fact that + // the directory / file does not exist + File::create("src/SUMMARY.md").unwrap(); + }, + Ok(_) => { /* If there is no error, the directory / file does exist */ } + } + + return Ok(()); + } + + pub fn set_dest(mut self, dest: PathBuf) -> Self { + self.dest = dest; + self + } + + pub fn set_src(mut self, src: PathBuf) -> Self { + self.src = src; + self + } +}