termancy Guide Components Commands API

field

field - what settings mean, shared by cli and cfg.

A Spec(T) says, for each field of a struct (or variant of a tagged union), its help, whether it is secret or deprecated, and the checks its value must pass; and checks that span fields. Specs are keyed by type: a Set of them applies each one wherever its type occurs, so a Listener spec covers public, admin, and every element of a list of listeners alike. How a setting is spelled is not here: names, short flags, keys, and environment variables belong to cli.flags and cfg.settings, per place.

const field = @import("field");

const Listener = struct { host: []const u8 = "0.0.0.0", port: u16 = 8080 };

fn notReserved(port: u16, w: *std.Io.Writer) !void {
    if (port == 6000) return field.fail(w, "is reserved for X11", .{});
}

const listener: field.Spec(Listener) = .{ .fields = .{
    .host = .{ .help = "Address to bind" },
    .port = .{ .help = "Port to listen on", .checks = &.{ .min(1), .from(notReserved) } },
} };

pub const Specs = field.Set(.{listener});

A check is a plain function, fn (V, *std.Io.Writer) !void, that writes why a value is invalid and returns error.Invalid; the built-in ones (min, max, minLen, maxLen, oneOf, glob) also describe themselves (About) for help, documentation, schemas, and completion. Checks run on decoded values, and every failure is reported, not just the first.

The module imports only std.

Declarations

matchesWhether text matches a glob pattern: * any run of bytes, ? one byte.
isSetWhether S is a Set of specs.

The full API reference