Skip to content

Commit

Permalink
Move option to emit struct names to PrettyConfig (#329)
Browse files Browse the repository at this point in the history
* Move struct_names from Serializer to PrettyConfig

* Update changelog
  • Loading branch information
cswinter authored Nov 2, 2021
1 parent 4519973 commit 62db940
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 12 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## Unreleased
- Add `struct_names` option to `PrettyConfig`

## [0.7.0] - 2021-10-22

- Add `unwrap_variant_newtypes` extension ([#319](https://github.com/ron-rs/ron/pull/319))
Expand Down Expand Up @@ -178,4 +181,4 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Add roundtrip tests ([#24](https://github.com/ron-rs/ron/pull/24))

## [0.0.1] - 2015-07-30
Initial release
Initial release
44 changes: 33 additions & 11 deletions src/ser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ where
W: io::Write,
T: ?Sized + Serialize,
{
let mut s = Serializer::new(writer, None, false)?;
let mut s = Serializer::new(writer, None)?;
value.serialize(&mut s)
}

Expand All @@ -27,7 +27,7 @@ where
W: io::Write,
T: ?Sized + Serialize,
{
let mut s = Serializer::new(writer, Some(config), false)?;
let mut s = Serializer::new(writer, Some(config))?;
value.serialize(&mut s)
}

Expand All @@ -40,7 +40,7 @@ where
T: ?Sized + Serialize,
{
let buf = Vec::new();
let mut s = Serializer::new(buf, None, false)?;
let mut s = Serializer::new(buf, None)?;
value.serialize(&mut s)?;
Ok(String::from_utf8(s.output).expect("Ron should be utf-8"))
}
Expand All @@ -51,7 +51,7 @@ where
T: ?Sized + Serialize,
{
let buf = Vec::new();
let mut s = Serializer::new(buf, Some(config), false)?;
let mut s = Serializer::new(buf, Some(config))?;
value.serialize(&mut s)?;
Ok(String::from_utf8(s.output).expect("Ron should be utf-8"))
}
Expand Down Expand Up @@ -85,6 +85,9 @@ pub struct PrettyConfig {
/// Indentation string
#[serde(default = "default_indentor")]
pub indentor: String,
// Whether to emit struct names
#[serde(default = "default_struct_names")]
pub struct_names: bool,
/// Separate tuple members with indentation
#[serde(default = "default_separate_tuple_members")]
pub separate_tuple_members: bool,
Expand Down Expand Up @@ -137,6 +140,15 @@ impl PrettyConfig {
self
}

/// Configures whether to emit struct names.
///
/// Default: `false`
pub fn struct_names(mut self, struct_names: bool) -> Self {
self.struct_names = struct_names;

self
}

/// Configures whether tuples are single- or multi-line.
/// If set to `true`, tuples will have their fields indented and in new
/// lines. If set to `false`, tuples will be serialized without any
Expand Down Expand Up @@ -201,6 +213,10 @@ fn default_indentor() -> String {
" ".to_string()
}

fn default_struct_names() -> bool {
false
}

fn default_separate_tuple_members() -> bool {
false
}
Expand All @@ -215,6 +231,7 @@ impl Default for PrettyConfig {
depth_limit: default_depth_limit(),
new_line: default_new_line(),
indentor: default_indentor(),
struct_names: default_struct_names(),
separate_tuple_members: default_separate_tuple_members(),
enumerate_arrays: default_enumerate_arrays(),
extensions: Extensions::default(),
Expand All @@ -231,15 +248,14 @@ impl Default for PrettyConfig {
pub struct Serializer<W: io::Write> {
output: W,
pretty: Option<(PrettyConfig, Pretty)>,
struct_names: bool,
is_empty: Option<bool>,
}

impl<W: io::Write> Serializer<W> {
/// Creates a new `Serializer`.
///
/// Most of the time you can just use `to_string` or `to_string_pretty`.
pub fn new(mut writer: W, config: Option<PrettyConfig>, struct_names: bool) -> Result<Self> {
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)]")?;
Expand All @@ -257,7 +273,6 @@ impl<W: io::Write> Serializer<W> {
},
)
}),
struct_names,
is_empty: None,
})
}
Expand Down Expand Up @@ -349,6 +364,13 @@ impl<W: io::Write> Serializer<W> {
self.output.write_all(name.as_bytes())?;
Ok(())
}

fn struct_names(&self) -> bool {
self.pretty
.as_ref()
.map(|(pc, _)| pc.struct_names)
.unwrap_or(false)
}
}

impl<'a, W: io::Write> ser::Serializer for &'a mut Serializer<W> {
Expand Down Expand Up @@ -480,7 +502,7 @@ impl<'a, W: io::Write> ser::Serializer for &'a mut Serializer<W> {
}

fn serialize_unit_struct(self, name: &'static str) -> Result<()> {
if self.struct_names {
if self.struct_names() {
self.write_identifier(name)?;

Ok(())
Expand All @@ -499,7 +521,7 @@ impl<'a, W: io::Write> ser::Serializer for &'a mut Serializer<W> {
where
T: ?Sized + Serialize,
{
if self.struct_names {
if self.struct_names() {
self.write_identifier(name)?;
}

Expand Down Expand Up @@ -567,7 +589,7 @@ impl<'a, W: io::Write> ser::Serializer for &'a mut Serializer<W> {
name: &'static str,
len: usize,
) -> Result<Self::SerializeTupleStruct> {
if self.struct_names {
if self.struct_names() {
self.write_identifier(name)?;
}

Expand Down Expand Up @@ -612,7 +634,7 @@ impl<'a, W: io::Write> ser::Serializer for &'a mut Serializer<W> {
}

fn serialize_struct(self, name: &'static str, len: usize) -> Result<Self::SerializeStruct> {
if self.struct_names {
if self.struct_names() {
self.write_identifier(name)?;
}
self.output.write_all(b"(")?;
Expand Down
21 changes: 21 additions & 0 deletions tests/to_string_pretty.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
use ron::ser::{to_string_pretty, PrettyConfig};
use ron::to_string;
use serde::{Deserialize, Serialize};

#[derive(Debug, PartialEq, Deserialize, Serialize)]
struct Point {
x: f64,
y: f64,
}

#[test]
fn test_struct_names() {
let value = Point { x: 1.0, y: 2.0 };
let struct_name = to_string_pretty(&value, PrettyConfig::default().struct_names(true));
assert_eq!(
struct_name,
Ok("Point(\n x: 1,\n y: 2,\n)".to_string())
);
let no_struct_name = to_string(&value);
assert_eq!(no_struct_name, Ok("(x:1,y:2)".to_string()));
}

0 comments on commit 62db940

Please sign in to comment.