From 457eefa3c7bc2522f6f1bac33459aff792821cbb Mon Sep 17 00:00:00 2001 From: Gabor Szabo Date: Thu, 6 Mar 2025 11:03:26 +0200 Subject: [PATCH 1/2] deny_unknown_fields in the configuration file To avoid typos and make the config reading stricter. --- src/config.rs | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/config.rs b/src/config.rs index 905020a39c..91915776ba 100644 --- a/src/config.rs +++ b/src/config.rs @@ -397,7 +397,7 @@ fn is_legacy_format(table: &Value) -> bool { /// Configuration options which are specific to the book and required for /// loading it from disk. #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] -#[serde(default, rename_all = "kebab-case")] +#[serde(default, rename_all = "kebab-case", deny_unknown_fields)] pub struct BookConfig { /// The book's title. pub title: Option, @@ -894,6 +894,17 @@ mod tests { assert_eq!(got.html_config().unwrap(), html_should_be); } + #[test] + #[should_panic(expected = "Invalid configuration file")] + fn fail_on_invalid_config_field() { + let src = r#" + [book] + password = Secret + "#; + + Config::from_str(src).unwrap(); + } + #[test] fn disable_runnable() { let src = r#" From 607823d6c05b84c98825eef484d5004be4083d1b Mon Sep 17 00:00:00 2001 From: Gabor Szabo Date: Thu, 6 Mar 2025 17:30:55 +0200 Subject: [PATCH 2/2] make the toml valid so it will really test the extra field --- src/config.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/config.rs b/src/config.rs index 91915776ba..c042e60beb 100644 --- a/src/config.rs +++ b/src/config.rs @@ -899,7 +899,7 @@ mod tests { fn fail_on_invalid_config_field() { let src = r#" [book] - password = Secret + password = "Secret" "#; Config::from_str(src).unwrap();