zonyitoo / rust-ini

INI file parser in Rust
MIT License
302 stars 78 forks source link

Parsing of Python multiline configparser ini dialect #138

Open davidhalter opened 3 weeks ago

davidhalter commented 3 weeks ago

Python's configparser allows to parse these multiline sections and will have a value of '\nb\nc' for foo:

[section1]
foo =
  b
  c
[section2]
bar = 1

I would like to parse this as well with rust-ini. However currently this fully ignores section2 and leads to this:

Properties {
    data: {
        "foo": "",
        "b\n  c\n[section2]\nbar": "1",
    },
}

In my opinion this is very weird and should probably never be the case. Here it seems that the key becomes something very long that even "eats" the next section. At the very least I feel like this should not be a valid key. I personally would like to change the current behavior to parse these multiline inis. Would a PR be accepted? I'm happy to do the work. Otherwise I would probably just fork. Do you think we need a feature flag for this like multiline-values? I feel like the current behavior doesn't really help anybody either.

If someone is interested, this is how you do it in Python:

>>> s = '''
... [section1]
... foo =
...   b
...   c
... [section2]
... bar = 1
... '''
>>> import configparser
>>> c = configparser.ConfigParser()
>>> c.read_string(s)
>>> c.items("section1")
[('foo', '\nb\nc')]
>>> c.items("section2")
[('bar', '1')]
zonyitoo commented 3 weeks ago

I would prefer to add a new field in ParseOption to support this kind of behavior.

PRs are welcome.

davidhalter commented 3 weeks ago

I have a busy 2 weeks in front of me, but I'm happy to do it afterwards.

Also: Isn't the current behavior broken in a way? Do we really want multi-line keys?

zonyitoo commented 3 weeks ago

Well, the current behavior about parsing key is: read until we found character = or :.

We don't want multi-line keys. It should be fixed.