Ziggy format parse diagnostic error

program

const std = @import("std");
const ziggy = @import("ziggy");
const Config = struct { heartbeat: []const u8 };
pub fn main() !void {
    var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
    defer arena.deinit();
    const allocator = arena.allocator();
    var diagnostic = ziggy.Diagnostic{ .path = "diagnose.txt" };
    const config = ziggy.parseLeaky(Config, allocator, "str", .{ .diagnostic = &diagnostic }) catch |err| {
        std.debug.print("{any}\n", .{err});
        return;
    };
    std.debug.print("{any}\n", .{config});
}

sample.zgy

{ "heartbeat": "10ms" }

output:
error.Syntax
as per docs -

Now you can access the data through the returned value. The function will only return error.Syntax in case of a syntax error. If you want more detailed diagnostics, provide an instance of ziggy.Diagnostic to the last argument of parseLeaky. In case of an error you will be able to inspect it to get more information about the error (it also has convenience formatting functions).

i did update diagnostic flag unfortunately it did not help with any trace
Am pretty sure doing some thing idiotic from my end and the fix is simple.

Hi @ehrktia, welcome to Ziggit!

It looks like sample.zgy is in json, not ziggy. Try changing it to

{ .heartbeat = "10ms" }
1 Like

Hi, Ziggy is currently in the middle of a pretty big rewrite.
Regardless of the issues that you’re encountering now, my recommendation would be to wait for the rewrite to be completed.

That said, Sourthporter probably gave you correct advice, with just one nitpick: Ziggy is a superset of JSON, so all valid JSON documents are valid Ziggy documents, but a JSON object maps to Ziggy Dictionary (previously called Map) and not to a Ziggy struct.

I currently don’t remember if the parser will agree to parse a Dictionary to a Zig struct or not.

One thing to note though is that the provided example code has some issues.

  1. it seems that you’re trying to parse "str" instead of the contents of a file, which would explain why you’re getting a syntax error.
  2. The diagnostic struct is not being printed in the case of an error, which is why is not doing anything useful for you.
  3. diagnostic.path is meant to be the path where the ziggy file you want to parse is located and it’s just used to print nice error messages. if you hardcode the source in your Zig source code, or read it from stdin for example, then you can set .path = null.

You can use diagnostic like this:

const source = "{ .foo = true }";
var diagnostic = ziggy.Diagnostic{ .path = null };

const config = ziggy.parseLeaky(Config, allocator, source, .{ .diagnostic = &diagnostic }) catch {
    std.debug.print("error: {f}\n", .{diagnostic.fmt(source)});
    return;
};
5 Likes

thanks very much for detailed explanation. I am learning thought can do it in Ziggy to kill 2 stones learn and do a project. Will follow the suggestion.

thanks the reason I omitted the . in front of key is the doc mentioned it is acceptable to have key with no . in front. Will try again with dot in front of my key value.

this is the simple fix as expected

1 Like