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.

main

main.main
pub fn main(init: process.Init) u8

File

Code

pub fn main(init: process.Init) u8 {
    const gpa = init.gpa;
    const arena = init.arena.allocator();
    const io = init.io;
    const environ_map = init.environ_map;

    const args = init.minimal.args.toSlice(arena) catch {
        std.debug.print("ran out of memory allocating arguments\n", .{});
        if (fast_exit) process.exit(1);
        return 1;
    };

    var zig_integration = false;
    if (args.len > 1 and std.mem.eql(u8, args[1], "--zig-integration")) {
        zig_integration = true;
    }

    const NO_COLOR = std.zig.EnvVar.NO_COLOR.isSet(environ_map);
    const CLICOLOR_FORCE = std.zig.EnvVar.CLICOLOR_FORCE.isSet(environ_map);

    var stderr_buf: [1024]u8 = undefined;
    var stderr = Io.File.stderr().writer(io, &stderr_buf);
    var diagnostics: aro.Diagnostics = switch (zig_integration) {
        false => .{ .output = .{ .to_writer = .{
            .mode = Io.Terminal.Mode.detect(io, stderr.file, NO_COLOR, CLICOLOR_FORCE) catch .no_color,
            .writer = &stderr.interface,
        } } },
        true => .{ .output = .{ .to_list = .{ .arena = .init(gpa) } } },
    };
    defer diagnostics.deinit();

    var comp = aro.Compilation.init(.{
        .gpa = gpa,
        .arena = arena,
        .io = io,
        .diagnostics = &diagnostics,
        .environ_map = environ_map,
    }) catch |err| switch (err) {
        error.OutOfMemory => {
            std.debug.print("ran out of memory initializing C compilation\n", .{});
            if (fast_exit) process.exit(1);
            return 1;
        },
    };
    defer comp.deinit();

    var driver: aro.Driver = .{ .comp = &comp, .diagnostics = &diagnostics, .aro_name = "aro" };
    defer driver.deinit();

    var toolchain: aro.Toolchain = .{ .driver = &driver };
    defer toolchain.deinit();

    translate(&driver, &toolchain, args, zig_integration) catch |err| switch (err) {
        error.OutOfMemory => {
            std.debug.print("ran out of memory translating\n", .{});
            if (fast_exit) process.exit(1);
            return 1;
        },
        error.FatalError => if (zig_integration) {
            serveErrorBundle(arena, io, &diagnostics) catch |bundle_err| {
                std.debug.print("unable to serve error bundle: {}\n", .{bundle_err});
                if (fast_exit) process.exit(1);
                return 1;
            };

            if (fast_exit) process.exit(0);
            return 0;
        } else {
            if (fast_exit) process.exit(1);
            return 1;
        },
        error.WriteFailed => {
            std.debug.print("unable to write to stdout\n", .{});
            if (fast_exit) process.exit(1);
            return 1;
        },
    };
    assert(comp.diagnostics.errors == 0 or !zig_integration);
    if (fast_exit) process.exit(@intFromBool(comp.diagnostics.errors != 0));
    return @intFromBool(comp.diagnostics.errors != 0);
}