// SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie // SPDX-License-Identifier: MIT //! Where it went wrong, and what was wrong with it. //! //! A Zig error is a name and nothing else, and `error.BadNumber` is not enough //! to fix a zone file with. Hand a `Diagnostics` to `Parser` or `Zone.parse` //! and it fills in the line, the column, and a sentence -- and it does so //! without allocating, because the thing being reported may well be that there //! was no memory: //! //! ```zig //! var diagnostics: zone.Diagnostics = .{}; //! var z = zone.Zone.parse(gpa, &reader, .{ .diagnostics = &diagnostics }) catch |err| { //! std.debug.print("{s}: {f}\n", .{ @errorName(err), diagnostics }); //! return err; //! }; //! ``` //! //! Nothing here is load-bearing: the same errors come back whether or not a //! diagnostics is given, and a message that did not fit is truncated rather //! than reported as a second failure. const std = @import("std"); const Writer = std.Io.Writer; const Diagnostics = @This(); /// The line the entry that failed began on, counting from one. Zero when /// nothing has failed yet. line: u32 = 0, /// The column, counting from one. column: u32 = 0, /// The name of the file the line is in, when the caller has said what to call /// it. `$INCLUDE` makes this worth having: without it a line number names a /// line in a file nobody can identify. file: ?[]const u8 = null, buffer: [220]u8 = undefined, len: usize = 0, /// What went wrong, in a sentence, or the empty string if nothing has been /// said about it. pub fn message(self: *const Diagnostics) []const u8 { return self.buffer[0..self.len]; } /// Records where the reader was. The parser calls this as each entry begins, /// so that whatever fails is attributed to the entry it failed in rather than /// to the token the failure was noticed at. pub fn at(self: *Diagnostics, line: u32, column: u32) void { self.line = line; self.column = column; } /// Records what was wrong. A message too long for the buffer is cut short -- /// this is a diagnostic, and a truncated one beats a second failure. pub fn note(self: *Diagnostics, comptime fmt: []const u8, args: anytype) void { var w = Writer.fixed(&self.buffer); w.print(fmt, args) catch {}; self.len = w.buffered().len; } /// `file:line:column: message`, with whichever of those are known. pub fn format(self: Diagnostics, w: *Writer) Writer.Error!void { if (self.file) |f| try w.print("{s}:", .{f}); if (self.line != 0) try w.print("{d}:{d}: ", .{ self.line, self.column }); try w.writeAll(self.message()); } const testing = std.testing; test message { var d: Diagnostics = .{}; try testing.expectEqualStrings("", d.message()); d.note("no record type is called \"{s}\"", .{"AAA"}); try testing.expectEqualStrings("no record type is called \"AAA\"", d.message()); } test at { var d: Diagnostics = .{}; d.at(3, 1); d.note("missing the address", .{}); try testing.expectFmt("3:1: missing the address", "{f}", .{d}); } test note { var d: Diagnostics = .{}; // Too long for the buffer, and cut short rather than refused. d.note("{s}", .{@as([300]u8, @splat('x'))}); try testing.expectEqual(d.buffer.len, d.message().len); } test format { var d: Diagnostics = .{ .file = "db.example.com" }; d.at(12, 5); d.note("not a number: \"{s}\"", .{"soon"}); try testing.expectFmt("db.example.com:12:5: not a number: \"soon\"", "{f}", .{d}); } test "a diagnostic prints as a compiler error does" { var d: Diagnostics = .{ .file = "example.com.zone" }; d.at(12, 5); d.note("expected a number, found \"{s}\"", .{"soon"}); try testing.expectFmt( "example.com.zone:12:5: expected a number, found \"soon\"", "{f}", .{d}, ); } test "a message longer than the buffer is cut short rather than lost" { var d: Diagnostics = .{}; d.note("{s}", .{&@as([500]u8, @splat('x'))}); try testing.expectEqual(d.buffer.len, d.message().len); }