result/src/Result/Extensions/Extensions.Task.cs
Louis Seubert 2a5c134d9f
All checks were successful
default / dotnet default workflow (8.0) (push) Successful in 39s
chore: add copyright header
2024-05-01 18:30:15 +02:00

92 lines
No EOL
4.4 KiB
C#

// Copyright (c) The Geekeey Authors
// SPDX-License-Identifier: EUPL-1.2
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)]
// ReSharper disable once InconsistentNaming
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>> MapAsync<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)]
// ReSharper disable once InconsistentNaming
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>> ThenAsync<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)]
// ReSharper disable once InconsistentNaming
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>> MapAsync<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)]
// ReSharper disable once InconsistentNaming
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>> ThenAsync<T, TNew>(this ValueTask<Result<T>> result,
Func<T, ValueTask<Result<TNew>>> func)
=> await (await result).ThenAsync(func);
#endregion
}