Something went wrong. Try again.
BIND zone files for Zig 0.16: read one, edit it, write it back
Something went wrong. Try again.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788// SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie <jeff@ocjtech.us>// 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 "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}", .{"x" ** 500}); try testing.expectEqual(d.buffer.len, d.message().len);}