Skip to content

Commit 78b5dfd

Browse files
committed
disincentivize usage of functions that expose toml::Table in Config
1 parent ec996d3 commit 78b5dfd

File tree

3 files changed

+34
-3
lines changed

3 files changed

+34
-3
lines changed

examples/nop-preprocessor.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ fn handle_supports(pre: &dyn Preprocessor, sub_args: &ArgMatches) -> ! {
7171
/// in your main `lib.rs` file.
7272
mod nop_lib {
7373
use super::*;
74+
use serde::Deserialize;
7475

7576
/// A no-op preprocessor.
7677
pub struct Nop;
@@ -87,10 +88,18 @@ mod nop_lib {
8788
}
8889

8990
fn run(&self, ctx: &PreprocessorContext, book: Book) -> Result<Book, Error> {
91+
// The config options our preprocessor uses, deserialized from the book.toml that
92+
// mdbook uses
93+
#[derive(Deserialize)]
94+
struct Config {
95+
blow_up: bool,
96+
}
97+
9098
// In testing we want to tell the preprocessor to blow up by setting a
9199
// particular config value
92-
if let Some(nop_cfg) = ctx.config.get_preprocessor(self.name()) {
93-
if nop_cfg.contains_key("blow-up") {
100+
let nop_cfg: Option<Config> = ctx.config.get_preprocessor_deserialized(self.name())?;
101+
if let Some(nop_cfg) = nop_cfg {
102+
if nop_cfg.blow_up {
94103
anyhow::bail!("Boom!!1!");
95104
}
96105
}

src/config.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,17 +240,39 @@ impl Config {
240240
}
241241

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

249+
/// Convenience function to fetch the renderer section from the config and deserialize it
250+
/// into some arbitrary type.
251+
pub fn get_renderer_deserialized<'de, T: Deserialize<'de>, I: AsRef<str>>(
252+
&self,
253+
index: I,
254+
) -> Result<Option<T>> {
255+
let key = format!("output.{}", index.as_ref());
256+
self.get_deserialized_opt(key)
257+
}
258+
248259
/// Get the table associated with a particular preprocessor.
260+
#[deprecated = "prefer get_preprocessor_deserialized over get_preprocessor"]
249261
pub fn get_preprocessor<I: AsRef<str>>(&self, index: I) -> Option<&Table> {
250262
let key = format!("preprocessor.{}", index.as_ref());
251263
self.get(&key).and_then(Value::as_table)
252264
}
253265

266+
/// Convenience function to fetch the preprocessor section from the config and deserialize it
267+
/// into some arbitrary type.
268+
pub fn get_preprocessor_deserialized<'de, T: Deserialize<'de>, I: AsRef<str>>(
269+
&self,
270+
index: I,
271+
) -> Result<Option<T>> {
272+
let key = format!("preprocessor.{}", index.as_ref());
273+
self.get_deserialized_opt(key)
274+
}
275+
254276
fn from_legacy(mut table: Value) -> Config {
255277
let mut cfg = Config::default();
256278

tests/custom_preprocessors.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ fn ask_the_preprocessor_to_blow_up() {
3737
md.with_preprocessor(example());
3838

3939
md.config
40-
.set("preprocessor.nop-preprocessor.blow-up", true)
40+
.set("preprocessor.nop-preprocessor.blow_up", true)
4141
.unwrap();
4242

4343
let got = md.build();

0 commit comments

Comments
 (0)