Git Product home page Git Product logo

parsetoml's Introduction

NimToml

Parsetoml is a Nim library to parse TOML files (https://github.com/toml-lang/toml). Currently it supports v0.5.0 of the TOML specification. It passes all of the validation tests in the validation suite, including various 0.5.0 examples.

Installation

Use nimble to install it:

nimble install parsetoml

Usage

The full documentation is available at http://parsetoml.readthedocs.org/en/latest/. Here I just show a few snippets of code.

Import the library using

import parsetoml

There are several functions that can parse TOML content:

let table1 = parsetoml.parseString(""""
[input]
file_name = "test.txt"

[output]
verbose = true
""")
let table2 = parsetoml.parseFile(f)
let table3 = parsetoml.parseFile("test.toml")

The return value of parseString and parseFile is a TomlTableRef object. Several functions are available to query for specific fields; here is an example:

# Get the value, or fail if it is not found
let verboseFlag = parsetoml.getBool(table1, "output.verbose")

# You can specify a default as well
let input = parsetoml.getString(table1, "input.file_name", "dummy.txt")

For the validation this library needs to output JSON, it therefore has a procedure to produce JSON code. This is not just a simple JSON object however, it follows the rather verbose specification of the validator.

import parsetoml
import json
import streams

let table = parsetoml.parseStream(newFileStream(stdin))

echo table.toJson.pretty

If you need to not only read TOML there is also the possibility of writing the internal TOML representation out as a string. This is still an early implementation and has not been run through the validator (it requires JSON input). Therefore it probably contains bugs.

import parsetoml
import streams

var table = parsetoml.parseString("""
[table]
a = 100
""")

var a = table.getValueFromFullAddr("table.a")
a.intVal = 200

echo table.toTomlString

License

Parsetoml is released under a MIT license.

parsetoml's People

Contributors

ziotom78 avatar pmunch avatar data-man avatar kaushalmodi avatar iffy avatar

Watchers

 avatar James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.