A pretty printer for zig
zig
at aa04716ea6ccca41ebbaa5579f92d4bca6a89f11 246 lines 7.4 kB view raw
1const std = @import("std"); 2const print = std.debug.print; 3 4const build_options = @import("build-options"); 5 6const pretty_mod = @import("pretty"); 7const pretty = pretty_mod.pretty; 8const prettyO = pretty_mod.prettyO; 9 10pub const pretty_options = pretty_mod.Options{ 11 .skip_root_type_name = build_options.skip_root_type_name, 12 .theme = .{ 13 .indent_width = build_options.indent_width, 14 }, 15}; 16 17const employees = @embedFile("employees.json"); 18 19const Hello = enum { world, developer }; 20 21const Gender = enum(u8) { male, female, nonbinary, other, _ }; 22 23const Person = struct { 24 age: u8, 25 gender: ?Gender, 26 27 pub fn pretty(self: *const @This(), comptime ctx: pretty_mod.Context, run: *const pretty_mod.Runtime) !void { 28 run.setColor(ctx, .dim); 29 try run.write("AGE = "); 30 run.resetColor(); 31 try run.print("{d}", .{self.age}); 32 run.setColor(ctx, .dim); 33 try run.write(", GENDER = "); 34 run.resetColor(); 35 try run.print("{?}", .{self.gender}); 36 } 37}; 38 39const Nested = struct { 40 a: ChildA, 41 b: ChildB, 42}; 43 44const ChildA = struct { 45 child: ChildC, 46}; 47 48const ChildB = struct { 49 hello: Hello, 50}; 51 52const ChildC = struct { 53 person: [2]Person, 54}; 55 56const Employee = struct { 57 id: []const u8, 58 name: []const u8, 59 position: []const u8, 60 department: Department, 61 projects: []Project, 62}; 63 64const Department = struct { 65 id: []const u8, 66 name: []const u8, 67 manager: Manager, 68 69 const Manager = struct { 70 id: []const u8, 71 name: []const u8, 72 contact: Contact, 73 74 const Contact = struct { 75 email: []const u8, 76 phone: []const u8, 77 }; 78 }; 79}; 80 81const Project = struct { 82 projectId: []const u8, 83 projectName: []const u8, 84 startDate: []const u8, 85 tasks: []Task, 86 87 const Task = struct { 88 taskId: []const u8, 89 title: []const u8, 90 status: []const u8, 91 details: Details, 92 93 const Details = struct { 94 hoursSpent: u32, 95 technologiesUsed: [][]const u8, 96 expectedCompletion: []const u8, 97 }; 98 }; 99}; 100 101const JsonData = union(enum) { 102 employee: Employee, 103}; 104 105const ErrorSet = error{ OutOfMemory, WriteFailed }; 106const Result = ErrorSet!Hello; 107 108const HelloResult = union(ResultType) { 109 const ResultType = enum { ok, err }; 110 111 ok: Hello, 112 err: ErrorSet, 113}; 114 115pub fn main(init: std.process.Init) !void { 116 print("Pretty type - {f}\n", .{pretty(Hello)}); 117 print("Pretty null - {f}\n", .{pretty(null)}); 118 119 print("\nBooleans\n", .{}); 120 print("Pretty bool true - {f}\n", .{pretty(true)}); 121 print("Pretty bool false - {f}\n", .{pretty(false)}); 122 print( 123 "Pretty bool true without type name - {f}\n", 124 .{prettyO(true, .{ 125 .skip_root_type_name = true, 126 .theme = .{ .type_value_sep = ": " }, 127 })}, 128 ); 129 130 print("\nUnsigned Integers\n", .{}); 131 print("Pretty u8 - {f}\n", .{pretty(@as(u8, 42))}); 132 print("Pretty u16 - {f}\n", .{pretty(@as(u16, 42))}); 133 print("Pretty u32 - {f}\n", .{pretty(@as(u32, 42))}); 134 print("Pretty u64 - {f}\n", .{pretty(@as(u64, 42))}); 135 print("Pretty usize - {f}\n", .{pretty(@as(usize, 42))}); 136 137 print("\nSigned Integers\n", .{}); 138 print("Pretty comptime_int - {f}\n", .{pretty(42)}); 139 print("Pretty i8 - {f}\n", .{pretty(@as(i8, 42))}); 140 print("Pretty i16 - {f}\n", .{pretty(@as(i16, 42))}); 141 print("Pretty i32 - {f}\n", .{pretty(@as(i32, 42))}); 142 print("Pretty i64 - {f}\n", .{pretty(@as(i64, 42))}); 143 print("Pretty isize - {f}\n", .{pretty(@as(isize, 42))}); 144 145 print("\nFloats\n", .{}); 146 print("Pretty comptime_float - {f}\n", .{pretty(3.131)}); 147 print("Pretty f16 - {f}\n", .{pretty(@as(f16, 3.141))}); 148 print("Pretty f32 - {f}\n", .{pretty(@as(f32, 3.141))}); 149 print("Pretty f64 - {f}\n", .{pretty(@as(f64, 3.141))}); 150 151 print("\nEnums\n", .{}); 152 print("Pretty enum - {f}\n", .{pretty(Hello.world)}); 153 print("Pretty enum literal - {f}\n", .{pretty(.hello_world)}); 154 155 const opt_null: ?Hello = null; 156 const opt_not_null: ?Hello = .developer; 157 158 print("\nOptionals\n", .{}); 159 print("Pretty optional = null - {f}\n", .{pretty(opt_null)}); 160 print("Pretty optional = not null - {f}\n", .{pretty(opt_not_null)}); 161 162 const person = Person{ .age = 13, .gender = null }; 163 164 const nested = Nested{ 165 .a = .{ .child = .{ .person = .{ person, Person{ 166 .age = 42, 167 .gender = .nonbinary, 168 } } } }, 169 .b = .{ .hello = .world }, 170 }; 171 172 print("\nStructs\n", .{}); 173 print("Pretty simple struct - {f}\n", .{pretty(person)}); 174 print( 175 "Pretty simple inline struct - {f}\n", 176 .{prettyO(person, .{ .struct_inline = true })}, 177 ); 178 print("Pretty nested struct - {f}\n", .{pretty(nested)}); 179 180 const simple_union = HelloResult{ .ok = .world }; 181 182 print("\nUnions\n", .{}); 183 print("Pretty simple union - {f}\n", .{pretty(simple_union)}); 184 185 print("\nArrays\n", .{}); 186 print("Pretty array of floats - {f}\n", .{pretty([_]f32{ 0.1, 0.2, 0.3 })}); 187 print( 188 "Pretty array of floats inline - {f}\n", 189 .{prettyO([3]f32{ 0.1, 0.2, 0.3 }, .{ .array_inline = true })}, 190 ); 191 print( 192 "Pretty array of floats inline with indices - {f}\n", 193 .{prettyO([3]f32{ 0.1, 0.2, 0.3 }, .{ 194 .array_inline = true, 195 .array_always_show_index = true, 196 })}, 197 ); 198 199 const zig_logo_color: @Vector(4, u8) = .{ 247, 164, 29, 255 }; 200 201 print("\nVectors\n", .{}); 202 print("Pretty vector of u8s - {f}\n", .{pretty(zig_logo_color)}); 203 204 const ptr_array = [_]f32{ 0.1, 0.3, 0.9 }; 205 const persons_allocated = try init.gpa.alloc(Person, 10); 206 defer init.gpa.free(persons_allocated); 207 208 var str_allocated = try init.gpa.alloc(u8, 5); 209 defer init.gpa.free(str_allocated); 210 str_allocated[0] = 'H'; 211 str_allocated[1] = 'e'; 212 str_allocated[2] = 'l'; 213 str_allocated[3] = 'l'; 214 str_allocated[4] = 'o'; 215 216 var buffer: [100]u8 = [_]u8{1} ** 100; 217 const buffer_ptr: *[100]u8 = &buffer; 218 const buffer_many_ptr: [*]u8 = buffer_ptr; 219 220 print("\nPointers\n", .{}); 221 print("Pretty string - {f}\n", .{pretty("Hello World!")}); 222 print("Pretty ptr to an array - {f}\n", .{pretty(&ptr_array)}); 223 print("Pretty ptr to nested struct - {f}\n", .{pretty(&nested)}); 224 print("Pretty ptr to slice of structs - {f}\n", .{pretty(persons_allocated)}); 225 print("Pretty ptr to slice of u8 - {f}\n", .{pretty(str_allocated)}); 226 print("Pretty ptr to buffer many ptr - {f}\n", .{pretty(buffer_many_ptr)}); 227 228 const eu_error: Result = error.OutOfMemory; 229 const eu_ok: Result = .world; 230 231 print("\nError types\n", .{}); 232 print("Pretty error set - {f}\n", .{pretty(ErrorSet.OutOfMemory)}); 233 print("Pretty error union error - {f}\n", .{pretty(eu_error)}); 234 print("Pretty error union ok - {f}\n", .{pretty(eu_ok)}); 235 236 print("\nFunctions\n", .{}); 237 print("Pretty function pretty - {f}\n", .{pretty(pretty)}); 238 print("Pretty function main - {f}\n", .{pretty(main)}); 239 240 const parsed: std.json.Parsed(JsonData) = try std.json.parseFromSlice(JsonData, init.gpa, employees, .{}); 241 defer parsed.deinit(); 242 243 print("\nJson\n", .{}); 244 print("Data - {f}\n", pretty(pretty(employees))); 245 print("{f}\n", .{pretty(parsed.value)}); 246}