Skip to content

Commit

Permalink
Replace PrettyConfig's Strings with Cow<'static, str>
Browse files Browse the repository at this point in the history
Signed-off-by: Coca <[email protected]>
  • Loading branch information
Coca162 committed Sep 12, 2024
1 parent abc60f5 commit fe6809f
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 41 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Breaking: Enforce that ron always writes valid UTF-8 ([#488](https://github.com/ron-rs/ron/pull/488))
- Add convenient `Value::from` impls ([#498](https://github.com/ron-rs/ron/pull/498))
- Add new extension `explicit_struct_names` which requires that struct names are included during deserialization ([#522](https://github.com/ron-rs/ron/pull/522))
- Breaking: Change `PrettyConfig` so that `new_line`, `indentor` and `separator` are all `Cow<'static, str>` instead of `String` ([#546](https://github.com/ron-rs/ron/pull/546))

### Format Changes

Expand Down
2 changes: 1 addition & 1 deletion fuzz/fuzz_targets/bench/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ impl From<ArbitraryPrettyConfig> for PrettyConfig {
fn from(arbitrary: ArbitraryPrettyConfig) -> Self {
Self::default()
.depth_limit((arbitrary.depth_limit % 16).into())
.indentor(String::from(" ")) // conserve some memory and time
.indentor(" ") // conserve some memory and time
.struct_names(arbitrary.struct_names)
.separate_tuple_members(arbitrary.separate_tuple_members)
.enumerate_arrays(arbitrary.enumerate_arrays)
Expand Down
30 changes: 15 additions & 15 deletions src/ser/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::fmt;
use std::{borrow::Cow, fmt};

use serde::{ser, ser::Serialize};
use serde_derive::{Deserialize, Serialize};
Expand Down Expand Up @@ -71,7 +71,7 @@ struct Pretty {
/// let my_config = PrettyConfig::new()
/// .depth_limit(4)
/// // definitely superior (okay, just joking)
/// .indentor("\t".to_owned());
/// .indentor("\t");
/// ```
#[allow(clippy::struct_excessive_bools)]
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
Expand All @@ -81,11 +81,11 @@ pub struct PrettyConfig {
/// Limit the pretty-ness up to the given depth.
pub depth_limit: usize,
/// New line string
pub new_line: String,
pub new_line: Cow<'static, str>,
/// Indentation string
pub indentor: String,
pub indentor: Cow<'static, str>,
/// Separator string
pub separator: String,
pub separator: Cow<'static, str>,
// Whether to emit struct names
pub struct_names: bool,
/// Separate tuple members with indentation
Expand Down Expand Up @@ -135,8 +135,8 @@ impl PrettyConfig {
///
/// Default: `\r\n` on Windows, `\n` otherwise
#[must_use]
pub fn new_line(mut self, new_line: String) -> Self {
self.new_line = new_line;
pub fn new_line(mut self, new_line: impl Into<Cow<'static, str>>) -> Self {
self.new_line = new_line.into();

self
}
Expand All @@ -145,8 +145,8 @@ impl PrettyConfig {
///
/// Default: 4 spaces
#[must_use]
pub fn indentor(mut self, indentor: String) -> Self {
self.indentor = indentor;
pub fn indentor(mut self, indentor: impl Into<Cow<'static, str>>) -> Self {
self.indentor = indentor.into();

self
}
Expand All @@ -155,8 +155,8 @@ impl PrettyConfig {
///
/// Default: 1 space
#[must_use]
pub fn separator(mut self, separator: String) -> Self {
self.separator = separator;
pub fn separator(mut self, separator: impl Into<Cow<'static, str>>) -> Self {
self.separator = separator.into();

self
}
Expand Down Expand Up @@ -344,12 +344,12 @@ impl Default for PrettyConfig {
PrettyConfig {
depth_limit: usize::MAX,
new_line: if cfg!(not(target_os = "windows")) {
String::from("\n")
Cow::Borrowed("\n")
} else {
String::from("\r\n") // GRCOV_EXCL_LINE
Cow::Borrowed("\r\n") // GRCOV_EXCL_LINE
},
indentor: String::from(" "),
separator: String::from(" "),
indentor: Cow::Borrowed(" "),
separator: Cow::Borrowed(" "),
struct_names: false,
separate_tuple_members: false,
enumerate_arrays: false,
Expand Down
2 changes: 1 addition & 1 deletion tests/147_empty_sets_serialisation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ fn empty_sets_arrays() {

let pretty = ron::ser::PrettyConfig::new()
.enumerate_arrays(true)
.new_line("\n".to_string());
.new_line("\n");
let serial = ron::ser::to_string_pretty(&value, pretty).unwrap();

println!("Serialized: {}", serial);
Expand Down
12 changes: 5 additions & 7 deletions tests/240_array_pretty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use ron::ser::{to_string_pretty, PrettyConfig};
fn small_array() {
let arr = &[(), (), ()][..];
assert_eq!(
to_string_pretty(&arr, PrettyConfig::new().new_line("\n".to_string())).unwrap(),
to_string_pretty(&arr, PrettyConfig::new().new_line("\n")).unwrap(),
"[
(),
(),
Expand All @@ -14,9 +14,7 @@ fn small_array() {
assert_eq!(
to_string_pretty(
&arr,
PrettyConfig::new()
.new_line("\n".to_string())
.compact_arrays(true)
PrettyConfig::new().new_line("\n").compact_arrays(true)
)
.unwrap(),
"[(), (), ()]"
Expand All @@ -25,9 +23,9 @@ fn small_array() {
to_string_pretty(
&arr,
PrettyConfig::new()
.new_line("\n".to_string())
.new_line("\n")
.compact_arrays(true)
.separator("".to_string())
.separator("")
)
.unwrap(),
"[(),(),()]"
Expand All @@ -36,7 +34,7 @@ fn small_array() {
to_string_pretty(
&vec![(1, 2), (3, 4)],
PrettyConfig::new()
.new_line("\n".to_string())
.new_line("\n")
.separate_tuple_members(true)
.compact_arrays(true)
)
Expand Down
2 changes: 1 addition & 1 deletion tests/depth_limit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ fn depth_limit() {
.depth_limit(1)
.separate_tuple_members(true)
.enumerate_arrays(true)
.new_line("\n".to_string());
.new_line("\n");
let s = ron::ser::to_string_pretty(&data, pretty);

assert_eq!(s, Ok(EXPECTED.to_string()));
Expand Down
2 changes: 1 addition & 1 deletion tests/preserve_sequence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ fn make_roundtrip(source: &str) -> String {
.depth_limit(3)
.separate_tuple_members(true)
.enumerate_arrays(true)
.new_line("\n".into());
.new_line("\n");
to_string_pretty(&config, pretty).expect("Serialization failed")
}

Expand Down
21 changes: 6 additions & 15 deletions tests/unicode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,31 +39,22 @@ fn test_file_invalid_unicode() {
#[test]
fn serialize_invalid_whitespace() {
assert_eq!(
ron::ser::to_string_pretty(
&42,
ron::ser::PrettyConfig::default().new_line(String::from("a"))
)
.unwrap_err(),
ron::ser::to_string_pretty(&42, ron::ser::PrettyConfig::default().new_line("a"))
.unwrap_err(),
Error::Message(String::from(
"Invalid non-whitespace `PrettyConfig::new_line`"
))
);
assert_eq!(
ron::ser::to_string_pretty(
&42,
ron::ser::PrettyConfig::default().indentor(String::from("a"))
)
.unwrap_err(),
ron::ser::to_string_pretty(&42, ron::ser::PrettyConfig::default().indentor("a"))
.unwrap_err(),
Error::Message(String::from(
"Invalid non-whitespace `PrettyConfig::indentor`"
))
);
assert_eq!(
ron::ser::to_string_pretty(
&42,
ron::ser::PrettyConfig::default().separator(String::from("a"))
)
.unwrap_err(),
ron::ser::to_string_pretty(&42, ron::ser::PrettyConfig::default().separator("a"))
.unwrap_err(),
Error::Message(String::from(
"Invalid non-whitespace `PrettyConfig::separator`"
))
Expand Down

0 comments on commit fe6809f

Please sign in to comment.