~alva/zig-bare

243dfd6cc1bcfcd7dbab34de091c390e05da0395 — owl 2 months ago 2852a5f trunk
keep up with zig
2 files changed, 32 insertions(+), 32 deletions(-)

M interop/go-bare/unmarshal_test.zig
M src/bare.zig
M interop/go-bare/unmarshal_test.zig => interop/go-bare/unmarshal_test.zig +1 -1
@@ 57,7 57,7 @@ test "unmarshal value" {
        var d = decoder(testing.allocator, reader);
        defer d.deinit();
        const res = switch (@typeInfo(@TypeOf(tuple[2]))) {
            .ErrorSet => |_| {
            .error_set => |_| {
                try testing.expectError(tuple[2], d.decode(tuple[1]));
                continue;
            },

M src/bare.zig => src/bare.zig +31 -31
@@ 40,18 40,18 @@ pub fn Decoder(comptime ReaderType: type) type {
        /// Decode a supported BARE type.
        pub fn decode(self: *Self, comptime T: type) !T {
            return switch (@typeInfo(T)) {
                .Int => self.decodeInt(T),
                .Float => self.decodeFloat(T),
                .Bool => self.decodeBool(),
                .Struct => if (comptime isHashMap(T))
                .int => self.decodeInt(T),
                .float => self.decodeFloat(T),
                .bool => self.decodeBool(),
                .@"struct" => if (comptime isHashMap(T))
                    self.decodeHashMap(T)
                else
                    self.decodeStruct(T),
                .Enum => self.decodeEnum(T),
                .Optional => self.decodeOptional(T),
                .Array => self.decodeArray(T),
                .Union => self.decodeUnion(T),
                .Pointer => self.decodePointer(T),
                .@"enum" => self.decodeEnum(T),
                .optional => self.decodeOptional(T),
                .array => self.decodeArray(T),
                .@"union" => self.decodeUnion(T),
                .pointer => self.decodePointer(T),
                else => @compileError("unsupported type " ++ @typeName(T)),
            };
        }


@@ 107,7 107,7 @@ pub fn Decoder(comptime ReaderType: type) type {
        }

        fn decodeStruct(self: *Self, comptime T: type) !T {
            const ti = @typeInfo(T).Struct;
            const ti = @typeInfo(T).@"struct";
            if (ti.fields.len < 1)
                @compileError("structs must have 1 or more fields");



@@ 128,11 128,11 @@ pub fn Decoder(comptime ReaderType: type) type {
            if (0x0 == try self.reader.readByte())
                return null;

            return try self.decode(@typeInfo(T).Optional.child);
            return try self.decode(@typeInfo(T).optional.child);
        }

        fn decodeArray(self: *Self, comptime T: type) !T {
            const ti = @typeInfo(T).Array;
            const ti = @typeInfo(T).array;
            if (ti.len < 1)
                @compileError("array length must be at least 1");



@@ 145,7 145,7 @@ pub fn Decoder(comptime ReaderType: type) type {
        }

        fn decodeUnion(self: *Self, comptime T: type) !T {
            const ti = @typeInfo(T).Union;
            const ti = @typeInfo(T).@"union";

            if (ti.tag_type == null)
                @compileError("only tagged unions are supported");


@@ 163,7 163,7 @@ pub fn Decoder(comptime ReaderType: type) type {
        }

        fn decodePointer(self: *Self, comptime T: type) !T {
            const ti = @typeInfo(T).Pointer;
            const ti = @typeInfo(T).pointer;

            if (ti.size != .Slice)
                @compileError("slices are the only supported pointer type");


@@ 230,18 230,18 @@ pub fn Encoder(comptime WriterType: type) type {
        pub fn encode(self: *Self, value: anytype) !void {
            const T = @TypeOf(value);
            return switch (@typeInfo(T)) {
                .Int => self.encodeInt(T, value),
                .Float => self.encodeFloat(T, value),
                .Bool => self.encodeBool(value),
                .Struct => if (comptime isHashMap(T))
                .int => self.encodeInt(T, value),
                .float => self.encodeFloat(T, value),
                .bool => self.encodeBool(value),
                .@"struct" => if (comptime isHashMap(T))
                    self.encodeHashMap(value)
                else
                    self.encodeStruct(value),
                .Enum => self.encodeEnum(value),
                .Optional => self.encodeOptional(value),
                .Array => self.encodeArray(value),
                .Union => self.encodeUnion(value),
                .Pointer => self.encodePointer(value),
                .@"enum" => self.encodeEnum(value),
                .optional => self.encodeOptional(value),
                .array => self.encodeArray(value),
                .@"union" => self.encodeUnion(value),
                .pointer => self.encodePointer(value),
                else => @compileError("unsupported type " ++ @typeName(T)),
            };
        }


@@ 293,7 293,7 @@ pub fn Encoder(comptime WriterType: type) type {
        }

        fn encodeStruct(self: *Self, value: anytype) !void {
            const ti = @typeInfo(@TypeOf(value)).Struct;
            const ti = @typeInfo(@TypeOf(value)).@"struct";

            if (ti.fields.len < 1)
                @compileError("structs must have 1 or more fields");


@@ 314,7 314,7 @@ pub fn Encoder(comptime WriterType: type) type {
        }

        fn encodeArray(self: *Self, value: anytype) !void {
            const ti = @typeInfo(@TypeOf(value)).Array;
            const ti = @typeInfo(@TypeOf(value)).array;

            if (ti.len < 1)
                @compileError("array length must be at least 1");


@@ 324,7 324,7 @@ pub fn Encoder(comptime WriterType: type) type {
        }

        fn encodePointer(self: *Self, value: anytype) !void {
            const ti = @typeInfo(@TypeOf(value)).Pointer;
            const ti = @typeInfo(@TypeOf(value)).pointer;

            if (ti.size != .Slice)
                @compileError("slices are the only supported pointer type");


@@ 354,7 354,7 @@ pub fn Encoder(comptime WriterType: type) type {

        fn encodeUnion(self: *Self, value: anytype) !void {
            const T = @TypeOf(value);
            const ti = @typeInfo(T).Union;
            const ti = @typeInfo(T).@"union";

            if (ti.tag_type) |TT| {
                const tag = @intFromEnum(value);


@@ 371,7 371,7 @@ pub fn Encoder(comptime WriterType: type) type {

fn isHashMap(comptime T: type) bool {
    switch (@typeInfo(T)) {
        .Struct => {
        .@"struct" => {
            // These are the only parts of the HashMap API that are used.
            // `HashMapKeyType` and `HashMapValueType` add further constraints.
            const has1 = @hasDecl(T, "iterator");


@@ 393,7 393,7 @@ fn HashMapValueType(comptime T: type) type {
}

fn HashMapType(comptime T: type, comptime field_name: []const u8) type {
    const fields = @typeInfo(T.KV).Struct.fields;
    const fields = @typeInfo(T.KV).@"struct".fields;

    inline for (fields) |f|
        if (comptime mem.eql(u8, f.name, field_name))


@@ 402,9 402,9 @@ fn HashMapType(comptime T: type, comptime field_name: []const u8) type {

fn isValidHashMapKeyType(comptime T: type) bool {
    return switch (@typeInfo(T)) {
        .Int, .Float, .Bool, .Enum => true,
        .int, .float, .bool, .@"enum" => true,
        // Strings are allowed, but we don't quite have strings.
        .Pointer => |p| p.size == .Slice,
        .pointer => |p| p.size == .Slice,
        else => false,
    };
}