diff --git a/CHANGELOG.md b/CHANGELOG.md index 2520b588..d422cca5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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)) @@ -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 \ No newline at end of file +Initial release diff --git a/src/ser/mod.rs b/src/ser/mod.rs index 0f85ec4c..3426a137 100644 --- a/src/ser/mod.rs +++ b/src/ser/mod.rs @@ -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) } @@ -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) } @@ -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")) } @@ -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")) } @@ -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, @@ -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 @@ -201,6 +213,10 @@ fn default_indentor() -> String { " ".to_string() } +fn default_struct_names() -> bool { + false +} + fn default_separate_tuple_members() -> bool { false } @@ -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(), @@ -231,7 +248,6 @@ impl Default for PrettyConfig { pub struct Serializer { output: W, pretty: Option<(PrettyConfig, Pretty)>, - struct_names: bool, is_empty: Option, } @@ -239,7 +255,7 @@ impl Serializer { /// 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, struct_names: bool) -> Result { + 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)]")?; @@ -257,7 +273,6 @@ impl Serializer { }, ) }), - struct_names, is_empty: None, }) } @@ -349,6 +364,13 @@ impl Serializer { 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 { @@ -480,7 +502,7 @@ impl<'a, W: io::Write> ser::Serializer for &'a mut Serializer { } fn serialize_unit_struct(self, name: &'static str) -> Result<()> { - if self.struct_names { + if self.struct_names() { self.write_identifier(name)?; Ok(()) @@ -499,7 +521,7 @@ impl<'a, W: io::Write> ser::Serializer for &'a mut Serializer { where T: ?Sized + Serialize, { - if self.struct_names { + if self.struct_names() { self.write_identifier(name)?; } @@ -567,7 +589,7 @@ impl<'a, W: io::Write> ser::Serializer for &'a mut Serializer { name: &'static str, len: usize, ) -> Result { - if self.struct_names { + if self.struct_names() { self.write_identifier(name)?; } @@ -612,7 +634,7 @@ impl<'a, W: io::Write> ser::Serializer for &'a mut Serializer { } fn serialize_struct(self, name: &'static str, len: usize) -> Result { - if self.struct_names { + if self.struct_names() { self.write_identifier(name)?; } self.output.write_all(b"(")?; diff --git a/tests/to_string_pretty.rs b/tests/to_string_pretty.rs new file mode 100644 index 00000000..9379e1db --- /dev/null +++ b/tests/to_string_pretty.rs @@ -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())); +}