From bb33830799a1e27ad704818d85cf857864d1c587 Mon Sep 17 00:00:00 2001 From: Momo Langenstein Date: Thu, 4 Nov 2021 18:28:03 +0000 Subject: [PATCH 1/2] Add output_extensions option to PrettyConfig --- src/ser/mod.rs | 49 ++++++++++++++++++++++++++++++++++--------------- 1 file changed, 34 insertions(+), 15 deletions(-) diff --git a/src/ser/mod.rs b/src/ser/mod.rs index 984881e66..5c0274959 100644 --- a/src/ser/mod.rs +++ b/src/ser/mod.rs @@ -100,6 +100,8 @@ pub struct PrettyConfig { /// Enable extensions. Only configures 'implicit_some', /// 'unwrap_newtypes', and 'unwrap_variant_newtypes' for now. pub extensions: Extensions, + #[serde(default = "default_output_extensions")] + pub output_extensions: bool, /// Private field to ensure adding a field is non-breaking. #[serde(skip)] _future_proof: (), @@ -191,6 +193,16 @@ impl PrettyConfig { self } + + /// Configures whether the extensions are included in the output as + /// `#![enable(..)]` attributes + /// + /// Default: `true` + pub fn output_extensions(mut self, output_extensions: bool) -> Self { + self.output_extensions = output_extensions; + + self + } } fn default_depth_limit() -> usize { @@ -226,6 +238,10 @@ fn default_enumerate_arrays() -> bool { false } +fn default_output_extensions() -> bool { + true +} + impl Default for PrettyConfig { fn default() -> Self { PrettyConfig { @@ -237,6 +253,7 @@ impl Default for PrettyConfig { enumerate_arrays: default_enumerate_arrays(), extensions: Extensions::default(), decimal_floats: default_decimal_floats(), + output_extensions: default_output_extensions(), _future_proof: (), } } @@ -259,21 +276,23 @@ impl Serializer { /// Most of the time you can just use `to_string` or `to_string_pretty`. pub fn new(mut writer: W, config: Option) -> Result { if let Some(conf) = &config { - if conf.extensions.contains(Extensions::IMPLICIT_SOME) { - writer.write_all(b"#![enable(implicit_some)]")?; - writer.write_all(conf.new_line.as_bytes())?; - }; - if conf.extensions.contains(Extensions::UNWRAP_NEWTYPES) { - writer.write_all(b"#![enable(unwrap_newtypes)]")?; - writer.write_all(conf.new_line.as_bytes())?; - }; - if conf - .extensions - .contains(Extensions::UNWRAP_VARIANT_NEWTYPES) - { - writer.write_all(b"#![enable(unwrap_variant_newtypes)]")?; - writer.write_all(conf.new_line.as_bytes())?; - }; + if conf.output_extensions { + if conf.extensions.contains(Extensions::IMPLICIT_SOME) { + writer.write_all(b"#![enable(implicit_some)]")?; + writer.write_all(conf.new_line.as_bytes())?; + }; + if conf.extensions.contains(Extensions::UNWRAP_NEWTYPES) { + writer.write_all(b"#![enable(unwrap_newtypes)]")?; + writer.write_all(conf.new_line.as_bytes())?; + }; + if conf + .extensions + .contains(Extensions::UNWRAP_VARIANT_NEWTYPES) + { + writer.write_all(b"#![enable(unwrap_variant_newtypes)]")?; + writer.write_all(conf.new_line.as_bytes())?; + }; + } }; Ok(Serializer { output: writer, From f401108296fe1a569d0b7700a9a952bd42d01bc9 Mon Sep 17 00:00:00 2001 From: Momo Langenstein Date: Wed, 17 Nov 2021 14:00:56 +0000 Subject: [PATCH 2/2] Added changelog entry --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index a05c268f1..19f428ac3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Fix newtype variant unwrapping around enum, seq and map ([#331](https://github.com/ron-rs/ron/pull/331)) - Implement `unwrap_newtypes` extension during serialization ([#333](https://github.com/ron-rs/ron/pull/333)) - Implement `unwrap_variant_newtypes` extension during serialization ([#336](https://github.com/ron-rs/ron/pull/336)) +- Add `output_extensions` option to `Pretty Config` ([#339](https://github.com/ron-rs/ron/pull/339)) ## [0.7.0] - 2021-10-22