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

Pretty configuration options #61

Merged
merged 2 commits into from
Oct 12, 2017
Merged

Pretty configuration options #61

merged 2 commits into from
Oct 12, 2017

Conversation

kvark
Copy link
Collaborator

@kvark kvark commented Oct 11, 2017

Fixes #58
BREAKING CHANGE

@kvark kvark requested a review from torkleyy October 11, 2017 18:40
@kvark
Copy link
Collaborator Author

kvark commented Oct 11, 2017

Non-separated tuple members:

(
    float: (2.180000066757202, -1.1),
    tuple: ((), false),
    map: {
        0: '1',
        3: '5',
        8: '1',
        1: '2',
    },
    nested: (
        a: "Hello from \"RON\"",
        b: 'b',
    ),
    var: A(255, ""),
)

Separated tuple members:

(
    float: (
        2.180000066757202,
        -1.1,
    ),
    tuple: (
        (),
        false,
    ),
    map: {
        8: '1',
        0: '1',
        3: '5',
        1: '2',
    },
    nested: (
        a: "Hello from \"RON\"",
        b: 'b',
    ),
    var: A(
        255,
        "",
    ),
)

Copy link
Contributor

@torkleyy torkleyy left a comment

Choose a reason for hiding this comment

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

Nice, I love these options. Also solves the question about which line endings to use. Found a few nits, see below.

fn default() -> Self {
PrettyConfig {
#[cfg(not(target_os = "windows"))]
new_line: "\n".to_string(),
Copy link
Contributor

Choose a reason for hiding this comment

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

Good idea!

src/ser/mod.rs Outdated
struct_names: bool,
}

impl Serializer {
fn separate_tuple_members(&self) -> bool {
match self.pretty {
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: could use map and unwrap_or

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I don't think it would look much better

Copy link
Contributor

Choose a reason for hiding this comment

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

It's just the style that seems to be preferred. You can also leave it as is if you prefer this style.

use serde::ser::Serialize;

/// Serializes `value` in the recommended RON layout.
pub fn to_string<T>(value: &T) -> Result<String>
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't see why we have to make this a breaking change.

Copy link
Contributor

Choose a reason for hiding this comment

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

Could we just add a second variant and leave this function, using PrettyConfig::default?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Not sure if it's worth it? All it takes from the user is to pass in default() as a parameter

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

although, you are right, by keeping the old function we can make it a non-breaking change

Copy link
Contributor

Choose a reason for hiding this comment

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

Right, I just don't see why we should bump the version.

/// New line string
pub new_line: String,
/// Indentation string
pub indentor: String,
Copy link
Contributor

Choose a reason for hiding this comment

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

Could theoretically use &str or &[u8] instead.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I tried, facing the lifetime hell since the &'a (assuming we don't want &'static) starts polluting Serializer struct

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah, let's leave it like that then. Pretty serialization isn't something that suffers from two string allocations I guess.

src/ser/mod.rs Outdated
struct Pretty {
indent: usize,
}

/// Pretty serializer configuration
pub struct PrettyConfig {
Copy link
Contributor

Choose a reason for hiding this comment

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

Let's derive Copy, Clone, Debug and serde traits for this struct.

@kvark
Copy link
Collaborator Author

kvark commented Oct 11, 2017

Thanks for the review, please take another look!

src/ser/mod.rs Outdated
use std::fmt::{Display, Formatter, Result as FmtResult};

use serde::ser::{self, Serialize};

#[deprecated]
Copy link
Contributor

Choose a reason for hiding this comment

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

I wouldn't deprecate it. Better just move the new function to that module. If you really want to, we should give an explanation.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I don't see why we need pretty module, it's empty and serves no solid purpose.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

And what deprecated tells you is basically that it's going to be cut off in the next breaking release, which is what I wanted to do in the first place :)

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, but let's add an explanation that one should simply use to_string_pretty with a default config instead.

Copy link
Contributor

Choose a reason for hiding this comment

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

So #[deprecated(since="0.1.4", note="please use to_string_pretty with PrettyConfig::default() instead")]

@kvark
Copy link
Collaborator Author

kvark commented Oct 11, 2017

@torkleyy agreed. How about now?

Copy link
Contributor

@torkleyy torkleyy left a comment

Choose a reason for hiding this comment

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

Thanks! After you've addressed that last thing, feel free to merge.

src/ser/mod.rs Outdated
struct Pretty {
indent: usize,
}

/// Pretty serializer configuration
#[derive(Clone, Debug)]
Copy link
Contributor

Choose a reason for hiding this comment

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

We need Deserialize and Serialize, too.

@kvark
Copy link
Collaborator Author

kvark commented Oct 11, 2017 via email

@torkleyy
Copy link
Contributor

So what's the problem?

@kvark
Copy link
Collaborator Author

kvark commented Oct 11, 2017

@torkleyy well, one more dependency imposed for all the users. Not sure if it's worth it, considering how marginal the need to serialize the config is.

@Aceeri
Copy link

Aceeri commented Oct 11, 2017

@kvark serde re-exports serde_derive if you use the "derive" feature flag I believe.

@kvark
Copy link
Collaborator Author

kvark commented Oct 11, 2017 via email

@kvark
Copy link
Collaborator Author

kvark commented Oct 12, 2017

@torkleyy added serde_derive unconditionally now

Copy link
Contributor

@torkleyy torkleyy left a comment

Choose a reason for hiding this comment

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

Thank you for addressing all my concerns!

@torkleyy
Copy link
Contributor

Let's, finally, merge this :)

bors r+

bors bot added a commit that referenced this pull request Oct 12, 2017
61: Pretty configuration options r=torkleyy a=kvark

Fixes #58
~~BREAKING CHANGE~~
@bors
Copy link
Contributor

bors bot commented Oct 12, 2017

Build succeeded

@bors bors bot merged commit ed79046 into master Oct 12, 2017
@kvark kvark deleted the pretty branch October 12, 2017 17:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants