using OneOf; namespace Bit.Core.Billing.Commands; public record BadRequest(string Response); public record Conflict(string Response); public record Unhandled(Exception? Exception = null, string Response = "Something went wrong with your request. Please contact support for assistance."); /// /// A union type representing the result of a billing command. /// /// Choices include: /// /// : Success /// : Invalid input /// : A known, but unresolvable issue /// : An unknown issue /// /// /// /// The successful result type of the operation. public class BillingCommandResult(OneOf input) : OneOfBase(input) { public static implicit operator BillingCommandResult(T output) => new(output); public static implicit operator BillingCommandResult(BadRequest badRequest) => new(badRequest); public static implicit operator BillingCommandResult(Conflict conflict) => new(conflict); public static implicit operator BillingCommandResult(Unhandled unhandled) => new(unhandled); public BillingCommandResult Map(Func f) => Match( value => new BillingCommandResult(f(value)), badRequest => new BillingCommandResult(badRequest), conflict => new BillingCommandResult(conflict), unhandled => new BillingCommandResult(unhandled)); public Task TapAsync(Func f) => Match( f, _ => Task.CompletedTask, _ => Task.CompletedTask, _ => Task.CompletedTask); } public static class BillingCommandResultExtensions { public static async Task> AndThenAsync( this Task> task, Func>> binder) { var result = await task; return await result.Match( binder, badRequest => Task.FromResult(new BillingCommandResult(badRequest)), conflict => Task.FromResult(new BillingCommandResult(conflict)), unhandled => Task.FromResult(new BillingCommandResult(unhandled))); } }