5fd122a554077f3bf0036561420c241da8ea8949 — Noah Graff 3 months ago 9fdffc8
stopped using anyerror
1 files changed, 14 insertions(+), 13 deletions(-)

M src/serialize.zig
M src/serialize.zig => src/serialize.zig +14 -13
@@ 5,15 5,16 @@
 const Allocator = std.mem.Allocator;
 
 // TODO:
-// * change anyerror on all these functions to an error set.
-// * fix @compileError message to Expected <type(s)>, found <type>.
+// * fix union field access in serializeUnion
+
+const Error = error{OutOfMemory};
 
 // In a struct you can declare a function with the name 'serialize' or
 // 'serializeJson' with the signature fn(value: <struct type>, allocator: *Allocator) Error!Value
 // to use that instead of the default way of serializing a struct. If you do
 // you have to use the provided allocator to allocate, not one in the struct.
 // if the struct has both serialize and serializeJson it will use serializeJson.
-pub fn serialize(allocator: *Allocator, comptime T: type, value: T) anyerror!ValueTree {
+pub fn serialize(allocator: *Allocator, comptime T: type, value: T) Error!ValueTree {
     var arena = std.heap.ArenaAllocator.init(allocator);
     errdefer arena.deinit();
 


@@ 27,7 28,7 @@
 
 /// If you want a ValueTree instead, use serialize
 /// user is responsible for freeing memory
-pub fn serializeValue(allocator: *Allocator, comptime T: type, value: T) anyerror!Value {
+pub fn serializeValue(allocator: *Allocator, comptime T: type, value: T) Error!Value {
     switch (@typeId(T)) {
         .Null => return Value{ .Null = {} },
         .Bool => return Value{ .Bool = value },


@@ 47,7 48,7 @@
     }
 }
 
-fn serializeUnion(allocator: *Allocator, comptime T: type, value: T) anyerror!Value {
+fn serializeUnion(allocator: *Allocator, comptime T: type, value: T) Error!Value {
     const info = switch (@typeInfo(T)) {
         .Union => |i| i,
         else => @compileError("Expected union, found '" ++ @typeName(T) ++ "'"),


@@ 75,11 76,11 @@
     std.debug.panic("Should not have gotten here!");
 }
 
-fn serializeStruct(allocator: *Allocator, comptime T: type, value: T) anyerror!Value {
+fn serializeStruct(allocator: *Allocator, comptime T: type, value: T) Error!Value {
     const hasFn = std.meta.trait.hasFn;
     const info = switch (@typeInfo(T)) {
         .Struct => |i| i,
-        else => @compileError(@typeName(T) ++ " is not a struct!"),
+        else => @compileError("Expected a struct, found '" ++ @typeName(T) ++ "'"),
     };
 
     if (comptime hasFn("serializeJson")(T)) {


@@ 103,10 104,10 @@
 /// treats the pointer as it's child type if the size is one,
 /// if size > 1 returns a Value.String if the child type is u8
 /// otherwise this will return a Value.Array
-fn serializePointer(allocator: *Allocator, comptime T: type, value: T) anyerror!Value {
+fn serializePointer(allocator: *Allocator, comptime T: type, value: T) Error!Value {
     const info = switch (@typeInfo(T)) {
         .Pointer => |i| i,
-        else => @compileError(@typeName(T) ++ " is not a pointer!"),
+        else => @compileError("Expected a pointer or slice, found '" ++ @typeName(T) ++ "'"),
     };
 
     switch (info.size) {


@@ 126,10 127,10 @@
     }
 }
 
-fn serializeArray(allocator: *Allocator, comptime T: type, value: T) anyerror!Value {
+fn serializeArray(allocator: *Allocator, comptime T: type, value: T) Error!Value {
     const info = switch (@typeInfo(T)) {
         .Array => |i| i,
-        else => @compileError(@typeName(T) ++ " is not an array!"),
+        else => @compileError("Expected array found '" ++ @typeName(T) ++ "'"),
     };
 
     if (info.child == u8) {


@@ 251,7 252,7 @@
         last_name: []const u8,
         age: u8,
 
-        pub fn serializeJson(self: Self, allocator: *Allocator) anyerror!Value {
+        pub fn serializeJson(self: Self, allocator: *Allocator) Error!Value {
             var object = ObjectMap.init(allocator);
             _ = try object.put(self.first_name, try serializeValue(allocator, u8, self.age));
             return Value{ .Object = object };


@@ 268,7 269,7 @@
         last_name: []const u8,
         age: u8,
 
-        pub fn serialize(self: Self, allocator: *Allocator) anyerror!Value {
+        pub fn serialize(self: Self, allocator: *Allocator) Error!Value {
             var object = ObjectMap.init(allocator);
             _ = try object.put(self.last_name, try serializeValue(allocator, u8, self.age));
             return Value{ .Object = object };