clean up structure and code

This commit is contained in:
Mathieu David 2015-07-16 18:20:36 +02:00
parent 2cec95c352
commit ad01c37432
6 changed files with 50 additions and 67 deletions

View File

@ -1,48 +0,0 @@
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,4 +1,3 @@
pub mod mdbook; mod mdbook;
mod bookconfig;
pub use mdbook::MDBook; pub use mdbook::mdbook::MDBook;

View File

@ -40,7 +40,6 @@ fn main() {
None => no_subcommand(args), None => no_subcommand(args),
Some(command) => (command.exec)(args), Some(command) => (command.exec)(args),
} }
} }
@ -64,7 +63,7 @@ fn no_subcommand(args: Vec<String>) {
if matches.opt_present("version") { if matches.opt_present("version") {
println!("{} {}", NAME, VERSION); println!("{} {}", NAME, VERSION);
} else { } else {
if !matches.opt_present("version"){ if !matches.opt_present("version") && args.len() > 0 {
print!("Try again, `{0}", NAME); print!("Try again, `{0}", NAME);
for index in 1..args.len() { for index in 1..args.len() {
print!(" {}", args[index]); print!(" {}", args[index]);
@ -88,7 +87,6 @@ fn help(usage: &String) {
} }
println!(""); println!("");
println!("For more information about a specific command, try `mdbook <command> --help`"); println!("For more information about a specific command, try `mdbook <command> --help`");
} }
fn init(args: Vec<String>) { fn init(args: Vec<String>) {
@ -123,7 +121,6 @@ fn init(args: Vec<String>) {
if let Err(e) = book.init() { if let Err(e) = book.init() {
println!("Error: {}", e); println!("Error: {}", e);
} }
} }
fn build(args: Vec<String>) { fn build(args: Vec<String>) {

36
src/mdbook/bookconfig.rs Normal file
View File

@ -0,0 +1,36 @@
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) -> Self {
self.dest = dest;
self
}
pub fn src(&self) -> PathBuf {
self.src.clone()
}
pub fn set_src(mut self, src: PathBuf) -> Self {
self.src = src;
self
}
}

View File

@ -1,12 +1,11 @@
use std::path::PathBuf; use std::path::PathBuf;
use std::fs::{self, File, metadata}; use std::fs::{self, File, metadata};
use std::io::Write; use std::io::Write;
use std::io::{Error, ErrorKind}; use std::io::Error;
use bookconfig::BookConfig; use mdbook::bookconfig::BookConfig;
pub struct MDBook { pub struct MDBook {
path: PathBuf,
config: BookConfig, config: BookConfig,
} }
@ -25,18 +24,16 @@ impl MDBook {
} }
MDBook { MDBook {
path: path.to_owned(), config: BookConfig::new()
config: BookConfig::new(), .set_src(path.join("src"))
.set_dest(path.join("book")),
} }
} }
pub fn init(&self) -> Result<(), Error> { pub fn init(&self) -> Result<(), Error> {
// Logic problem: When self.dest is absolute, the directory given let dest = self.config.dest();
// as parameter is never used... let src = self.config.src();
let dest = self.path.join(&self.config.dest());
let src = self.path.join(&self.config.src());
// 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) {
@ -91,12 +88,12 @@ impl MDBook {
} }
pub fn set_dest(mut self, dest: PathBuf) -> Self { pub fn set_dest(mut self, dest: PathBuf) -> Self {
self.config.set_dest(dest); self.config = 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.config.set_src(src); self.config = self.config.set_src(src);
self self
} }

2
src/mdbook/mod.rs Normal file
View File

@ -0,0 +1,2 @@
pub mod mdbook;
mod bookconfig;