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.

printValue

ScannedConfig.printValue
fn printValue(sc: *const ScannedConfig, s: *Serializer, comptime Field: type, field_value: Field) !void

File

lib/compiler/Maker/ScannedConfig.zig:59

Code

fn printValue(sc: *const ScannedConfig, s: *Serializer, comptime Field: type, field_value: Field) !void {
    const c = &sc.configuration;
    switch (Field) {
        Configuration.String => {
            try s.value(field_value.slice(c), .{});
        },
        Configuration.Deps.Index => {
            try printValue(sc, s, []const Configuration.Step.Index, field_value.get(c).steps.slice);
        },
        Configuration.MaxRss => {
            try s.value(field_value.toBytes(), .{});
        },
        Configuration.Step.Run.Arg.Index => {
            var sub_struct = try s.beginStruct(.{});
            try printStruct(sc, &sub_struct, Configuration.Step.Run.Arg, field_value.get(c));
            try sub_struct.end();
        },
        Configuration.Step.ObjCopy.UpdateSection.Flags => {
            var sub_struct = try s.beginStruct(.{});
            try printStruct(sc, &sub_struct, Field, field_value);
            try sub_struct.end();
        },
        Configuration.LazyPath.Index => {
            switch (field_value.get(c)) {
                inline else => |u| {
                    var sub_struct = try s.beginStruct(.{});
                    try printStruct(sc, &sub_struct, @TypeOf(u), u);
                    try sub_struct.end();
                },
            }
        },
        else => switch (@typeInfo(Field)) {
            .int => try s.int(field_value),
            .pointer => |info| switch (info.size) {
                .slice => {
                    var slice_field = try s.beginTuple(.{});
                    for (field_value) |elem| {
                        try slice_field.fieldPrefix();
                        try printValue(sc, s, info.child, elem);
                    }
                    try slice_field.end();
                },
                else => comptime unreachable,
            },
            .@"enum" => {
                if (@hasDecl(Field, "storage")) switch (Field.storage) {
                    .extended => {
                        var sub_struct = try s.beginStruct(.{});
                        switch (field_value.get(c.extra)) {
                            inline else => |u| {
                                try printStruct(sc, &sub_struct, @TypeOf(u), u);
                            },
                        }
                        try sub_struct.end();
                    },
                    .flag_optional => comptime unreachable,
                    .flag_length_prefixed_list => comptime unreachable,
                    .enum_optional => comptime unreachable,
                    .union_list => comptime unreachable,
                    .length_prefixed_list => comptime unreachable,
                    .flag_list => comptime unreachable,
                    .flag_union => comptime unreachable,
                    .multi_list => comptime unreachable,
                } else if (std.enums.tagName(Field, field_value)) |name| {
                    try s.ident(name);
                } else {
                    try s.int(@backingInt(field_value));
                }
            },
            .@"struct" => |info| switch (info.layout) {
                .@"packed" => {
                    try s.value(field_value, .{});
                },
                .@"extern" => {
                    var sub_struct = try s.beginStruct(.{});
                    try printStruct(sc, &sub_struct, Field, field_value);
                    try sub_struct.end();
                },
                .auto => switch (Field.storage) {
                    .flag_optional, .enum_optional => {
                        if (field_value.value) |some| {
                            try printValue(sc, s, Field.Value, some);
                        } else {
                            try s.value(null, .{});
                        }
                    },
                    .length_prefixed_list, .flag_length_prefixed_list, .flag_list => {
                        try printValue(sc, s, @TypeOf(field_value.slice), field_value.slice);
                    },
                    .extended => @compileError("TODO"),
                    .union_list => {
                        var slice_field = try s.beginTuple(.{});
                        for (field_value.slice(c.extra), 0..) |elem, i| switch (field_value.tag(c.extra, i)) {
                            inline else => |tag| {
                                var sub_struct = try s.beginStruct(.{});
                                try sub_struct.fieldPrefix(@tagName(tag));
                                try printValue(sc, s, @FieldType(Field.Union, @tagName(tag)), @fromBackingInt(@intCast(elem)));
                                try sub_struct.end();
                            },
                        };
                        try slice_field.end();
                    },
                    .flag_union => try printValue(sc, s, Field.Union, field_value.u),
                    .multi_list => @compileError("TODO"),
                },
            },
            .@"union" => {
                try printTaggedUnion(sc, s, field_value);
            },
            else => @compileError("not implemented: " ++ @typeName(Field)),
        },
    }
}