Fix init creating empty `[rust]` table. (#1233)

This commit is contained in:
Eric Huss 2020-06-22 07:36:37 -07:00 committed by GitHub
parent bc23d08fa5
commit 9a97f0a096
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 11 deletions

View File

@ -346,21 +346,17 @@ impl<'de> Deserialize<'de> for Config {
impl Serialize for Config { impl Serialize for Config {
fn serialize<S: Serializer>(&self, s: S) -> std::result::Result<S::Ok, S::Error> { fn serialize<S: Serializer>(&self, s: S) -> std::result::Result<S::Ok, S::Error> {
use serde::ser::Error;
// TODO: This should probably be removed and use a derive instead. // TODO: This should probably be removed and use a derive instead.
let mut table = self.rest.clone(); let mut table = self.rest.clone();
let book_config = match Value::try_from(self.book.clone()) { let book_config = Value::try_from(&self.book).expect("should always be serializable");
Ok(cfg) => cfg,
Err(_) => {
return Err(S::Error::custom("Unable to serialize the BookConfig"));
}
};
let rust_config = Value::try_from(&self.rust).expect("should always be serializable");
table.insert("book", book_config); table.insert("book", book_config);
if self.rust != RustConfig::default() {
let rust_config = Value::try_from(&self.rust).expect("should always be serializable");
table.insert("rust", rust_config); table.insert("rust", rust_config);
}
table.serialize(s) table.serialize(s)
} }
} }

View File

@ -24,6 +24,12 @@ fn base_mdbook_init_should_create_default_content() {
println!("{}", target.display()); println!("{}", target.display());
assert!(target.exists(), "{} doesn't exist", file); assert!(target.exists(), "{} doesn't exist", file);
} }
let contents = fs::read_to_string(temp.path().join("book.toml")).unwrap();
assert_eq!(
contents,
"[book]\nauthors = []\nlanguage = \"en\"\nmultilingual = false\nsrc = \"src\"\n"
);
} }
/// Run `mdbook init` in a directory containing a SUMMARY.md should create the /// Run `mdbook init` in a directory containing a SUMMARY.md should create the