Skip to content

Commit 9a97f0a

Browse files
authored
Fix init creating empty [rust] table. (#1233)
1 parent bc23d08 commit 9a97f0a

File tree

2 files changed

+13
-11
lines changed

2 files changed

+13
-11
lines changed

src/config.rs

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -346,21 +346,17 @@ impl<'de> Deserialize<'de> for Config {
346346

347347
impl Serialize for Config {
348348
fn serialize<S: Serializer>(&self, s: S) -> std::result::Result<S::Ok, S::Error> {
349-
use serde::ser::Error;
350349
// TODO: This should probably be removed and use a derive instead.
351-
352350
let mut table = self.rest.clone();
353351

354-
let book_config = match Value::try_from(self.book.clone()) {
355-
Ok(cfg) => cfg,
356-
Err(_) => {
357-
return Err(S::Error::custom("Unable to serialize the BookConfig"));
358-
}
359-
};
360-
let rust_config = Value::try_from(&self.rust).expect("should always be serializable");
361-
352+
let book_config = Value::try_from(&self.book).expect("should always be serializable");
362353
table.insert("book", book_config);
363-
table.insert("rust", rust_config);
354+
355+
if self.rust != RustConfig::default() {
356+
let rust_config = Value::try_from(&self.rust).expect("should always be serializable");
357+
table.insert("rust", rust_config);
358+
}
359+
364360
table.serialize(s)
365361
}
366362
}

tests/init.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@ fn base_mdbook_init_should_create_default_content() {
2424
println!("{}", target.display());
2525
assert!(target.exists(), "{} doesn't exist", file);
2626
}
27+
28+
let contents = fs::read_to_string(temp.path().join("book.toml")).unwrap();
29+
assert_eq!(
30+
contents,
31+
"[book]\nauthors = []\nlanguage = \"en\"\nmultilingual = false\nsrc = \"src\"\n"
32+
);
2733
}
2834

2935
/// Run `mdbook init` in a directory containing a SUMMARY.md should create the

0 commit comments

Comments
 (0)