Skip to content

[WIP] A robust, efficient implementation of the Rusty Object Notation (RON) for the Zig programming language.

License

Notifications You must be signed in to change notification settings

teh-cmc/zig-ron

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Zig-RON

wip

Hopefully: a robust, efficient implementation of the Rusty Object Notation for the Zig programming language.

This is a work in progress: you can and should expect missing docs, miscellaneous bugs, a general lack of polish, etc.

The deserializer should already be in a pretty usable state though, hence this early release. There simply isn't a serializer yet, that's the easy part though :)

Feel free to reach out either via the issue tracker or on Zig discord (clem#0498).

Usage

const std = @import("std");
const ron = @import("ron");

const MyStruct = struct {
    bool: bool,
    float: f32,
    int: i42,
    string: []const u8,
    char: ron.Char,
    unit: ron.Unit,

    array_list: std.ArrayList(u32),
    slice: []u32,

    string_map: std.StringArrayHashMap([]const u8),
    map: std.AutoArrayHashMap(bool, []const u8),

    option: ?[]const u8,
    enums: enum { a, b, c },
    unions: union(enum) { a: bool, b: []const u8, c: f32 },
    any: ron.Value, // Generic heterogeneous values

    nested: ?*@This(),

    default: []const u8 = "some default value",
};

const data =
    \\MyStruct(
    \\  bool: true,
    \\  float: 42.420,
    \\  int: 666,
    \\  string: "ガタカへようこそ",
    \\  char: '🚁',
    \\  unit: (),
    \\
    \\  array_list: [1, 2, 3, 4],
    \\  slice: [5, 6, 7, 8],
    \\
    \\  string_map: { "hello": "world" },
    \\  map: { false: "is false", true: "is not false" },
    \\
    \\  option: Some("\u{263A}"),
    \\  enums: b,
    \\  unions: c(666.111),
    \\  any: seq([string("hey"), bool(true), float(42.42)]),
    \\
    // Tuple to struct coercion!
    \\  nested: Some((
    \\    false, 1.0, 2, "xxx", 'X', (),
    \\    [], [], {}, {},
    \\    None, a, b("nope!"), option(None),
    \\    None,
    \\  )),
    \\
    // This will be silently ignored.
    \\  field_that_doesnt_exist: DoesntExist(
    \\    xxx: 42,
    \\    42: "xxx"
    \\  ),
    \\)
;

pub fn main() !void {
    var allocator = std.heap.page_allocator;

    var deser = ron.Deserializer.init(allocator);
    // clears internal parser resources
    defer deser.deinit();
    // clears data allocated for the end-user (arrays, maps, escaped strings...)
    defer deser.parseFree();

    var tokens = &ron.Lexer.init(data).token_stream;
    const res = deser.parse(MyStruct, tokens) catch |err| {
        try deser.report(std.io.getStdErr().writer());
        return err;
    };

    _ = ron.debug_ext.dbg(res);
}

License

MIT

About

[WIP] A robust, efficient implementation of the Rusty Object Notation (RON) for the Zig programming language.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages