// SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie // SPDX-License-Identifier: MIT //! Performing an operation on behalf of a porcelain command, which needs its //! answer to go on: what came back, or why nothing did -- reported, and as //! the status to exit with. const std = @import("std"); const zfj = @import("zfj"); const Context = @import("Context.zig"); const Exit = Context.Exit; pub fn Got(comptime Ok: type) type { return union(enum) { ok: Ok, failed: Exit, }; } fn OkOf(comptime Attempt: type) type { return @FieldType(@typeInfo(Attempt).error_union.payload, "ok"); } /// The outcome of an operation's `Result` form. A response that does not /// parse is a failure here whatever `--strict` says, since the command /// cannot go on without the model. pub fn result(ctx: *Context, comptime name: []const u8, attempt: anytype) Got(OkOf(@TypeOf(attempt))) { var outcome = attempt catch |err| return .{ .failed = ctx.transport(name, err) }; switch (outcome) { .ok => |ok| return .{ .ok = ok }, .api_error => |*raw| { defer raw.deinit(); return .{ .failed = ctx.apiError(name, raw.status, raw.body) }; }, .parse_error => |*e| { defer e.raw.deinit(); const strict = ctx.global.strict; ctx.global.strict = true; defer ctx.global.strict = strict; return .{ .failed = ctx.parseFailure(name, "its documented model", e.error_name) }; }, } } /// The outcome of an operation's `Raw` form, for one whose successful /// response there is nothing to read from: null when it succeeded. pub fn done(ctx: *Context, comptime name: []const u8, attempt: anytype) ?Exit { var raw: zfj.RawResponse = attempt catch |err| return ctx.transport(name, err); defer raw.deinit(); if (raw.status.class() != .success) return ctx.apiError(name, raw.status, raw.body); return null; }