Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

History

8 Commits

Repository files navigation

zigStructPrint

Small library to pretty-print Zig structs (and arrays)

zigStructPrint is licensed under under the MIT License and available from https://github.com/Durobot/zigStructPrint

Please note that only Zig 0.14.0-dev.1421+f87dd43c1 (give or take) and up is supported because of this breaking change in the Zig standard library. If you need zigStructPrint for an earlier version of Zig, get this version instead.

To use, either drop zsp.zig into your project, or, if you prefer Zig package manager:

  1. In build.zig.zon, in .dependencies, add

    .zigStructPrint=
    .{
    .url="https://github.com/Durobot/zigStructPrint/archive/<COMMIT HASH, 40 HEX DIGITS>.tar.gz",
    .hash="<ZIG PACKAGE HASH, 68 HEX DIGITS>"// Use arbitrary hash, get correct hash from the error 
    }
  2. In build.zig, in pub fn build, before b.installArtifact(exe);, add

    constzsp=b.dependency("zigStructPrint",
    .{
    .target=target,
    .optimize=optimize,
    });
    exe.root_module.addImport("zigStructPrint", zsp.module("zigStructPrint"));

Build with zig build, as you normally do.

Actually printing out your struct:

constzsp=@import("zigStructPrint");
. . .
constMyStruct=struct
{
a: i8=-10,
b: u32=10,
c: [3]u8= [_]u8 { 1, 2, 3 },
d: [2]Nested= .{ .{ .f=10.0, .g="Hello" }, .{ .f=-20.0, .g="Bye" }, },
e: [3]Color= .{ .red, .green, .yellow },
constNested=struct { f: f32, g: []constu8 };
constColor=enum { red, yellow, green };
};
constms=MyStruct {};
zsp.printStruct(ms, true, 0); // try `false` to get full type names

And the output is:

{
a: i8=-10b: u32=10c: [3]u8= [ 1, 2, 3, ]
d: [2]Nested= [ { f: f32=10, g: []constu8="Hello", }, { f: f32=-20, g: []constu8="Bye", }, ]
e: [3]Color= [ red, green, yellow, ]
}

About

Small library to pretty-print Zig structs (and arrays)

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages