From 8045e217c9ec48d06345e4f3d6bdfbed72dabb1b Mon Sep 17 00:00:00 2001 From: Yannick Seurin Date: Mon, 29 Apr 2024 22:53:11 +0200 Subject: [PATCH 1/2] remove serde kebab-case renaming --- src/types.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/types.rs b/src/types.rs index b5a8133..d6f758d 100644 --- a/src/types.rs +++ b/src/types.rs @@ -5,7 +5,6 @@ use std::str::FromStr; /// Book wide defaults that may be provided by the user. #[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize, Default)] -#[serde(rename_all = "kebab-case")] pub(crate) struct AdmonitionDefaults { #[serde(default)] pub(crate) title: Option, From 3f8bf86ac3204b5848523806ce9995d37d63f09c Mon Sep 17 00:00:00 2001 From: Tom Milligan Date: Sun, 19 May 2024 09:43:48 +0100 Subject: [PATCH 2/2] fix: allow css_id_prefix kebab cases for backcompat --- src/book_config.rs | 88 +++++++++++++++++++++++++++++++++++++++++++++- src/types.rs | 3 ++ 2 files changed, 90 insertions(+), 1 deletion(-) diff --git a/src/book_config.rs b/src/book_config.rs index b24e353..3080b1e 100644 --- a/src/book_config.rs +++ b/src/book_config.rs @@ -23,7 +23,7 @@ pub(crate) fn admonish_config_from_str(data: &str) -> Result { toml::from_str(data).context("Invalid mdbook-admonish configuration in book.toml") } -#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)] +#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize, Default)] pub(crate) struct Config { #[serde(default)] pub on_failure: OnFailure, @@ -86,3 +86,89 @@ impl Default for OnFailure { Self::Continue } } + +#[cfg(test)] +mod test { + use super::*; + use pretty_assertions::assert_eq; + + #[test] + fn empty_config_okay() -> Result<()> { + let actual = admonish_config_from_str("")?; + let expected = Config::default(); + assert_eq!(actual, expected); + Ok(()) + } + + #[test] + fn css_id_prefix_kebab_case_allowed() -> Result<()> { + let expected = Config { + default: AdmonitionDefaults { + css_id_prefix: Some("flam-".to_owned()), + ..Default::default() + }, + ..Default::default() + }; + + // Snake case okay + let actual = admonish_config_from_str(r#"default = { css_id_prefix = "flam-" }"#)?; + assert_eq!(actual, expected); + + // Kebab case back-compat okay + let actual = admonish_config_from_str(r#"default = { css-id-prefix = "flam-" }"#)?; + assert_eq!(actual, expected); + Ok(()) + } + + #[test] + fn full_config_roundtrip() -> Result<()> { + let input = Config { + default: AdmonitionDefaults { + css_id_prefix: Some("flam-".to_owned()), + collapsible: true, + title: Some("".to_owned()), + }, + assets_version: Some("1.1.1".to_owned()), + custom: vec![CustomDirective { + directive: "test-directive".to_owned(), + icon: PathBuf::from("/tmp/test-directive.svg"), + color: hex_color::HexColor::from((155, 79, 150)), + aliases: vec!["test-directive-alias-0".to_owned()], + title: Some("test-directive-title".to_owned()), + }], + on_failure: OnFailure::Bail, + renderer: HashMap::from([( + "test-mode".to_owned(), + RendererConfig { + render_mode: Some(RenderMode::Strip), + }, + )]), + }; + + let expected = r##"on_failure = "bail" +assets_version = "1.1.1" + +[default] +title = "" +collapsible = true +css_id_prefix = "flam-" + +[renderer.test-mode] +render_mode = "strip" + +[[custom]] +directive = "test-directive" +icon = "/tmp/test-directive.svg" +color = "#9B4F96" +aliases = ["test-directive-alias-0"] +title = "test-directive-title" +"##; + + let serialized = toml::to_string(&input)?; + assert_eq!(serialized, expected); + + let actual = admonish_config_from_str(&serialized)?; + assert_eq!(actual, input); + Ok(()) + } +} diff --git a/src/types.rs b/src/types.rs index d6f758d..fdf7dc2 100644 --- a/src/types.rs +++ b/src/types.rs @@ -13,6 +13,9 @@ pub(crate) struct AdmonitionDefaults { pub(crate) collapsible: bool, #[serde(default)] + // For backwards compatibility, we support this field with kebab-case style + // naming, even though this was introduced in error. + #[serde(alias = "css-id-prefix")] pub(crate) css_id_prefix: Option, }