Crystal: RFC: INI parser yielding line number

Created on 1 Jun 2020  路  2Comments  路  Source: crystal-lang/crystal

Context

I would like to make something similar to an hypothetical INI::Serializable, which will use the stdlib to parse an INI IO/String and set instance variable accordingly.

INI.parse returns a Hash(String, Hash(String, String)), which allows to do more or less what I want, except two things that can be better:

  • The line number, is of course not present on a Hash. Even if there are key indexes, they don't tell the exact line where the section/key is on the INI file.
  • Less an issue: an intermediate Hash is created, which is not very efficient.

The line number is useful to raise an exception to the user, with as information the invalid key and its line.

Proposals

Yielding each section, key, value and number is not possible because empty sections can exist.

A simple pull parser can be an option, or having a INI.parse which can yield a Section | Key union object (a line can either be a section declaration or a key/value).

Additional notes

I think INI.build is fine, because it accepts any object which implements each, with values that also implements #each.
One little issue is that the main object has also to implement #[], to check if there is a empty section at first. It can be solved by using is_a? Hash.

Most helpful comment

Also it might be worth to show TOML some love which is essentially a well defined superset of INI.

All 2 comments

Also it might be worth to show TOML some love which is essentially a well defined superset of INI.

INI is very widely used for configuration, where JSON is considered not readable enough and YAML too complicated.
Examples of applications using it: Gitea, Minetest, PHP.

A lot of Linux applications are using it for configuration (as .conf or .ini) in $XDG_CONFIG_HOME and in /etc: VLC, Geany, LibreOffice, LXQt.

A lot of languages have it in their stdlib.

It is ok for me to propose a INI::PullParser PR.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

asterite picture asterite  路  3Comments

grosser picture grosser  路  3Comments

jhass picture jhass  路  3Comments

lgphp picture lgphp  路  3Comments

asterite picture asterite  路  3Comments