Home

Awesome

zig-nestedtext

Build badge Release badge

A NestedText parser written in Zig 0.13 targeting NestedText v2.0 (note Deviations From Specification below).

See my Zig NestedText Library blog post.

Usage

There are a few options for making use of this project:

The Zig library has no external dependencies.

The CLI tool depends only on Clap for command line arg parsing (included as a git submodule under deps/).

Run zig build to build the static library libnestedtext.a under zig-out/lib/, which can then be linked with your program.

CLI Tool

An executable CLI program nt-cli is included for exposing the core functionality of the project. When building with zig build this can be found under zig-out/bin/.

This tool can be used to convert between NestedText and JSON, for example:

$./zig-out/bin/nt-cli -f samples/employees.nt
debug(cli):     +0 Starting up
debug(cli):     +0 Parsed args
debug(cli):     +1 Finished reading input
debug(cli):     +1 Parsed NestedText
debug(cli):     +1 Converted to JSON
debug(cli):     +1 Stringified JSON
{"president":{"name":"Katheryn McDaniel","address":"138 Almond Street\nTopeka, Kansas 20697","phone":{"cell":"1-210-555-5297","home":"1-210-555-8470"},"email":"KateMcD@aol.com","additional-roles":["board member"]},"vice-president":{"name":"Margaret Hodge","address":"2586 Marigold Lane\nTopeka, Kansas 20682","phone":"1-470-555-0398","email":"margaret.hodge@ku.edu","additional-roles":["new membership task force","accounting task force"]},"treasurer":[{"name":"Fumiko Purvis","address":"3636 Buffalo Ave\nTopeka, Kansas 20692","phone":"1-268-555-0280","email":"fumiko.purvis@hotmail.com","additional-roles":["accounting task force"]},{"name":"Merrill Eldridge","phone":"1-268-555-3602","email":"merrill.eldridge@yahoo.com"}]}
debug(cli):     +1 Exiting with: 0

Yes, that took around 1 millisecond ;)

And back to NestedText:

$./zig-out/bin/nt-cli -f samples/employees.nt | ./zig-out/bin/nt-cli -F json -O nt
president:
  name: Katheryn McDaniel
  address:
    > 138 Almond Street
    > Topeka, Kansas 20697
  phone:
    cell: 1-210-555-5297
    home: 1-210-555-8470
  email: KateMcD@aol.com
  additional-roles:
    - board member
vice-president:
  name: Margaret Hodge
  address:
    > 2586 Marigold Lane
    > Topeka, Kansas 20682
  phone: 1-470-555-0398
  email: margaret.hodge@ku.edu
  additional-roles:
    - new membership task force
    - accounting task force
treasurer:
  -
    name: Fumiko Purvis
    address:
      > 3636 Buffalo Ave
      > Topeka, Kansas 20692
    phone: 1-268-555-0280
    email: fumiko.purvis@hotmail.com
    additional-roles:
      - accounting task force
  -
    name: Merrill Eldridge
    phone: 1-268-555-3602
    email: merrill.eldridge@yahoo.com

Deviations From Specification

The amount of deviation from the official NestedText spec is kept to a minimum, however there is one minor case to be aware of (as reflected in the fork of the official testsuite being used to test this project). Where possible these issues are resolved upstream.

Note that this project implements a strict subset of the official spec, to avoid compatibility issues.

Empty Values in Flow-style

Empty values are officially allowed in flow-style NestedText, e.g. {:} -> {"":""}, however this is disallowed by this project.

The design choices behind handling for empty values in flow-style are explained in detail at https://zigforum.org/t/zig-nestedtext-release-0-1-0/383/5.

This has been discussed with the NestedText creators in GitHub issues (here and here) and over email, and discussions are ongoing.

To summarise the rationale behind this deviation:

If you have any thoughts about this either way, please consider posting a comment to https://github.com/LewisGaul/zig-nestedtext/issues/17.

Line Endings Preserved

When a string contains newlines, those newlines are preserved from the NestedText content itself. Without this, it is impossible to represent carriage return and newline characters in a platform-independent, implementation-independent way.

This has been discussed at https://github.com/KenKundert/nestedtext_tests/issues/5.

Changelog

See CHANGELOG.md.

Development

Build with zig build - the library will be under zig-out/lib/ and the CLI tool will be under zig-out/bin/.

Run the tests with zig build test.

Please feel free to propose changes via a PR, but please make sure all tests are passing, and also make sure to run zig fmt . (there are GitHub actions checks for both of these).