Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add output_extensions option to PrettyConfig #339

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
49 changes: 34 additions & 15 deletions src/ser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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: (),
Expand Down Expand Up @@ -191,6 +193,16 @@ impl PrettyConfig {

self
}

/// Configures whether the extensions are included in the output as
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should describe the use-case here and what to look out for (enabling the same extensions when deserializing).

/// `#![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 {
Expand Down Expand Up @@ -226,6 +238,10 @@ fn default_enumerate_arrays() -> bool {
false
}

fn default_output_extensions() -> bool {
true
}

impl Default for PrettyConfig {
fn default() -> Self {
PrettyConfig {
Expand All @@ -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: (),
}
}
Expand All @@ -259,21 +276,23 @@ impl<W: io::Write> Serializer<W> {
/// Most of the time you can just use `to_string` or `to_string_pretty`.
pub fn new(mut writer: W, config: Option<PrettyConfig>) -> Result<Self> {
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,
Expand Down