From 7747278b6311c03d1094c4734324f5f9d6edb11e Mon Sep 17 00:00:00 2001 From: Vivianne Langdon Date: Mon, 17 Jul 2023 23:58:55 -0700 Subject: [PATCH] Cleaning up boilerplate --- src/main.zig | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/src/main.zig b/src/main.zig index 9a02ec6..b199dab 100644 --- a/src/main.zig +++ b/src/main.zig @@ -106,6 +106,12 @@ const Serializer = struct { const Ok = void; const Error = getty.ser.Error || std.mem.Allocator.Error; + pub fn init(writer: std.ArrayList(u8).Writer) Self { + return Self{ + .writer = writer, + }; + } + pub fn serializeGenericString(comptime joiner: []const u8, writer: std.ArrayList(u8).Writer, value: anytype) Error!Ok { try writer.print("{d}" ++ joiner, .{value.len}); try writer.print("{s}", .{value}); @@ -166,7 +172,7 @@ const Seq = struct { const Error = Serializer.Error; fn serializeElement(self: Self, value: anytype) Error!void { - try getty.serialize(null, value, (Serializer{ .writer = self.writer }).serializer()); + try getty.serialize(null, value, Serializer.init(self.writer).serializer()); } fn end(self: Self) Error!Ok { @@ -193,7 +199,7 @@ const Struct = struct { fn serializeField(self: Self, comptime field: []const u8, value: anytype) Error!void { try Serializer.serializeGenericString("'", self.writer, field); - try getty.serialize(null, value, (Serializer{ .writer = self.writer }).serializer()); + try getty.serialize(null, value, Serializer.init(self.writer).serializer()); } fn end(self: Self) Error!Ok { @@ -204,7 +210,7 @@ const Struct = struct { test "serialize bool" { var arr = std.ArrayList(u8).init(std.testing.allocator); defer arr.deinit(); - var s = (Serializer{ .writer = arr.writer() }).serializer(); + var s = Serializer.init(arr.writer()).serializer(); try getty.serialize(null, true, s); try getty.serialize(null, false, s); @@ -216,7 +222,7 @@ test "serialize bool" { test "serialize ints" { var arr = std.ArrayList(u8).init(std.testing.allocator); defer arr.deinit(); - var s = (Serializer{ .writer = arr.writer() }).serializer(); + var s = Serializer.init(arr.writer()).serializer(); try getty.serialize(null, 42, s); try getty.serialize(null, 86, s); @@ -228,7 +234,7 @@ test "serialize ints" { test "serialize floats" { var arr = std.ArrayList(u8).init(std.testing.allocator); defer arr.deinit(); - var s = (Serializer{ .writer = arr.writer() }).serializer(); + var s = Serializer.init(arr.writer()).serializer(); try getty.serialize(null, @as(f64, 42.42), s); try getty.serialize(null, @as(f32, 42.42), s); @@ -238,7 +244,7 @@ test "serialize floats" { test "serialize bytestrs" { var arr = std.ArrayList(u8).init(std.testing.allocator); defer arr.deinit(); - var s = (Serializer{ .writer = arr.writer() }).serializer(); + var s = Serializer.init(arr.writer()).serializer(); try getty.serialize(null, "hello", s); try getty.serialize(null, "my name is vivi 💖", s); @@ -249,7 +255,7 @@ test "serialize bytestrs" { test "serialize strs" { var arr = std.ArrayList(u8).init(std.testing.allocator); defer arr.deinit(); - var s = (Serializer{ .writer = arr.writer() }).serializer(); + var s = Serializer.init(arr.writer()).serializer(); try getty.serialize(null, try Utf8String.init("hello"), s); try getty.serialize(null, try Utf8String.init("my name is vivi 💖"), s); @@ -266,7 +272,7 @@ test "serialize enum, aka symbol" { var arr = std.ArrayList(u8).init(std.testing.allocator); defer arr.deinit(); - var s = (Serializer{ .writer = arr.writer() }).serializer(); + var s = Serializer.init(arr.writer()).serializer(); try getty.serialize(null, Testing.cool, s); @@ -282,7 +288,7 @@ test "serialize struct" { var arr = std.ArrayList(u8).init(std.testing.allocator); defer arr.deinit(); - var s = (Serializer{ .writer = arr.writer() }).serializer(); + var s = Serializer.init(arr.writer()).serializer(); try getty.serialize(null, Testing{ .val1 = 25, @@ -302,7 +308,7 @@ test "serialize mixed" { var arr = std.ArrayList(u8).init(std.testing.allocator); defer arr.deinit(); - var s = (Serializer{ .writer = arr.writer() }).serializer(); + var s = Serializer.init(arr.writer()).serializer(); try getty.serialize(null, 42, s); try getty.serialize(null, "my name is vivi 💖", s); @@ -323,7 +329,7 @@ test "serialize record" { var arr = std.ArrayList(u8).init(std.testing.allocator); defer arr.deinit(); - var s = (Serializer{ .writer = arr.writer() }).serializer(); + var s = Serializer.init(arr.writer()).serializer(); try getty.serialize(null, rec, s); @@ -344,7 +350,7 @@ test "serialize record" { // var arr = std.ArrayList(u8).init(std.testing.allocator); // defer arr.deinit(); -// var s = (Serializer{ .writer = arr.writer() }).serializer(); +// var s = Serializer.init(arr.writer()).serializer(); // var mapCat = std.StringArrayHashMap(void).init(std.testing.allocator); // defer mapCat.deinit();