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

Inconsistent pretty string #7

Open
sigoden opened this issue Aug 29, 2024 · 1 comment
Open

Inconsistent pretty string #7

sigoden opened this issue Aug 29, 2024 · 1 comment
Labels
maybe? This might be an issue, we might get to it. It's unclear...

Comments

@sigoden
Copy link

sigoden commented Aug 29, 2024

If the string contains newlines, it should be formatted as a multi-line string with |.

However, when the string has a space before \n, it does not work.

use serde::Serialize;

#[derive(Serialize, Debug)]
struct Foo {
    v1: String,
    v2: String,
}

fn main() {
    let a = Foo {
        v1: "a\nc".to_string(),
        v2: "a \nc".to_string(),
    };
    let yaml = serde_yaml_ng::to_string(&a).unwrap();
    println!("{}", yaml);
}
v1: |-
  a
  c
v2: "a \nc"
@acatton
Copy link
Owner

acatton commented Aug 29, 2024

serde-yaml-ng, like the original serde-yaml wraps unsafe-libyaml which is itself a transpilation from C to Rust of the libyaml.

It is unclear to me who is responsible for this "issue". And I'm not fluent in enough libyaml to give you a definite answer.

All I can say is that the libyaml was originally written for Python. And there is a similar behavior there:

>>> import yaml
>>> print(yaml.dump({'v1': 'a\nc', 'v2': 'a \nc'}))
v1: 'a

  c'
v2: "a \nc"

So I suspect, that the culprit is the libyaml. Either way, this won't get fixed for two reasons: I'm not going to recompile the libyaml to Rust, because I find it to be an heresy. And, I actually want to migrate to libyaml-safer as mentioned in #5. I've been working slowly on this as there is a lot of refactoring necessary for this move.

But regardless of the move, I am unsure how this is a bug. Yeah, it doesn't look neat, I'll grant you this. But deserialize(serialize(data)) = data is still valid, right? This is valid YAML being generated. And one could argue that the current serialization makes it obvious that there are trailing spaces, right?

I'll leave this issue open as documentation, and see what we can do once we fix #5.

@acatton acatton added the maybe? This might be an issue, we might get to it. It's unclear... label Aug 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
maybe? This might be an issue, we might get to it. It's unclear...
Projects
None yet
Development

No branches or pull requests

2 participants