Skip to content

Commit

Permalink
Modifications to keep up with the breaking changes in Zig standard li…
Browse files Browse the repository at this point in the history
  • Loading branch information
Durobot committed Oct 10, 2024
1 parent bb2daea commit d663bf6
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 31 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ Small library to pretty-print Zig structs (and arrays)

**zigStructPrint** is licensed under under [the MIT License](https://en.wikipedia.org/w/index.php?title=MIT_License&useskin=vector) 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](https://github.com/ziglang/zig/commit/0fe3fd01ddc2cd49c6a2b939577d16b9d2c65ea9) in the Zig standard library. If you need zigStructPrint for an earlier version of Zig, get [this version](https://github.com/Durobot/zigStructPrint/releases/tag/v0.1-beta) instead.

To use, either drop [zsp.zig](https://github.com/Durobot/zigStructPrint/blob/main/src/zsp.zig) into your project, or, if you prefer Zig package manager:

1. In `build.zig.zon`, in `.dependencies`, add
Expand Down
62 changes: 31 additions & 31 deletions src/zsp.zig
Original file line number Diff line number Diff line change
Expand Up @@ -27,40 +27,40 @@ const std = @import("std");
pub fn printStruct(s: anytype, shorten_types: bool, indent: comptime_int) void
{
const s_type_info = @typeInfo(@TypeOf(s));
if (s_type_info != .Struct)
if (s_type_info != .@"struct")
@compileError("fn printStruct: `s` is " ++ @typeName(s) ++ " , expected a struct");

const ind_str = " " ** indent;
const ind_str_2 = " " ** (indent + 1);
std.debug.print("{s}{{\n", .{ ind_str });
var name_buf = [_]u8 { 0 } ** 100;
inline for (s_type_info.Struct.fields) |fld|
inline for (s_type_info.@"struct".fields) |fld|
switch (@typeInfo(fld.type))
{
.Int, .Float, .ComptimeInt, .ComptimeFloat =>
.int, .float, .comptime_int, .comptime_float =>
std.debug.print("{s}{s}{s}: {s} = {d}\n",
.{ ind_str_2, if (fld.is_comptime) "comptime " else "",
fld.name, @typeName(fld.type), @field(s, fld.name) }),
.Bool =>
.bool =>
std.debug.print("{s}{s}{s}: {s} = {}\n",
.{ ind_str_2, if (fld.is_comptime) "comptime " else "",
fld.name, @typeName(fld.type), @field(s, fld.name) }),
.Struct =>
.@"struct" =>
{
std.debug.print("{s}{s}{s}: {s} =\n",
.{ ind_str_2, if (fld.is_comptime) "comptime " else "",
fld.name, typeName(@typeName(fld.type), &name_buf, shorten_types) });
printStruct(@field(s, fld.name), shorten_types, indent + 1);
},
.Array =>
.array =>
{
std.debug.print("{s}{s}{s}: {s} = [ ",
.{ ind_str_2, if (fld.is_comptime) "comptime " else "",
fld.name, typeName(@typeName(fld.type), &name_buf, shorten_types) });
printArray(@field(s, fld.name), shorten_types);
std.debug.print("]\n", .{});
},
.Pointer => |ptr_type_info|
.pointer => |ptr_type_info|
{
switch (ptr_type_info.size)
{
Expand All @@ -85,10 +85,10 @@ pub fn printStruct(s: anytype, shorten_types: bool, indent: comptime_int) void
}
}
},
.Enum => std.debug.print("{s}{s}{s}: {s} = {s}\n",
.{ ind_str_2, if (fld.is_comptime) "comptime " else "",
fld.name, typeName(@typeName(fld.type), &name_buf, shorten_types),
@tagName(@field(s, fld.name)) }),
.@"enum" => std.debug.print("{s}{s}{s}: {s} = {s}\n",
.{ ind_str_2, if (fld.is_comptime) "comptime " else "",
fld.name, typeName(@typeName(fld.type), &name_buf, shorten_types),
@tagName(@field(s, fld.name)) }),
else => std.debug.print("{s}{s}{s}: {s} = -\n",
.{ ind_str_2, if (fld.is_comptime) "comptime " else "", fld.name,
typeName(@typeName(fld.type), &name_buf, shorten_types) }),
Expand All @@ -99,38 +99,38 @@ pub fn printStruct(s: anytype, shorten_types: bool, indent: comptime_int) void
pub fn printStructInline(s: anytype, shorten_types: bool) void
{
const s_type_info = @typeInfo(@TypeOf(s));
if (s_type_info != .Struct)
if (s_type_info != .@"struct")
@compileError("fn printStructInline: `s` is " ++ @typeName(s) ++ " , expected a struct");

std.debug.print("{{ ", .{});
var name_buf = [_]u8 { 0 } ** 100;
inline for (s_type_info.Struct.fields) |fld|
inline for (s_type_info.@"struct".fields) |fld|
switch (@typeInfo(fld.type))
{
.Int, .Float, .ComptimeInt, .ComptimeFloat =>
.int, .float, .comptime_int, .comptime_float =>
std.debug.print("{s}{s}: {s} = {d}, ",
.{ if (fld.is_comptime) "comptime " else "",
fld.name, @typeName(fld.type), @field(s, fld.name) }),
.Bool =>
.bool =>
std.debug.print("{s}{s}: {s} = {}, ",
.{ if (fld.is_comptime) "comptime " else "",
fld.name, @typeName(fld.type), @field(s, fld.name) }),
.Struct =>
.@"struct" =>
{
std.debug.print("{s}{s}: {s} = ",
.{ if (fld.is_comptime) "comptime " else "",
fld.name, typeName(@typeName(fld.type), &name_buf, shorten_types) });
printStruct(@field(s, fld.name), 0, true);
},
.Array =>
.array =>
{
std.debug.print("{s}{s}: {s} = [ ",
.{ if (fld.is_comptime) "comptime " else "",
fld.name, typeName(@typeName(fld.type), &name_buf, shorten_types) });
printArray(@field(s, fld.name), shorten_types);
std.debug.print("], ", .{});
},
.Pointer => |ptr_type_info|
.pointer => |ptr_type_info|
{
switch (ptr_type_info.size)
{
Expand All @@ -155,10 +155,10 @@ pub fn printStructInline(s: anytype, shorten_types: bool) void
}
}
},
.Enum => std.debug.print("{s}{s}: {s} = {s}, ",
.{ if (fld.is_comptime) "comptime " else "",
fld.name, typeName(@typeName(fld.type), &name_buf, shorten_types),
@tagName(@field(s, fld.name)) }),
.@"enum" => std.debug.print("{s}{s}: {s} = {s}, ",
.{ if (fld.is_comptime) "comptime " else "",
fld.name, typeName(@typeName(fld.type), &name_buf, shorten_types),
@tagName(@field(s, fld.name)) }),
else => std.debug.print("{s}{s}: {s} = -, ",
.{ if (fld.is_comptime) "comptime " else "", fld.name,
typeName(@typeName(fld.type), &name_buf, shorten_types) }),
Expand All @@ -169,11 +169,11 @@ pub fn printStructInline(s: anytype, shorten_types: bool) void
pub fn printArray(a: anytype, shorten_types: bool) void
{
const a_type_info = @typeInfo(@TypeOf(a));
if (a_type_info != .Array and
(a_type_info != .Pointer or a_type_info.Pointer.size != .Slice))
if (a_type_info != .array and
(a_type_info != .pointer or a_type_info.pointer.size != .Slice))
@compileError("fn printArray: `a` is " ++ @typeName(a) ++ " , expected an array or a slice");

if (a_type_info == .Pointer and a_type_info.Pointer.child == u8 and a_type_info.Pointer.is_const)
if (a_type_info == .pointer and a_type_info.pointer.child == u8 and a_type_info.pointer.is_const)
{
std.debug.print("\"{s}\" ", .{a});
return;
Expand All @@ -182,17 +182,17 @@ pub fn printArray(a: anytype, shorten_types: bool) void
for (a) |e|
switch (@typeInfo(@TypeOf(e)))
{
.Int, .Float, .ComptimeInt, .ComptimeFloat =>
.int, .float, .comptime_int, .comptime_float =>
std.debug.print("{d}, ", .{ e }),
.Bool => std.debug.print("{}, ", .{ e }),
.Array =>
.bool => std.debug.print("{}, ", .{ e }),
.array =>
{
std.debug.print("[ ", .{});
printArray(e, shorten_types);
std.debug.print("], ", .{});
},
.Struct => printStructInline(e, shorten_types),
.Pointer => |ptr_info|
.@"struct" => printStructInline(e, shorten_types),
.pointer => |ptr_info|
if (ptr_info.size == .Slice)
{
std.debug.print("[ ", .{});
Expand All @@ -201,7 +201,7 @@ pub fn printArray(a: anytype, shorten_types: bool) void
}
else
std.debug.print("-, ", .{}),
.Enum => std.debug.print("{s}, ", .{ @tagName(e) }),
.@"enum" => std.debug.print("{s}, ", .{ @tagName(e) }),
else => std.debug.print("-, ", .{}),
};
}
Expand Down

0 comments on commit d663bf6

Please sign in to comment.