diff --git a/src/bookconfig.rs b/src/bookconfig.rs new file mode 100644 index 00000000..302fbf08 --- /dev/null +++ b/src/bookconfig.rs @@ -0,0 +1,48 @@ +use std::path::PathBuf; + +pub struct BookConfig { + dest: PathBuf, + src: PathBuf, + multilingual: bool, +} + + +impl BookConfig { + pub fn new() -> Self { + BookConfig { + dest: PathBuf::from("book"), + src: PathBuf::from("src"), + multilingual: false, + } + } + + pub fn dest(&self) -> PathBuf { + self.dest.clone() + } + + pub fn set_dest(&mut self, dest: PathBuf) { + + // dest has to be relative to the path in MDBook, + // we check if the path is relative, otherwhise we truncate + if dest.is_relative() { + self.dest = dest + } else { + self.dest = PathBuf::from(dest.file_name().unwrap()) + } + } + + pub fn src(&self) -> PathBuf { + self.src.clone() + } + + pub fn set_src(&mut self, src: PathBuf) { + + // src has to be relative to the path in MDBook, + // we check if the path is relative, otherwhise we truncate + if src.is_relative() { + self.src = src + } else { + self.src = PathBuf::from(src.file_name().unwrap()) + } + } +} diff --git a/src/lib.rs b/src/lib.rs index dbf26e55..667fd136 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,3 +1,4 @@ pub mod mdbook; +mod bookconfig; pub use mdbook::MDBook; diff --git a/src/main.rs b/src/main.rs index ea9fa087..9be7e1f6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,7 +1,6 @@ extern crate mdbook; extern crate getopts; use std::env; -use std::path::PathBuf; use mdbook::MDBook; @@ -119,9 +118,9 @@ fn init(args: Vec) { std::env::current_dir().unwrap().join(&args[2]) }; - let book = MDBook::new(); + let book = MDBook::new(&dir); - if let Err(e) = book.init(&dir) { + if let Err(e) = book.init() { println!("Error: {}", e); } @@ -148,7 +147,7 @@ fn build(args: Vec) { } let dir = std::env::current_dir().unwrap(); - let book = MDBook::new(); + let book = MDBook::new(&dir); if let Err(e) = book.build(&dir) { println!("Error: {}", e); diff --git a/src/mdbook.rs b/src/mdbook.rs index de19af7c..d01e4520 100644 --- a/src/mdbook.rs +++ b/src/mdbook.rs @@ -3,41 +3,40 @@ use std::fs::{self, File, metadata}; use std::io::Write; use std::io::{Error, ErrorKind}; +use bookconfig::BookConfig; + pub struct MDBook { - dest: PathBuf, - src: PathBuf, + path: PathBuf, + config: BookConfig, } impl MDBook { - pub fn new() -> Self { + pub fn new(path: &PathBuf) -> Self { + + // Hacky way to check if the path exists... Until PathExt moves to stable + match metadata(path) { + Err(_) => panic!("Directory does not exist"), + Ok(f) => { + if !f.is_dir() { + panic!("Is not a directory"); + } + } + } + MDBook { - dest: PathBuf::from("book"), - src: PathBuf::from("src"), + path: path.to_owned(), + config: BookConfig::new(), } } - pub fn init(&self, dir: &PathBuf) -> Result<(), Error> { - - // Hacky way to check if the directory exists... Until PathExt moves to stable - match metadata(dir) { - Err(_) => return Err(Error::new(ErrorKind::Other, "Directory does not exist")), - _ => {} - } + pub fn init(&self) -> Result<(), Error> { // Logic problem: When self.dest is absolute, the directory given // as parameter is never used... - let dest = if self.dest.is_relative() { - dir.join(&self.dest) - } else { - self.dest.clone() - }; + let dest = self.path.join(&self.config.dest()); - let src = if self.src.is_relative() { - dir.join(&self.src) - } else { - self.src.clone() - }; + let src = self.path.join(&self.config.src()); // Hacky way to check if the directory exists... Until PathExt moves to stable match metadata(&dest) { @@ -86,16 +85,18 @@ impl MDBook { pub fn build(&self, dir: &PathBuf) -> Result<(), Error> { + + Ok(()) } pub fn set_dest(mut self, dest: PathBuf) -> Self { - self.dest = dest; + self.config.set_dest(dest); self } pub fn set_src(mut self, src: PathBuf) -> Self { - self.src = src; + self.config.set_src(src); self }