Zig 0.17.0-dev (Split by item)

This is an example of documentation generated by ZigDoc, an alternative to Zig's built-in Auto Doc feature. See also examples in other modes/formats. The project being documented here (as the example) is the Zig library itself.

EntryHeader

git.EntryHeader
const EntryHeader = union(Type)

File

Code

const EntryHeader = union(Type) {
    commit: Undeltified,
    tree: Undeltified,
    blob: Undeltified,
    tag: Undeltified,
    ofs_delta: OfsDelta,
    ref_delta: RefDelta,

    const Type = enum(u3) {
        commit = 1,
        tree = 2,
        blob = 3,
        tag = 4,
        ofs_delta = 6,
        ref_delta = 7,
    };

    const Undeltified = struct {
        uncompressed_length: u64,
    };

    const OfsDelta = struct {
        offset: u64,
        uncompressed_length: u64,
    };

    const RefDelta = struct {
        base_object: Oid,
        uncompressed_length: u64,
    };

    fn objectType(header: EntryHeader) Object.Type {
        return switch (header) {
            inline .commit, .tree, .blob, .tag => |_, tag| @field(Object.Type, @tagName(tag)),
            else => unreachable,
        };
    }

    fn uncompressedLength(header: EntryHeader) u64 {
        return switch (header) {
            inline else => |entry| entry.uncompressed_length,
        };
    }

    fn read(format: Oid.Format, reader: *Io.Reader) !EntryHeader {
        const InitialByte = packed struct { len: u4, type: u3, has_next: bool };
        const initial: InitialByte = @bitCast(reader.takeByte() catch |e| switch (e) {
            error.EndOfStream => return error.InvalidFormat,
            else => |other| return other,
        });
        const rest_len = if (initial.has_next) try reader.takeLeb128(u64) else 0;
        var uncompressed_length: u64 = initial.len;
        uncompressed_length |= std.math.shlExact(u64, rest_len, 4) catch return error.InvalidFormat;
        const @"type" = std.enums.fromInt(EntryHeader.Type, initial.type) orelse return error.InvalidFormat;
        return switch (@"type") {
            inline .commit, .tree, .blob, .tag => |tag| @unionInit(EntryHeader, @tagName(tag), .{
                .uncompressed_length = uncompressed_length,
            }),
            .ofs_delta => .{ .ofs_delta = .{
                .offset = try readOffsetVarInt(reader),
                .uncompressed_length = uncompressed_length,
            } },
            .ref_delta => .{ .ref_delta = .{
                .base_object = Oid.readBytes(format, reader) catch |e| switch (e) {
                    error.EndOfStream => return error.InvalidFormat,
                    else => |other| return other,
                },
                .uncompressed_length = uncompressed_length,
            } },
        };
    }
}