namespace Titano.Server.Api; /// Result of a server operation: a value, or an HTTP status with an API error code (and optional details). public readonly record struct Outcome(T? Value, int Status, string? Code, string? Details) { public bool Succeeded => Code is null; public static Outcome Ok(T value) => new(value, StatusCodes.Status200OK, null, null); public static Outcome Fail(int status, string code, string? details = null) => new(default, status, code, details); /// The API response: for a value, the error body otherwise. public IResult ToResult(HttpContext context, Func ok) => Succeeded ? ok(Value!) : ApiErrors.Result(context, Status, Code!, Details); public static implicit operator Outcome(T value) => Ok(value); }