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

Preservation of Multi-Line Basic Strings in TOML Parsing #29

Closed
maliknajjar opened this issue Oct 1, 2024 · 5 comments
Closed

Preservation of Multi-Line Basic Strings in TOML Parsing #29

maliknajjar opened this issue Oct 1, 2024 · 5 comments
Labels
duplicate This issue or pull request already exists enhancement New feature or request

Comments

@maliknajjar
Copy link

Hi,

I've encountered an issue with the preservation of multi-line basic strings while using the smol-toml parser in one of my projects.

When parsing and then converting a multi-line basic string (as defined in the TOML v1.0.0 specification), the new line characters (\n) are not properly retained. Instead, the string is converted into a basic single-line string, even though the original content includes new lines.

For example:

when I parse this into a js object
image

and then I try to strigify that js object into a toml string again it shows like this
image

A possible solution is for the parser to check if a string contains a newline character. If it does, the string should be formatted as a multi-line TOML string.

Let me know if further details are needed!

@cyyynthia
Copy link
Member

Well, semantically speaking the lib is properly retaining the newline, both strings are byte equivalent. The problem is that you're hitting one of the limitations of the lib: it is not meant for complete preservation of documents, but rather values. It does not care what the original document looked like and will stringify it to whatever the code is outputting.

FWIW, this issue applies to:

  • Single-line vs multi-line strings
  • Strings vs literal strings
  • Numeric separators
  • Indentation
  • Datetime separator (T or )
  • Inline tables and dotted-keys
  • Array of inline tables
  • Comments
  • Form of floating point numbers
  • Probably some more I forgot

The question to have formatting options is tracked by #13. Preservation of comments is tracked by #17. I'm inclined to close this as dupe of #13 specifically

@cyyynthia cyyynthia added duplicate This issue or pull request already exists enhancement New feature or request labels Oct 2, 2024
@2colours
Copy link

2colours commented Oct 2, 2024

Just to restate here as well: the upstream issue that led to this library maliknajjar/guifier#7 seems to be rather that the GUI input field dropped the newline characters outright. However, that doesn't seem to be caused by the library, and it's not stated in the issue either.

@maliknajjar
Copy link
Author

maliknajjar commented Oct 2, 2024

it wouldn't really matter. Even if the text editor interpreted the \n as new lines, it would result in:

multi_line_string = "Line1
Line2
Line3"

which is invalid TOML syntax. it should be three quotes instead of one
like this

multi_line_string = """Line1
Line2
Line3"""

@maliknajjar
Copy link
Author

so for now. there is no way to produce multi-line strings using the stringify method

@cyyynthia
Copy link
Member

Multi-line strings vs single-line strings is only a stylistic difference. The two examples you've provided in the issue post are byte-equivalent, and is a correct representation of the multiline string. See the TOML spec for strings, specifically multi-line basic strings.

With regards to correctness, the lib is working as expected.
image

As I said, using multi-line strings is a stylistic preference which is something tracked under #13. While it is not the prettiest output, newlines are serialized properly.

@cyyynthia cyyynthia closed this as not planned Won't fix, can't repro, duplicate, stale Oct 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
duplicate This issue or pull request already exists enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants