Added BookConfig struct, cleaned up some parts

This commit is contained in:
Mathieu David 2015-07-08 15:17:11 +02:00
parent 39c0344f9a
commit 4d884b9c40
4 changed files with 77 additions and 28 deletions

48
src/bookconfig.rs Normal file
View File

@ -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())
}
}
}

View File

@ -1,3 +1,4 @@
pub mod mdbook; pub mod mdbook;
mod bookconfig;
pub use mdbook::MDBook; pub use mdbook::MDBook;

View File

@ -1,7 +1,6 @@
extern crate mdbook; extern crate mdbook;
extern crate getopts; extern crate getopts;
use std::env; use std::env;
use std::path::PathBuf;
use mdbook::MDBook; use mdbook::MDBook;
@ -119,9 +118,9 @@ fn init(args: Vec<String>) {
std::env::current_dir().unwrap().join(&args[2]) 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); println!("Error: {}", e);
} }
@ -148,7 +147,7 @@ fn build(args: Vec<String>) {
} }
let dir = std::env::current_dir().unwrap(); let dir = std::env::current_dir().unwrap();
let book = MDBook::new(); let book = MDBook::new(&dir);
if let Err(e) = book.build(&dir) { if let Err(e) = book.build(&dir) {
println!("Error: {}", e); println!("Error: {}", e);

View File

@ -3,41 +3,40 @@ use std::fs::{self, File, metadata};
use std::io::Write; use std::io::Write;
use std::io::{Error, ErrorKind}; use std::io::{Error, ErrorKind};
use bookconfig::BookConfig;
pub struct MDBook { pub struct MDBook {
dest: PathBuf, path: PathBuf,
src: PathBuf, config: BookConfig,
} }
impl MDBook { 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 { MDBook {
dest: PathBuf::from("book"), path: path.to_owned(),
src: PathBuf::from("src"), config: BookConfig::new(),
} }
} }
pub fn init(&self, dir: &PathBuf) -> Result<(), Error> { pub fn init(&self) -> 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")),
_ => {}
}
// Logic problem: When self.dest is absolute, the directory given // Logic problem: When self.dest is absolute, the directory given
// as parameter is never used... // as parameter is never used...
let dest = if self.dest.is_relative() { let dest = self.path.join(&self.config.dest());
dir.join(&self.dest)
} else {
self.dest.clone()
};
let src = if self.src.is_relative() { let src = self.path.join(&self.config.src());
dir.join(&self.src)
} else {
self.src.clone()
};
// Hacky way to check if the directory exists... Until PathExt moves to stable // Hacky way to check if the directory exists... Until PathExt moves to stable
match metadata(&dest) { match metadata(&dest) {
@ -86,16 +85,18 @@ impl MDBook {
pub fn build(&self, dir: &PathBuf) -> Result<(), Error> { pub fn build(&self, dir: &PathBuf) -> Result<(), Error> {
Ok(()) Ok(())
} }
pub fn set_dest(mut self, dest: PathBuf) -> Self { pub fn set_dest(mut self, dest: PathBuf) -> Self {
self.dest = dest; self.config.set_dest(dest);
self self
} }
pub fn set_src(mut self, src: PathBuf) -> Self { pub fn set_src(mut self, src: PathBuf) -> Self {
self.src = src; self.config.set_src(src);
self self
} }