feat: initial project commit
All checks were successful
default / default (8.0) (push) Successful in 1m7s

This commit is contained in:
Louis Seubert 2024-04-14 17:42:13 +02:00
commit 30ef7bd477
Signed by: louis9902
GPG key ID: 4B9DB28F826553BD
40 changed files with 3752 additions and 0 deletions

View file

@ -0,0 +1,85 @@
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
namespace Geekeey.Extensions.Result;
/// <summary>
/// Extensions for or relating to <see cref="Result{T}"/>.
/// </summary>
[ExcludeFromCodeCoverage]
public static partial class Extensions
{
#region Task<Result<T>>
/// <summary>
/// Maps the success value of the result object of the completed task using a mapping function, or does nothing if
/// the result object of the completed task is a failure.
/// </summary>
/// <param name="result">A task object retunring a result object when completing.</param>
/// <param name="func">The function used to map the success value.</param>
/// <typeparam name="T">The type of the object inside the result returned by the task.</typeparam>
/// <typeparam name="TNew">The type of the new value.</typeparam>
/// <returns>A new result containing either the mapped success value or the failure value of the original
/// result.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static async ValueTask<Result<TNew>> Map<T, TNew>(this Task<Result<T>> result,
Func<T, TNew> func)
=> (await result).Map(func);
/// <inheritdoc cref="Map{T,TNew}(System.Threading.Tasks.Task{Geekeey.Extensions.Result.Result{T}},System.Func{T,TNew})"/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static async Task<Result<TNew>> Map<T, TNew>(this Task<Result<T>> result,
Func<T, ValueTask<TNew>> func)
=> await (await result).MapAsync(func);
/// <summary>
/// Maps the success value of the result object of the completed task to a new result using a mapping function, or
/// does nothing if the result object of the completed task is a failure.
/// </summary>
/// <param name="result">A task object retunring a result object when completing.</param>
/// <param name="func">The function used to map the success value.</param>
/// <typeparam name="T">The type of the object inside the result returned by the task.</typeparam>
/// <typeparam name="TNew">The type of the new value.</typeparam>
/// <returns>A new result containing either the mapped success value or the failure value of the original
/// result.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static async Task<Result<TNew>> Then<T, TNew>(this Task<Result<T>> result,
Func<T, Result<TNew>> func)
=> (await result).Then(func);
/// <inheritdoc cref="Then{T,TNew}(System.Threading.Tasks.Task{Geekeey.Extensions.Result.Result{T}},System.Func{T,Geekeey.Extensions.Result.Result{TNew}})"/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static async Task<Result<TNew>> Then<T, TNew>(this Task<Result<T>> result,
Func<T, ValueTask<Result<TNew>>> func)
=> await (await result).ThenAsync(func);
#endregion
#region ValueTask<Result<T>>
/// <inheritdoc cref="Map{T,TNew}(System.Threading.Tasks.Task{Geekeey.Extensions.Result.Result{T}},System.Func{T,TNew})"/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static async Task<Result<TNew>> Map<T, TNew>(this ValueTask<Result<T>> result,
Func<T, TNew> func)
=> (await result).Map(func);
/// <inheritdoc cref="Map{T,TNew}(System.Threading.Tasks.Task{Geekeey.Extensions.Result.Result{T}},System.Func{T,TNew})"/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static async ValueTask<Result<TNew>> Map<T, TNew>(this ValueTask<Result<T>> result,
Func<T, ValueTask<TNew>> func)
=> await (await result).MapAsync(func);
/// <inheritdoc cref="Then{T,TNew}(System.Threading.Tasks.Task{Geekeey.Extensions.Result.Result{T}},System.Func{T,Geekeey.Extensions.Result.Result{TNew}})"/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static async ValueTask<Result<TNew>> Then<T, TNew>(this ValueTask<Result<T>> result,
Func<T, Result<TNew>> func)
=> (await result).Then(func);
/// <inheritdoc cref="Then{T,TNew}(System.Threading.Tasks.Task{Geekeey.Extensions.Result.Result{T}},System.Func{T,Geekeey.Extensions.Result.Result{TNew}})"/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static async ValueTask<Result<TNew>> Then<T, TNew>(this ValueTask<Result<T>> result,
Func<T, ValueTask<Result<TNew>>> func)
=> await (await result).ThenAsync(func);
#endregion
}