| | | 1 | | namespace Allyaria.Abstractions.Results; |
| | | 2 | | |
| | | 3 | | /// <summary> |
| | | 4 | | /// Represents the outcome of an operation that may return a value of type <typeparamref name="T" />. Encapsulates succe |
| | | 5 | | /// and failure states, error details, and contextual information. |
| | | 6 | | /// </summary> |
| | | 7 | | /// <typeparam name="T">The type of the value returned on success.</typeparam> |
| | | 8 | | public sealed class AryResult<T> |
| | | 9 | | { |
| | | 10 | | /// <summary>Initializes a new instance of the <see cref="AryResult{T}" /> class.</summary> |
| | | 11 | | /// <param name="isSuccess">Indicates whether the operation succeeded.</param> |
| | | 12 | | /// <param name="value">The result value, if successful.</param> |
| | | 13 | | /// <param name="error">The associated <see cref="Exception" />, if any.</param> |
| | 16 | 14 | | private AryResult(bool isSuccess, T? value = default, Exception? error = null) |
| | | 15 | | { |
| | 16 | 16 | | IsSuccess = isSuccess; |
| | 16 | 17 | | Error = error; |
| | 16 | 18 | | Value = value; |
| | 16 | 19 | | } |
| | | 20 | | |
| | | 21 | | /// <summary>Gets the <see cref="Exception" /> associated with this result, if any.</summary> |
| | | 22 | | /// <value>The exception describing the failure cause, or <see langword="null" /> if the operation succeeded.</value |
| | 14 | 23 | | public Exception? Error { get; } |
| | | 24 | | |
| | | 25 | | /// <summary>Gets a value indicating whether the operation failed.</summary> |
| | | 26 | | /// <value><see langword="true" /> if the operation failed; otherwise, <see langword="false" />.</value> |
| | 12 | 27 | | public bool IsFailure => !IsSuccess; |
| | | 28 | | |
| | | 29 | | /// <summary>Gets a value indicating whether the operation succeeded.</summary> |
| | | 30 | | /// <value><see langword="true" /> if the operation succeeded; otherwise, <see langword="false" />.</value> |
| | 24 | 31 | | public bool IsSuccess { get; } |
| | | 32 | | |
| | | 33 | | /// <summary>Gets the result value of the operation, if successful.</summary> |
| | | 34 | | /// <value> |
| | | 35 | | /// The value produced by the operation. This property is <see langword="null" /> if the operation failed. |
| | | 36 | | /// </value> |
| | 6 | 37 | | public T? Value { get; } |
| | | 38 | | |
| | | 39 | | /// <summary>Creates a failed <see cref="AryResult{T}" /> with the specified error information.</summary> |
| | | 40 | | /// <param name="error">The exception representing the failure cause.</param> |
| | | 41 | | /// <returns>A failed <see cref="AryResult{T}" /> containing the provided error details.</returns> |
| | | 42 | | public static AryResult<T> Failure(Exception? error) |
| | 10 | 43 | | => new(isSuccess: false, error: error ?? new AryException(message: "Unknown error")); |
| | | 44 | | |
| | | 45 | | /// <summary>Creates a successful <see cref="AryResult{T}" /> with the specified value.</summary> |
| | | 46 | | /// <param name="value">The result value to encapsulate.</param> |
| | | 47 | | /// <returns>A successful <see cref="AryResult{T}" /> containing the specified value.</returns> |
| | 6 | 48 | | public static AryResult<T> Success(T value) => new(isSuccess: true, value: value); |
| | | 49 | | |
| | | 50 | | /// <summary>Converts this <see cref="AryResult{T}" /> to a failed <see cref="AryResult" />.</summary> |
| | | 51 | | /// <returns>A failed <see cref="AryResult" /> with the same error details as this instance.</returns> |
| | | 52 | | /// <exception cref="AryInvalidOperationException">Thrown when attempting to convert a successful result to a failur |
| | | 53 | | public AryResult ToFailure() |
| | 4 | 54 | | => IsSuccess |
| | 4 | 55 | | ? throw new AryInvalidOperationException( |
| | 4 | 56 | | message: "Cannot convert a successful AryResult<T> to AryResult failure." |
| | 4 | 57 | | ) |
| | 4 | 58 | | : AryResult.Failure(error: Error); |
| | | 59 | | } |