Added BookConfig struct, cleaned up some parts
This commit is contained in:
parent
39c0344f9a
commit
4d884b9c40
|
@ -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())
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,3 +1,4 @@
|
|||
pub mod mdbook;
|
||||
mod bookconfig;
|
||||
|
||||
pub use mdbook::MDBook;
|
||||
|
|
|
@ -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<String>) {
|
|||
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<String>) {
|
|||
}
|
||||
|
||||
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);
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue