From ae1a8c362c5cee6b0aadc6ba726660d7fc737eb7 Mon Sep 17 00:00:00 2001 From: Eric Huss Date: Sat, 16 Aug 2025 15:21:46 -0700 Subject: [PATCH] Don't serialize the default for `book.src` This changes the serialization so that `book.src` is not serialized if it is the default. This removes the somewhat pointless `src = "src"` which shows up in the default `mdbook init` output. Deserialization should still default to `"src"`. --- crates/mdbook-core/src/config.rs | 6 ++++++ tests/testsuite/init.rs | 3 --- tests/testsuite/renderer.rs | 1 - 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/crates/mdbook-core/src/config.rs b/crates/mdbook-core/src/config.rs index d5746971b2..1a49f02e53 100644 --- a/crates/mdbook-core/src/config.rs +++ b/crates/mdbook-core/src/config.rs @@ -297,6 +297,7 @@ pub struct BookConfig { /// An optional description for the book. pub description: Option, /// Location of the book source relative to the book's root directory. + #[serde(skip_serializing_if = "is_default_src")] pub src: PathBuf, /// The main language of the book. pub language: Option, @@ -305,6 +306,11 @@ pub struct BookConfig { pub text_direction: Option, } +/// Helper for serde serialization. +fn is_default_src(src: &PathBuf) -> bool { + src == Path::new("src") +} + impl Default for BookConfig { fn default() -> BookConfig { BookConfig { diff --git a/tests/testsuite/init.rs b/tests/testsuite/init.rs index bf225a4da6..852f1a2fb3 100644 --- a/tests/testsuite/init.rs +++ b/tests/testsuite/init.rs @@ -28,7 +28,6 @@ All done, no errors... str![[r#" [book] authors = [] -src = "src" language = "en" "#]], @@ -94,7 +93,6 @@ All done, no errors... str![[r#" [book] authors = [] -src = "src" language = "en" "#]], @@ -128,7 +126,6 @@ All done, no errors... [book] title = "Example title" authors = [] -src = "src" language = "en" "#]], diff --git a/tests/testsuite/renderer.rs b/tests/testsuite/renderer.rs index 9f2ee0100a..a04ff0e3ec 100644 --- a/tests/testsuite/renderer.rs +++ b/tests/testsuite/renderer.rs @@ -188,7 +188,6 @@ fn backends_receive_render_context_via_stdin() { "authors": [], "description": null, "language": "en", - "src": "src", "text-direction": null, "title": null },