Skip to content

Commit

Permalink
Merge pull request #41 from mdzk-rs/pr-40
Browse files Browse the repository at this point in the history
handling book section on mdzk.toml
  • Loading branch information
kmaasrud authored Nov 6, 2021
2 parents 1a2c4df + d4b9491 commit f5a2e4a
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 8 deletions.
9 changes: 8 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
# Changelog

## 0.4.3 (Unreleased)
## Unreleased

### Enhacements

- mdzk now checks whether you updated your `mdzk.toml` correctly or not. A error will pop up if it finds a `[book]` on
your `mdzk.toml` file.

## 0.4.3

### New features

Expand Down
16 changes: 11 additions & 5 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,17 @@ impl Config {
/// Load an mdzk configuration file from a path.
pub fn from_disk<P: AsRef<Path>>(path: P) -> Result<Config> {
let mut buffer = String::new();
File::open(path)
.context("Unable to open the configuration file")?
File::open(&path)
.with_context(|| format!("Unable to open {:?}.", path.as_ref()))?
.read_to_string(&mut buffer)
.context("Couldn't read the file")?;
.context("Couldn't read the configuration file")?;

let mut conf = Config::from_str(&buffer).unwrap();
let mut conf = Config::from_str(&buffer)
.with_context(|| format!("Unable to load the configuration file {:?}", path.as_ref()))?;

if conf.rest.get_mut("book").is_some() {
warn!("Found a '[book]' section on your 'mdzk.toml' file. You might want to replace it with '[mdzk]' ;-)")
}

if let Some(preprocessors) = conf
.rest
Expand Down Expand Up @@ -63,7 +68,8 @@ impl FromStr for Config {

/// Load an mdzk configuration from some string.
fn from_str(src: &str) -> Result<Self> {
toml::from_str(src).context("Invalid configuration file")
toml::from_str(src)
.with_context(|| format!("Invalid TOML:\n{}", src))
}
}

Expand Down
3 changes: 1 addition & 2 deletions src/zk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ pub fn load_zk(dir: Option<PathBuf>) -> Result<MDBook, Error> {
};
debug!("Found root: {:?}", root);

let config: Config = Config::from_disk(root.join(CONFIG_FILE))
.with_context(|| format!("Could not load config file {:?}", root.join(CONFIG_FILE)))?;
let config: Config = Config::from_disk(root.join(CONFIG_FILE))?;
debug!("Successfully loaded config.");

if config.mdzk.generate_summary.unwrap_or(true) {
Expand Down

0 comments on commit f5a2e4a

Please sign in to comment.