From d8ad68c9471cfd46d5f69fe0d17be078d4163858 Mon Sep 17 00:00:00 2001 From: ISSOtm Date: Mon, 27 Sep 2021 21:28:21 +0200 Subject: [PATCH] Produce an error if `before` or `after` field is not a table --- src/book/mod.rs | 40 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 38 insertions(+), 2 deletions(-) diff --git a/src/book/mod.rs b/src/book/mod.rs index 6baac5ca..73ced4d5 100644 --- a/src/book/mod.rs +++ b/src/book/mod.rs @@ -401,7 +401,13 @@ fn determine_preprocessors(config: &Config) -> Result> || preprocessor_table.contains_key(name) }; - if let Some(before) = table.get("before").and_then(Value::as_array) { + if let Some(before) = table.get("before") { + let before = before.as_array().ok_or_else(|| { + Error::msg(format!( + "Expected preprocessor.{}.before to be an array", + name + )) + })?; for after in before { let after = after.as_str().ok_or_else(|| { Error::msg(format!( @@ -416,7 +422,13 @@ fn determine_preprocessors(config: &Config) -> Result> } } - if let Some(after) = table.get("after").and_then(Value::as_array) { + if let Some(after) = table.get("after") { + let after = after.as_array().ok_or_else(|| { + Error::msg(format!( + "Expected preprocessor.{}.after to be an array", + name + )) + })?; for before in after { let before = before.as_str().ok_or_else(|| { Error::msg(format!( @@ -630,6 +642,30 @@ mod tests { assert_eq!(random, "python random.py"); } + #[test] + fn preprocessor_before_must_be_array() { + let cfg_str = r#" + [preprocessor.random] + before = 0 + "#; + + let cfg = Config::from_str(cfg_str).unwrap(); + + assert!(determine_preprocessors(&cfg).is_err()); + } + + #[test] + fn preprocessor_after_must_be_array() { + let cfg_str = r#" + [preprocessor.random] + after = 0 + "#; + + let cfg = Config::from_str(cfg_str).unwrap(); + + assert!(determine_preprocessors(&cfg).is_err()); + } + #[test] fn preprocessor_order_is_honored() { let cfg_str = r#"