module

TOML

TOML v1.0 parser and serializer for Crystal, with comment-and-format preservation across parse → modify → write round-trips.

Two entry points are exposed:

  • TOML.parse_to_hash(string) — drop-in replacement for the crystal-community/TOML.cr API. Returns a plain Hash, fast, loses every comment and the original formatting. Use this when you only ever read.

  • TOML.parse(string) — returns a TOML::Document AST that preserves comments, blank lines, and the original byte-level formatting. Use this when you need to modify a TOML file in place and re-serialize it without diffing the whole document.

See CRYSTAL-TOML-SPECS.adoc (sibling repo prod-crystal/) for the full specification.

Constants

SPEC_VERSION = "1.0.0"

TOML specification version implemented by this shard.

VERSION = {{ (((read_file("/tmp/tmp.klCdec/src/src/toml/../../shard.yml")) .lines .find(&.starts_with?("version:")) || "version: 0.0.0") .gsub(/^version:\s*/, "")) .chomp }}

Lue au compile-time depuis shard.yml via le macro read_file. Cf. note mémoire feedback_shard_version_macro.md (mémoire ALOLI).

Class methods

parse(source : String) : Document

Parses a TOML v1.0 document into a Document AST. The returned document preserves comments, blank lines, key order and the original byte-level formatting, so a round-trip TOML.parse(s).to_toml == s for any unmodified s.

The parse runs a second semantic-validation pass after the syntactic one (duplicate keys, table redefinitions, etc.); if you want to skip that — e.g. to inspect a malformed AST during debugging — use Parser.new(source).parse directly.

Raises TOML::ParseError on invalid input.

Source
parse_to_hash(source : String) : Hash(String, Type)

Parses a TOML v1.0 document and returns a plain Hash(String, TOML::Type), dropping comments and the original formatting. Drop-in replacement for the crystal-community/TOML.cr API.

Raises TOML::ParseError on invalid input or duplicate keys.

Source

Nested types