Watch
1
0
Fork
You've already forked zon-rb
0
mirror of https://codeberg.org/r4gus/zon-rb.git synced 2026-11-17 03:20:37 +00:00
Zig Object Notation for Ruby
  • Ruby 98.4%
  • Zig 1.4%
  • Shell 0.2%
Find a file
Repository files (latest commit first)
Filename Latest commit message Latest commit date
David Sugar 89b4b5ac92 gemfile
2026-08-22 22:01:50 +02:00
bin added basic documentation 2025-09-27 18:11:51 +02:00
lib bumped up patch version 2026-08-22 21:59:36 +02:00
sig added basic documentation 2025-09-27 18:11:51 +02:00
spec fixed bug preventing literals to use the at-notation 2026-08-22 21:59:03 +02:00
.gitignore removed version list 2026-07-19 17:56:50 +02:00
.rspec initial commit with basic zon lexer and parser support 2025-09-26 09:25:11 +02:00
.rubocop.yml initial commit with basic zon lexer and parser support 2025-09-26 09:25:11 +02:00
CHANGELOG.md added temporary hash calculation fix 2026-08-01 23:08:23 +02:00
Gemfile removed version list 2026-07-19 17:56:50 +02:00
Gemfile.lock gemfile 2026-08-22 22:01:50 +02:00
LICENSE.txt initial commit with basic zon lexer and parser support 2025-09-26 09:25:11 +02:00
Rakefile initial commit with basic zon lexer and parser support 2025-09-26 09:25:11 +02:00
README.md moved hasher into Zig namespace 2025-10-19 19:43:11 +02:00
zon.gemspec moved zon-rb to Codeberg 2026-08-01 22:32:11 +02:00

Zig Object Notation (ZON) for Ruby

Gem Version

The Zig Object Notation (ZON) is a file format primarily used within the Zig ecosystem. For example, ZON is used for the Zig package manifest.

Installation

Install the gem and add to the application's Gemfile by executing:

bundle add zon-rb

If bundler is not being used to manage dependencies, install the gem by executing:

gem install zon-rb

Usage

To use this Gem you have to require it:

require "zon"

To parse ZON data into a Ruby object, one can use the Zon::parse method. The method expects either a String or IO (File) object as its argument.

irb(main):001> Zon::parse(".{ \"foo\", \"bar\"}")
=> ["foo", "bar"]
irb(main):002> Zon.parse(File.open("../PassKeeZ/build.zig.zon"))
=>
{name: :passkeez,
 version: "0.5.3",
 minimum_zig_version: "0.15.1",
 fingerprint: 15931082159778014966,
 dependencies:
  {keylib:
    {url: "https://github.com/Zig-Sec/keylib/archive/refs/tags/0.7.0.tar.gz",
     hash: "keylib-0.7.0-mbYjk6qaCQACutrMpyhgstSmYxSKmcuRmLI-CJSumBeA"},
   uuid:
    {url: "https://github.com/r4gus/uuid-zig/archive/refs/tags/0.4.0.tar.gz",
     hash: "uuid-0.4.0-oOieIR2AAAChAUVBY4ABjYI1XN0EbVALmiN0JIlggC3i"},
   kdbx: {path: "../kdbx"}},
 paths: ["build.zig", "build.zig.zon", "linux", "README.md", "script", "src", "static"]}

To serialize a Ruby object into ZON, use the Zon::serialize method.

irb(main):002> o
=>
{name: :passkeez,
 version: "0.5.3",
 minimum_zig_version: "0.15.1",
 fingerprint: 15931082159778014966,
 dependencies:
  {keylib: {url: "https://github.com/Zig-Sec/keylib/archive/refs/tags/0.7.0.tar.gz", hash: "keylib-0.7.0-mbYjk6qaCQACutrMpyhgstSmYxSKmcuRmLI-CJSumBeA"},
   uuid: {url: "https://github.com/r4gus/uuid-zig/archive/refs/tags/0.4.0.tar.gz", hash: "uuid-0.4.0-oOieIR2AAAChAUVBY4ABjYI1XN0EbVALmiN0JIlggC3i"},
   kdbx: {path: "../kdbx"}},
 paths: ["build.zig", "build.zig.zon", "linux", "README.md", "script", "src", "static"]}
irb(main):013> puts(Zon::serialize(o, {:indent_level => 4}))
.{
    .name = .passkeez,
    .version = "0.5.3",
    .minimum_zig_version = "0.15.1",
    .fingerprint = 0xdd1692d15d21a6f6,
    .dependencies = .{
        .keylib = .{
            .url = "https://github.com/Zig-Sec/keylib/archive/refs/tags/0.7.0.tar.gz",
            .hash = "keylib-0.7.0-mbYjk6qaCQACutrMpyhgstSmYxSKmcuRmLI-CJSumBeA",
        },
        .uuid = .{
            .url = "https://github.com/r4gus/uuid-zig/archive/refs/tags/0.4.0.tar.gz",
            .hash = "uuid-0.4.0-oOieIR2AAAChAUVBY4ABjYI1XN0EbVALmiN0JIlggC3i",
        },
        .kdbx = .{
            .path = "../kdbx",
        },
    },
    .paths = .{
        "build.zig",
        "build.zig.zon",
        "linux",
        "README.md",
        "script",
        "src",
        "static",
    },
}

To customize the generated ZON data one can use the :indent_level and :ibase options. The :indent_level specifies how much each block is indented. The :ibase option can be used to define in which format (hex: 16, octal: 8, binary: 2, decimal: 10) an Integer should be serialized. The default for :ibase is 16 (hexadecimal).

irb(main):010> Zon::serialize(255, {:ibase => 2})
=> "0b11111111"

Zig Manifest

Zon::Zig::Manifest provides you with an abstraction for your build.zig.zon. It will also validate your Zig package manifest and raise an exception if one of the required fields is missing.

o = Zon::parse(File.open("../PassKeeZ/build.zig.zon"))
manifest = Zon::Zig::Manifest.new o

# Use:
# manifest.name
# manifest.version
# ...

Zig Package Hash

Given a path pdir that points to the root of a Zig package, one can calculate the packages hash using the Zon::Hasher:

hasher = Zon::Zig::Hasher.new pdir
hasher.hash
# Hash with the format nnnn-vvvv-XXXX..XXXX
# e.g.: uuid-0.4.0-oOieIR2AAAChAUVBY4ABjYI1XN0EbVALmiN0JIlggC3i
result = hasher.result

Development

After checking out the repo, run bin/setup to install dependencies. Then, run rake spec to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and the created tag, and push the .gem file to rubygems.org.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/r4gus/zon-rb.

License

The gem is available as open source under the terms of the MIT License.