feature. See also
. The project being documented here (as the example) is the Zig library itself.
errors.Diagnostics
pub const Diagnostics = struct
File
Code
pub const Diagnostics = struct {
errors: std.ArrayList(ErrorDetails) = .empty,
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),
),
));
pub fn putString(self: *Diagnostics, str: []const u8) !SmallestStringIndexType {
if (self.strings.items.len >= std.math.maxInt(SmallestStringIndexType)) {
return error.OutOfMemory;
}
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;
}
}