Project

toml-rb

1.03
A long-lived project that still receives updates
A TOML 1.1.0 parser and dumper.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
 Dependencies

Development

~> 5.7
>= 0
~> 1.4
 Project Readme

TOML Logo

toml-rb

Gem Version Build Status Code Climate RubyGem

A pure Ruby TOML parser and dumper with no runtime dependencies.

toml-rb implements TOML 1.1.0 and passes all toml-test v2.2.0 decoder tests for that version.

Installation

$ gem install toml-rb

Or add this line to your Gemfile:

gem "toml-rb", "~> 6.0"

Parser Usage

require 'toml-rb'

# From a file!
path = File.join(File.dirname(__FILE__), 'path', 'to', 'file')
TomlRB.load_file(path)

# From a stream!
stream = <<-EOS
  title = "wow!"

  [awesome]
    you    = true
    others = false
EOS
TomlRB.parse(stream)
# => {"title"=>"wow!", "awesome"=>{"you"=>true, "others"=>false}}

# You want symbols as your keys? No problem!
TomlRB.load_file(path, symbolize_keys: true) 
# Works the same for TomlRB.parse

# Local datetimes, dates and times are subclasses of Time. Offset datetimes are Time.
times = <<-EOS
  odt = 1979-05-27T07:32:00Z
  ldt = 1979-05-27T07:32:00
  ld  = 1979-05-27
  lt  = 07:32:00
EOS
TomlRB.parse(times).transform_values(&:class)
# => {"odt"=>Time, "ldt"=>TomlRB::LocalDateTime, "ld"=>TomlRB::LocalDate, "lt"=>TomlRB::LocalTime}

Dumper Usage

TomlRB.dump raises TomlRB::Error for more than 100 nested tables and arrays, and for cyclic values.

require 'toml-rb'

# Simple example
TomlRB.dump( simple: true)
# => "simple = true\n"


# Complex example
hash = { 
    "title"=>"wow!", 
    "awesome"=> {
        "you"=>true, 
        "others"=>false
    }
}

TomlRB.dump(hash)
# => "title = \"wow!\"\n[awesome]\nothers = false\nyou = true\n"

Conformance

toml-rb is exercised against the official toml-test conformance suite for both TOML 1.0.0 and TOML 1.1.0. The harness lives in:

  • tool/toml-test-decoder — toml-test decoder shim.
  • .github/workflows/conformance.yml — CI workflow (per-version matrix).
  • .github/toml-test-skip-1.0.txt and .github/toml-test-skip-1.1.txt — skip-lists of toml-test names for each spec version.

The TOML 1.1.0 skip-list is empty. The TOML 1.0.0 skip-list has 9 tests with documents that TOML 1.0.0 forbids and TOML 1.1.0 allows. toml-rb accepts TOML 1.1.0 syntax and has no option to select the spec version, so these tests fail. CI runs toml-test with -skip-must-err, so the run fails when a skipped test passes.

Contributing

  1. Fork it
  2. Create your feature branch git checkout -b my-new-feature
  3. Add tests and commit your changes git commit -am 'Add some feature'
  4. Run tests $ rake
  5. Push the branch git push origin my-new-feature
  6. Create new Pull Request

License

MIT License

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.