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.

ErrorHandler

main.ErrorHandler
const ErrorHandler = union(enum)

File

Code

const ErrorHandler = union(enum) {
    server: std.zig.Server,
    stderr,

    pub fn emitCliDiagnostics(
        self: *ErrorHandler,
        allocator: Allocator,
        io: Io,
        args: []const []const u8,
        diagnostics: *cli.Diagnostics,
    ) !void {
        switch (self.*) {
            .server => |*server| {
                var error_bundle = try cliDiagnosticsToErrorBundle(allocator, diagnostics);
                defer error_bundle.deinit(allocator);

                try server.serveErrorBundle(error_bundle);
            },
            .stderr => return diagnostics.renderToStderr(io, args),
        }
    }

    pub fn emitAroDiagnostics(
        self: *ErrorHandler,
        allocator: Allocator,
        fail_msg: []const u8,
        comp: *aro.Compilation,
    ) !void {
        const io = comp.io;
        switch (self.*) {
            .server => |*server| {
                var error_bundle = try compiler_util.aroDiagnosticsToErrorBundle(
                    comp.diagnostics,
                    allocator,
                    fail_msg,
                );
                defer error_bundle.deinit(allocator);

                try server.serveErrorBundle(error_bundle);
            },
            .stderr => {
                // aro errors have already been emitted
                const stderr = try io.lockStderr(&.{}, null);
                defer io.unlockStderr();
                try renderErrorMessage(stderr.terminal(), .err, "{s}", .{fail_msg});
            },
        }
    }

    pub fn emitDiagnostics(
        self: *ErrorHandler,
        allocator: Allocator,
        io: Io,
        cwd: Io.Dir,
        source: []const u8,
        diagnostics: *Diagnostics,
        mappings: SourceMappings,
    ) !void {
        switch (self.*) {
            .server => |*server| {
                var error_bundle = try diagnosticsToErrorBundle(allocator, source, diagnostics, mappings);
                defer error_bundle.deinit(allocator);

                try server.serveErrorBundle(error_bundle);
            },
            .stderr => return diagnostics.renderToStderr(io, cwd, source, mappings),
        }
    }

    pub fn emitMessage(
        self: *ErrorHandler,
        allocator: Allocator,
        io: Io,
        msg_type: @import("utils.zig").ErrorMessageType,
        comptime format: []const u8,
        args: anytype,
    ) !void {
        switch (self.*) {
            .server => |*server| {
                // only emit errors
                if (msg_type != .err) return;

                var error_bundle = try errorStringToErrorBundle(allocator, format, args);
                defer error_bundle.deinit(allocator);

                try server.serveErrorBundle(error_bundle);
            },
            .stderr => {
                const stderr = try io.lockStderr(&.{}, null);
                defer io.unlockStderr();
                try renderErrorMessage(stderr.terminal(), msg_type, format, args);
            },
        }
    }
}