add load_with_config_file, restructure load a bit

This commit is contained in:
josh rotenberg 2021-05-03 13:33:41 -07:00
parent a72d6002b7
commit 2db29dede7
1 changed files with 54 additions and 7 deletions

View File

@ -30,6 +30,8 @@ use crate::utils;
use crate::config::{Config, RustEdition}; use crate::config::{Config, RustEdition};
const DEFAULT_CONFIG_FILE_NAME: &str = "book.toml";
/// The object used to manage and build a book. /// The object used to manage and build a book.
pub struct MDBook { pub struct MDBook {
/// The book's root directory. /// The book's root directory.
@ -45,10 +47,38 @@ pub struct MDBook {
} }
impl MDBook { impl MDBook {
/// Load a book from its root directory on disk. /// Load a book from its root directory on disk. If a `book.toml` file is present in the
/// root directory, the configuration is loaded from that. Otherwise, the
/// default `Config` is used.
///
/// ```no_run
/// # use mdbook::MDBook;
/// let root_dir = "/path/to/book/root";
///
/// let mut md = MDBook::load(root_dir)
/// .expect("Unable to load the book");
/// ```
pub fn load<P: Into<PathBuf>>(book_root: P) -> Result<MDBook> { pub fn load<P: Into<PathBuf>>(book_root: P) -> Result<MDBook> {
MDBook::load_book(book_root, None)
}
/// Load a book from its root directory on disk, specifying a configuration file explicitly. If
/// the supplied configuration file is not found, an error is returned.
///
/// ```no_run
/// # use mdbook::MDBook;
/// let root_dir = "/path/to/book/root";
/// let alternate_config_file = "/path/to/alternate-config.toml";
///
/// let mut md = MDBook::load_with_config_file(root_dir, alternate_config_file)
/// .expect("Unable to load the book");
/// ```
pub fn load_with_config_file<P: Into<PathBuf>>(book_root: P, config_file: P) -> Result<MDBook> {
MDBook::load_book(book_root, Some(config_file))
}
fn load_book<P: Into<PathBuf>>(book_root: P, config_file: Option<P>) -> Result<MDBook> {
let book_root = book_root.into(); let book_root = book_root.into();
let config_location = book_root.join("book.toml");
// the book.json file is no longer used, so we should emit a warning to // the book.json file is no longer used, so we should emit a warning to
// let people know to migrate to book.toml // let people know to migrate to book.toml
@ -60,11 +90,28 @@ impl MDBook {
warn!("\thttps://rust-lang.github.io/mdBook/format/config.html"); warn!("\thttps://rust-lang.github.io/mdBook/format/config.html");
} }
let mut config = if config_location.exists() { let mut config = match config_file {
debug!("Loading config from {}", config_location.display()); // the config file was explicitly supplied. if it doesn't exist, bail with an error
Config::from_disk(&config_location)? Some(config_file) => {
} else { let config_file = config_file.into();
Config::default() if config_file.exists() {
debug!("Loading config from {}", config_file.display());
Config::from_disk(&config_file)?
} else {
bail!("Configuration file {} not found", config_file.display());
}
}
// no config file was supplied, look for a book.toml, or fall back to the default
_ => {
let config_file = book_root.join(DEFAULT_CONFIG_FILE_NAME);
if config_file.exists() {
debug!("Loading config from {}", DEFAULT_CONFIG_FILE_NAME);
Config::from_disk(&config_file)?
} else {
debug!("Loading config from Config::default()");
Config::default()
}
}
}; };
config.update_from_env(); config.update_from_env();