doc comment/function cleanup + compileError for missing srfFormat
All checks were successful
Generic zig build / build (push) Successful in 20s

This commit is contained in:
Emil Lerch 2026-05-29 11:32:31 -07:00
parent f149909075
commit d5a94b0ca8
Signed by: lobo
GPG key ID: A7B62D657EF764F8

View file

@ -797,8 +797,7 @@ pub const RecordIterator = struct {
/// defaults. Missing fields without defaults return an error. /// defaults. Missing fields without defaults return an error.
/// ///
/// For tagged unions, the active tag field must appear first in the /// For tagged unions, the active tag field must appear first in the
/// stream (unlike `Record.to` which can do random access). The tag /// stream. The tag field name defaults to `"type"` or `T.srf_tag_field` if
/// field name defaults to `"type"` or `T.srf_tag_field` if
/// declared. /// declared.
pub fn to(self: FieldIterator, comptime T: type, options: CoercionOptions) !T { pub fn to(self: FieldIterator, comptime T: type, options: CoercionOptions) !T {
const ti = @typeInfo(T); const ti = @typeInfo(T);
@ -889,9 +888,7 @@ pub const RecordIterator = struct {
return error.CoercionNotPossible; return error.CoercionNotPossible;
} }
test to { test to {
// Example: coerce fields directly into a Zig struct from the iterator, // Example: coerce fields directly into a Zig struct from the iterator
// without collecting into an intermediate Record. This is the most
// allocation-efficient path for typed deserialization.
const Data = struct { const Data = struct {
name: []const u8, name: []const u8,
score: u8, score: u8,
@ -1119,8 +1116,7 @@ const Directive = union(enum) {
return null; return null;
} }
}; };
/// Options controlling SRF output formatting. Used by `fmt`, `fmtFrom`, /// Options controlling SRF output formatting. Used by `fmt`
/// `Record.fmt`, and related formatters.
pub const FormatOptions = struct { pub const FormatOptions = struct {
/// When `true`, fields are separated by newlines and records by blank /// When `true`, fields are separated by newlines and records by blank
/// lines (`#!long` format). When `false` (default), fields are /// lines (`#!long` format). When `false` (default), fields are
@ -1149,13 +1145,9 @@ pub const FormatOptions = struct {
}; };
/// Returns a formatter for writing typed Zig values directly to SRF format. /// Returns a formatter for writing typed Zig values directly to SRF format.
/// Each value is converted to a `Record` via `Record.from` and written to /// Custom serialization is supported via the `srfFormat` method convention
/// the output. Custom serialization is supported via the `srfFormat` method /// on struct/union fields.
/// convention on struct/union fields. pub fn fmt(comptime T: type, items: []const T, options: FormatOptions) FromFormatter(T) {
///
/// The `allocator` is used only for fields that require custom formatting
/// (via `srfFormat`). A `FixedBufferAllocator` is recommended for this purpose.
pub fn fmtFrom(comptime T: type, items: []const T, options: FormatOptions) FromFormatter(T) {
return .{ .items = items, .options = options }; return .{ .items = items, .options = options };
} }
pub fn FromFormatter(comptime T: type) type { pub fn FromFormatter(comptime T: type) type {
@ -1260,7 +1252,7 @@ pub fn FromFormatter(comptime T: type) type {
.@"struct", .@"union" => { .@"struct", .@"union" => {
if (std.meta.hasMethod(F, "srfFormat")) if (std.meta.hasMethod(F, "srfFormat"))
return val.srfFormat(field_name, writer); return val.srfFormat(field_name, writer);
return error.WriteFailed; @compileError("struct/union formatting requires pub fn srfFormat(self: Self, comptime key, writer: *std.Io.Writer) std.Io.Writer.Error!void. No function found on type: " ++ F);
}, },
} }
} }
@ -1652,7 +1644,7 @@ test "serialize/deserialize" {
const compact_from = try std.fmt.bufPrint( const compact_from = try std.fmt.bufPrint(
&buf, &buf,
"{f}", "{f}",
.{fmtFrom(Data, all_data, .{})}, .{fmt(Data, all_data, .{})},
); );
const expect = const expect =
@ -1793,7 +1785,7 @@ test "unions" {
const compact_from = try std.fmt.bufPrint( const compact_from = try std.fmt.bufPrint(
&buf, &buf,
"{f}", "{f}",
.{fmtFrom(MixedData, data, .{})}, .{fmt(MixedData, data, .{})},
); );
const expect = const expect =
\\#!srfv1 \\#!srfv1
@ -1844,7 +1836,7 @@ test "enums" {
const compact_from = try std.fmt.bufPrint( const compact_from = try std.fmt.bufPrint(
&buf, &buf,
"{f}", "{f}",
.{fmtFrom(Data, data, .{})}, .{fmt(Data, data, .{})},
); );
const expect = const expect =
\\#!srfv1 \\#!srfv1
@ -2140,7 +2132,7 @@ test "parse tolerates commas and currency in numbers" {
try std.testing.expectEqual(null, try it.next()); try std.testing.expectEqual(null, try it.next());
} }
test fmtFrom { test fmt {
// Example: serialize typed Zig values directly to SRF format. // Example: serialize typed Zig values directly to SRF format.
const Data = struct { const Data = struct {
name: []const u8, name: []const u8,
@ -2154,7 +2146,7 @@ test fmtFrom {
const result = try std.fmt.bufPrint( const result = try std.fmt.bufPrint(
&buf, &buf,
"{f}", "{f}",
.{fmtFrom(Data, values, .{})}, .{fmt(Data, values, .{})},
); );
try std.testing.expectEqualStrings( try std.testing.expectEqualStrings(
\\#!srfv1 \\#!srfv1
@ -2163,7 +2155,7 @@ test fmtFrom {
\\ \\
, result); , result);
} }
test "fmtFrom commas" { test "fmt commas" {
// Example: serialize typed Zig values directly to SRF format. // Example: serialize typed Zig values directly to SRF format.
const Data = struct { const Data = struct {
name: []const u8 = "bob", name: []const u8 = "bob",
@ -2176,7 +2168,7 @@ test "fmtFrom commas" {
const result = try std.fmt.bufPrint( const result = try std.fmt.bufPrint(
&buf, &buf,
"{f}", "{f}",
.{fmtFrom(Data, values, .{})}, .{fmt(Data, values, .{})},
); );
try std.testing.expectEqualStrings( try std.testing.expectEqualStrings(
\\#!srfv1 \\#!srfv1
@ -2184,7 +2176,7 @@ test "fmtFrom commas" {
\\ \\
, result); , result);
} }
test "fmtFrom outputs defaults with option" { test "fmt outputs defaults with option" {
// Example: serialize typed Zig values directly to SRF format. // Example: serialize typed Zig values directly to SRF format.
const Data = struct { const Data = struct {
name: []const u8 = "bob", name: []const u8 = "bob",
@ -2197,7 +2189,7 @@ test "fmtFrom outputs defaults with option" {
const result = try std.fmt.bufPrint( const result = try std.fmt.bufPrint(
&buf, &buf,
"{f}", "{f}",
.{fmtFrom(Data, values, .{ .emit_default_values = true })}, .{fmt(Data, values, .{ .emit_default_values = true })},
); );
try std.testing.expectEqualStrings( try std.testing.expectEqualStrings(
\\#!srfv1 \\#!srfv1