From 431f3ab88ba283c5a6214c7dd62bf8b4a521dfe6 Mon Sep 17 00:00:00 2001 From: George Fischer Date: Tue, 23 May 2023 18:34:02 +0200 Subject: [PATCH 1/3] Make `Options` (De)serialisable with serde --- src/configuration.rs | 22 ++++++++++++++++++++++ src/util/line_ending.rs | 4 ++++ 2 files changed, 26 insertions(+) diff --git a/src/configuration.rs b/src/configuration.rs index fe5698bb..d59164b9 100644 --- a/src/configuration.rs +++ b/src/configuration.rs @@ -31,6 +31,11 @@ use alloc::{boxed::Box, fmt, string::String}; /// ``` #[allow(clippy::struct_excessive_bools)] #[derive(Clone, Debug, Eq, PartialEq)] +#[cfg_attr( + feature = "serde", + derive(serde::Serialize, serde::Deserialize), + serde(rename_all = "camelCase") +)] pub struct Constructs { /// Attention. /// @@ -467,6 +472,11 @@ impl Constructs { /// ``` #[allow(clippy::struct_excessive_bools)] #[derive(Clone, Debug, Default)] +#[cfg_attr( + feature = "serde", + derive(serde::Serialize, serde::Deserialize), + serde(rename_all = "camelCase") +)] pub struct CompileOptions { /// Whether to allow (dangerous) HTML. /// @@ -930,6 +940,11 @@ impl CompileOptions { /// # } /// ``` #[allow(clippy::struct_excessive_bools)] +#[cfg_attr( + feature = "serde", + derive(serde::Serialize, serde::Deserialize), + serde(rename_all = "camelCase") +)] pub struct ParseOptions { // Note: when adding fields, don’t forget to add them to `fmt::Debug` below. /// Which constructs to enable and disable. @@ -968,6 +983,7 @@ pub struct ParseOptions { /// # Ok(()) /// # } /// ``` + #[cfg_attr(feature = "serde", serde(default))] pub constructs: Constructs, /// Whether to support GFM strikethrough with a single tilde @@ -1020,6 +1036,7 @@ pub struct ParseOptions { /// # Ok(()) /// # } /// ``` + #[cfg_attr(feature = "serde", serde(default))] pub gfm_strikethrough_single_tilde: bool, /// Whether to support math (text) with a single dollar @@ -1079,6 +1096,7 @@ pub struct ParseOptions { /// # Ok(()) /// # } /// ``` + #[cfg_attr(feature = "serde", serde(default))] pub math_text_single_dollar: bool, /// Function to parse expressions with. @@ -1091,6 +1109,7 @@ pub struct ParseOptions { /// /// For an example that adds support for JavaScript with SWC, see /// `tests/test_utils/mod.rs`. + #[cfg_attr(feature = "serde", serde(skip))] pub mdx_expression_parse: Option>, /// Function to parse ESM with. @@ -1107,6 +1126,8 @@ pub struct ParseOptions { /// /// For an example that adds support for JavaScript with SWC, see /// `tests/test_utils/mod.rs`. + /// + #[cfg_attr(feature = "serde", serde(skip))] pub mdx_esm_parse: Option>, // Note: when adding fields, don’t forget to add them to `fmt::Debug` below. } @@ -1208,6 +1229,7 @@ impl ParseOptions { /// ``` #[allow(clippy::struct_excessive_bools)] #[derive(Debug, Default)] +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] pub struct Options { /// Configuration that describes how to parse from markdown. pub parse: ParseOptions, diff --git a/src/util/line_ending.rs b/src/util/line_ending.rs index be4d8a33..93068b33 100644 --- a/src/util/line_ending.rs +++ b/src/util/line_ending.rs @@ -16,6 +16,7 @@ use alloc::{str::FromStr, string::String}; /// # } /// ``` #[derive(Clone, Debug, Default, Eq, PartialEq)] +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] pub enum LineEnding { /// Both a carriage return (`\r`) and a line feed (`\n`). /// @@ -25,6 +26,7 @@ pub enum LineEnding { /// a␍␊ /// b /// ``` + #[cfg_attr(feature = "serde", serde(rename = "\r\n"))] CarriageReturnLineFeed, /// Sole carriage return (`\r`). /// @@ -34,6 +36,7 @@ pub enum LineEnding { /// a␍ /// b /// ``` + #[cfg_attr(feature = "serde", serde(rename = "\r"))] CarriageReturn, /// Sole line feed (`\n`). /// @@ -44,6 +47,7 @@ pub enum LineEnding { /// b /// ``` #[default] + #[cfg_attr(feature = "serde", serde(rename = "\n"))] LineFeed, } From f1fb7e524a866091b85864ab5c448424baf4a495 Mon Sep 17 00:00:00 2001 From: George Fischer Date: Tue, 23 May 2023 19:29:11 +0200 Subject: [PATCH 2/3] add `serde(default)` to structs with (De)serialise triaits --- src/configuration.rs | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/configuration.rs b/src/configuration.rs index d59164b9..57efa219 100644 --- a/src/configuration.rs +++ b/src/configuration.rs @@ -475,7 +475,7 @@ impl Constructs { #[cfg_attr( feature = "serde", derive(serde::Serialize, serde::Deserialize), - serde(rename_all = "camelCase") + serde(default, rename_all = "camelCase") )] pub struct CompileOptions { /// Whether to allow (dangerous) HTML. @@ -943,7 +943,7 @@ impl CompileOptions { #[cfg_attr( feature = "serde", derive(serde::Serialize, serde::Deserialize), - serde(rename_all = "camelCase") + serde(default, rename_all = "camelCase") )] pub struct ParseOptions { // Note: when adding fields, don’t forget to add them to `fmt::Debug` below. @@ -1229,7 +1229,11 @@ impl ParseOptions { /// ``` #[allow(clippy::struct_excessive_bools)] #[derive(Debug, Default)] -#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] +#[cfg_attr( + feature = "serde", + derive(serde::Serialize, serde::Deserialize), + serde(default) +)] pub struct Options { /// Configuration that describes how to parse from markdown. pub parse: ParseOptions, From 828b6f0da66b8a751d6901e064a8298820657e4e Mon Sep 17 00:00:00 2001 From: George Fischer Date: Fri, 26 May 2023 18:04:59 +0200 Subject: [PATCH 3/3] Adjust `Features` doc section to mention config in serde --- src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 584861ef..46a8e5c8 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -17,7 +17,7 @@ //! * **`default`** //! — nothing is enabled by default //! * **`serde`** -//! — enable serde to serialize the AST (includes `dep:serde`) +//! — enable serde to serialize the AST and configuration objects (includes `dep:serde`) //! * **`log`** //! — enable logging (includes `dep:log`); //! you can show logs with `RUST_LOG=debug`