Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

disincentivize usage of functions that expose toml::Table in Config #2407

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions examples/nop-preprocessor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ fn handle_supports(pre: &dyn Preprocessor, sub_args: &ArgMatches) -> ! {
/// in your main `lib.rs` file.
mod nop_lib {
use super::*;
use serde::Deserialize;

/// A no-op preprocessor.
pub struct Nop;
Expand All @@ -87,10 +88,18 @@ mod nop_lib {
}

fn run(&self, ctx: &PreprocessorContext, book: Book) -> Result<Book, Error> {
// The config options our preprocessor uses, deserialized from the book.toml that
// mdbook uses
#[derive(Deserialize)]
struct Config {
blow_up: bool,
}

// In testing we want to tell the preprocessor to blow up by setting a
// particular config value
if let Some(nop_cfg) = ctx.config.get_preprocessor(self.name()) {
if nop_cfg.contains_key("blow-up") {
let nop_cfg: Option<Config> = ctx.config.get_preprocessor_deserialized(self.name())?;
if let Some(nop_cfg) = nop_cfg {
if nop_cfg.blow_up {
anyhow::bail!("Boom!!1!");
}
}
Expand Down
22 changes: 22 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,17 +240,39 @@ impl Config {
}

/// Get the table associated with a particular renderer.
#[deprecated = "prefer get_renderer_deserialized over get_renderer"]
pub fn get_renderer<I: AsRef<str>>(&self, index: I) -> Option<&Table> {
let key = format!("output.{}", index.as_ref());
self.get(&key).and_then(Value::as_table)
}

/// Convenience function to fetch the renderer section from the config and deserialize it
/// into some arbitrary type.
pub fn get_renderer_deserialized<'de, T: Deserialize<'de>, I: AsRef<str>>(
&self,
index: I,
) -> Result<Option<T>> {
let key = format!("output.{}", index.as_ref());
self.get_deserialized_opt(key)
}

/// Get the table associated with a particular preprocessor.
#[deprecated = "prefer get_preprocessor_deserialized over get_preprocessor"]
pub fn get_preprocessor<I: AsRef<str>>(&self, index: I) -> Option<&Table> {
let key = format!("preprocessor.{}", index.as_ref());
self.get(&key).and_then(Value::as_table)
}

/// Convenience function to fetch the preprocessor section from the config and deserialize it
/// into some arbitrary type.
pub fn get_preprocessor_deserialized<'de, T: Deserialize<'de>, I: AsRef<str>>(
&self,
index: I,
) -> Result<Option<T>> {
let key = format!("preprocessor.{}", index.as_ref());
self.get_deserialized_opt(key)
}

fn from_legacy(mut table: Value) -> Config {
let mut cfg = Config::default();

Expand Down
2 changes: 1 addition & 1 deletion tests/custom_preprocessors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ fn ask_the_preprocessor_to_blow_up() {
md.with_preprocessor(example());

md.config
.set("preprocessor.nop-preprocessor.blow-up", true)
.set("preprocessor.nop-preprocessor.blow_up", true)
.unwrap();

let got = md.build();
Expand Down