From 1bc2ebd775a0e88f5f58b7040215fb0daee41a1a Mon Sep 17 00:00:00 2001 From: Gabor Szabo Date: Fri, 28 Mar 2025 10:56:41 +0300 Subject: [PATCH 1/2] warn on invalid fields in root of book.toml --- src/config.rs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/config.rs b/src/config.rs index 905020a39c..f4eab9688c 100644 --- a/src/config.rs +++ b/src/config.rs @@ -312,6 +312,8 @@ impl<'de> serde::Deserialize<'de> for Config { return Ok(Config::from_legacy(raw)); } + warn_on_invalid_fields(&raw); + use serde::de::Error; let mut table = match raw { Value::Table(t) => t, @@ -376,6 +378,20 @@ fn parse_env(key: &str) -> Option { .map(|key| key.to_lowercase().replace("__", ".").replace('_', "-")) } +fn warn_on_invalid_fields(table: &Value) { + let valid_items = ["book", "build", "rust", "output", "preprocessor"]; + + if let Some(table) = table.as_table() { + for item in table.keys() { + if !valid_items.contains(&item.as_str()) { + warn!("Invalid field {:?} in book.toml", &item); + } + } + } else { + warn!("Invalid format in book.toml"); + } +} + fn is_legacy_format(table: &Value) -> bool { let legacy_items = [ "title", From 97f19486817726965e6c4b9b9839dae0b5893f30 Mon Sep 17 00:00:00 2001 From: Eric Huss Date: Thu, 3 Apr 2025 11:17:25 -0700 Subject: [PATCH 2/2] Make the unexpected case explicit that it is an internal error The current code has the `else` clause as unreachable, and the text it has isn't clear that is the case. --- src/config.rs | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/src/config.rs b/src/config.rs index f4eab9688c..db159a45ad 100644 --- a/src/config.rs +++ b/src/config.rs @@ -381,14 +381,11 @@ fn parse_env(key: &str) -> Option { fn warn_on_invalid_fields(table: &Value) { let valid_items = ["book", "build", "rust", "output", "preprocessor"]; - if let Some(table) = table.as_table() { - for item in table.keys() { - if !valid_items.contains(&item.as_str()) { - warn!("Invalid field {:?} in book.toml", &item); - } + let table = table.as_table().expect("root must be a table"); + for item in table.keys() { + if !valid_items.contains(&item.as_str()) { + warn!("Invalid field {:?} in book.toml", &item); } - } else { - warn!("Invalid format in book.toml"); } }