| | | 1 | | namespace Allyaria.Abstractions.Results; |
| | | 2 | | |
| | | 3 | | /// <summary> |
| | | 4 | | /// Represents the outcome of an operation that can either succeed or fail, encapsulating success state, error details, |
| | | 5 | | /// associated metadata. |
| | | 6 | | /// </summary> |
| | | 7 | | public sealed class AryResult |
| | | 8 | | { |
| | | 9 | | /// <summary>Initializes a new instance of the <see cref="AryResult" /> class.</summary> |
| | | 10 | | /// <param name="isSuccess">Indicates whether the operation succeeded.</param> |
| | | 11 | | /// <param name="error">The associated <see cref="Exception" />, if any.</param> |
| | 16 | 12 | | private AryResult(bool isSuccess, Exception? error = null) |
| | | 13 | | { |
| | 16 | 14 | | IsSuccess = isSuccess; |
| | 16 | 15 | | Error = error; |
| | 16 | 16 | | } |
| | | 17 | | |
| | | 18 | | /// <summary>Gets the <see cref="Exception" /> associated with the result, if any.</summary> |
| | | 19 | | /// <value> |
| | | 20 | | /// The exception describing the failure cause, or <see langword="null" /> if the result represents success. |
| | | 21 | | /// </value> |
| | 14 | 22 | | public Exception? Error { get; } |
| | | 23 | | |
| | | 24 | | /// <summary>Gets a value indicating whether the operation failed.</summary> |
| | | 25 | | /// <value><see langword="true" /> if the operation failed; otherwise, <see langword="false" />.</value> |
| | 12 | 26 | | public bool IsFailure => !IsSuccess; |
| | | 27 | | |
| | | 28 | | /// <summary>Gets a value indicating whether the operation succeeded.</summary> |
| | | 29 | | /// <value><see langword="true" /> if the operation succeeded; otherwise, <see langword="false" />.</value> |
| | 24 | 30 | | public bool IsSuccess { get; } |
| | | 31 | | |
| | | 32 | | /// <summary>Creates a failure <see cref="AryResult" /> with the specified error information.</summary> |
| | | 33 | | /// <param name="error">The <see cref="Exception" /> representing the failure cause.</param> |
| | | 34 | | /// <returns>An <see cref="AryResult" /> representing a failed operation.</returns> |
| | | 35 | | public static AryResult Failure(Exception? error) |
| | 10 | 36 | | => new(isSuccess: false, error: error ?? new AryException(message: "Unknown error")); |
| | | 37 | | |
| | | 38 | | /// <summary>Creates a successful <see cref="AryResult" />.</summary> |
| | | 39 | | /// <returns>An <see cref="AryResult" /> representing a successful operation.</returns> |
| | 6 | 40 | | public static AryResult Success() => new(isSuccess: true); |
| | | 41 | | |
| | | 42 | | /// <summary>Converts this <see cref="AryResult" /> to a failed <see cref="AryResult{T}" /> instance.</summary> |
| | | 43 | | /// <typeparam name="T">The result type of the target <see cref="AryResult{T}" />.</typeparam> |
| | | 44 | | /// <returns>A failed <see cref="AryResult{T}" /> with the same error details as this result.</returns> |
| | | 45 | | /// <exception cref="AryInvalidOperationException">Thrown when attempting to convert a successful result into a fail |
| | | 46 | | public AryResult<T> ToFailure<T>() |
| | 4 | 47 | | => IsSuccess |
| | 4 | 48 | | ? throw new AryInvalidOperationException( |
| | 4 | 49 | | message: "Cannot convert a successful AryResult to AryResult<T> failure." |
| | 4 | 50 | | ) |
| | 4 | 51 | | : AryResult<T>.Failure(error: Error); |
| | | 52 | | } |