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.

Diagnostics

errors.Diagnostics
pub const Diagnostics = struct

File

Code

pub const Diagnostics = struct {
    errors: std.ArrayList(ErrorDetails) = .empty,
    /// Append-only, cannot handle removing strings.
    /// Expects to own all strings within the list.
    strings: std.ArrayList([]const u8) = .empty,
    allocator: Allocator,

    pub fn init(allocator: Allocator) Diagnostics {
        return .{
            .allocator = allocator,
        };
    }

    pub fn deinit(self: *Diagnostics) void {
        self.errors.deinit(self.allocator);
        for (self.strings.items) |str| {
            self.allocator.free(str);
        }
        self.strings.deinit(self.allocator);
    }

    pub fn append(self: *Diagnostics, error_details: ErrorDetails) !void {
        try self.errors.append(self.allocator, error_details);
    }

    const SmallestStringIndexType = @Int(.unsigned, @min(
        @bitSizeOf(ErrorDetails.FileOpenError.FilenameStringIndex),
        @min(
            @bitSizeOf(ErrorDetails.IconReadError.FilenameStringIndex),
            @bitSizeOf(ErrorDetails.BitmapReadError.FilenameStringIndex),
        ),
    ));

    /// Returns the index of the added string as the SmallestStringIndexType
    /// in order to avoid needing to `@intCast` it at callsites of putString.
    /// Instead, this function will error if the index would ever exceed the
    /// smallest FilenameStringIndex of an ErrorDetails type.
    pub fn putString(self: *Diagnostics, str: []const u8) !SmallestStringIndexType {
        if (self.strings.items.len >= std.math.maxInt(SmallestStringIndexType)) {
            return error.OutOfMemory; // ran out of string indexes
        }
        const dupe = try self.allocator.dupe(u8, str);
        const index = self.strings.items.len;
        try self.strings.append(self.allocator, dupe);
        return @intCast(index);
    }

    pub fn renderToStderr(self: *Diagnostics, io: Io, cwd: Io.Dir, source: []const u8, source_mappings: ?SourceMappings) Io.Cancelable!void {
        const stderr = try io.lockStderr(&.{}, null);
        defer io.unlockStderr();
        for (self.errors.items) |err_details| {
            renderErrorMessage(io, stderr.terminal(), cwd, err_details, source, self.strings.items, source_mappings) catch return;
        }
    }

    pub fn contains(self: *const Diagnostics, err: ErrorDetails.Error) bool {
        for (self.errors.items) |details| {
            if (details.err == err) return true;
        }
        return false;
    }

    pub fn containsAny(self: *const Diagnostics, errors: []const ErrorDetails.Error) bool {
        for (self.errors.items) |details| {
            for (errors) |err| {
                if (details.err == err) return true;
            }
        }
        return false;
    }
}