Skip to content

Commit 3c2b8cd

Browse files
authored
Merge pull request #1539 from joshrotenberg/report_config_errors
Report book.toml parse error when invalid fields are found
2 parents 3db0c0b + 6b0b42e commit 3c2b8cd

File tree

1 file changed

+61
-4
lines changed

1 file changed

+61
-4
lines changed

src/config.rs

Lines changed: 61 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,7 @@ impl Default for Config {
294294
}
295295
}
296296
}
297+
297298
impl<'de> Deserialize<'de> for Config {
298299
fn deserialize<D: Deserializer<'de>>(de: D) -> std::result::Result<Self, D::Error> {
299300
let raw = Value::deserialize(de)?;
@@ -310,10 +311,10 @@ impl<'de> Deserialize<'de> for Config {
310311
return Ok(Config::from_legacy(raw));
311312
}
312313

314+
use serde::de::Error;
313315
let mut table = match raw {
314316
Value::Table(t) => t,
315317
_ => {
316-
use serde::de::Error;
317318
return Err(D::Error::custom(
318319
"A config file should always be a toml table",
319320
));
@@ -322,17 +323,20 @@ impl<'de> Deserialize<'de> for Config {
322323

323324
let book: BookConfig = table
324325
.remove("book")
325-
.and_then(|value| value.try_into().ok())
326+
.map(|book| book.try_into().map_err(D::Error::custom))
327+
.transpose()?
326328
.unwrap_or_default();
327329

328330
let build: BuildConfig = table
329331
.remove("build")
330-
.and_then(|value| value.try_into().ok())
332+
.map(|build| build.try_into().map_err(D::Error::custom))
333+
.transpose()?
331334
.unwrap_or_default();
332335

333336
let rust: RustConfig = table
334337
.remove("rust")
335-
.and_then(|value| value.try_into().ok())
338+
.map(|rust| rust.try_into().map_err(D::Error::custom))
339+
.transpose()?
336340
.unwrap_or_default();
337341

338342
Ok(Config {
@@ -1070,4 +1074,57 @@ mod tests {
10701074
assert_eq!(html_config.input_404, Some("missing.md".to_string()));
10711075
assert_eq!(&get_404_output_file(&html_config.input_404), "missing.html");
10721076
}
1077+
1078+
#[test]
1079+
#[should_panic(expected = "Invalid configuration file")]
1080+
fn invalid_language_type_error() {
1081+
let src = r#"
1082+
[book]
1083+
title = "mdBook Documentation"
1084+
language = ["en", "pt-br"]
1085+
description = "Create book from markdown files. Like Gitbook but implemented in Rust"
1086+
authors = ["Mathieu David"]
1087+
src = "./source"
1088+
"#;
1089+
1090+
Config::from_str(src).unwrap();
1091+
}
1092+
1093+
#[test]
1094+
#[should_panic(expected = "Invalid configuration file")]
1095+
fn invalid_title_type() {
1096+
let src = r#"
1097+
[book]
1098+
title = 20
1099+
language = "en"
1100+
description = "Create book from markdown files. Like Gitbook but implemented in Rust"
1101+
authors = ["Mathieu David"]
1102+
src = "./source"
1103+
"#;
1104+
1105+
Config::from_str(src).unwrap();
1106+
}
1107+
1108+
#[test]
1109+
#[should_panic(expected = "Invalid configuration file")]
1110+
fn invalid_build_dir_type() {
1111+
let src = r#"
1112+
[build]
1113+
build-dir = 99
1114+
create-missing = false
1115+
"#;
1116+
1117+
Config::from_str(src).unwrap();
1118+
}
1119+
1120+
#[test]
1121+
#[should_panic(expected = "Invalid configuration file")]
1122+
fn invalid_rust_edition() {
1123+
let src = r#"
1124+
[rust]
1125+
edition = "1999"
1126+
"#;
1127+
1128+
Config::from_str(src).unwrap();
1129+
}
10731130
}

0 commit comments

Comments
 (0)