All checks were successful
default / dotnet default workflow (8.0) (push) Successful in 39s
90 lines
No EOL
3 KiB
C#
90 lines
No EOL
3 KiB
C#
// Copyright (c) The Geekeey Authors
|
|
// SPDX-License-Identifier: EUPL-1.2
|
|
|
|
namespace Geekeey.Extensions.Result;
|
|
|
|
/// <summary>
|
|
/// Extensions for or relating to <see cref="Result{T}"/>.
|
|
/// </summary>
|
|
public static partial class Extensions
|
|
{
|
|
/// <summary>
|
|
/// Turns a sequence of results into a single result containing the success values in the results only if all the
|
|
/// results have success values.
|
|
/// </summary>
|
|
/// <param name="results">The results to turn into a single sequence.</param>
|
|
/// <typeparam name="T">The type of the success values in the results.</typeparam>
|
|
/// <returns>A single result containing a sequence of all the success values from the original sequence of results,
|
|
/// or the first failure value encountered within the sequence.</returns>
|
|
/// <remarks>
|
|
/// This method completely enumerates the input sequence before returning and is not lazy. As a consequence of this,
|
|
/// the sequence within the returned result is an <see cref="IReadOnlyList{T}"/>.
|
|
/// </remarks>
|
|
public static Result<IReadOnlyList<T>> Join<T>(this IEnumerable<Result<T>> results)
|
|
{
|
|
_ = results.TryGetNonEnumeratedCount(out var count);
|
|
var list = new List<T>(count);
|
|
|
|
foreach (var result in results)
|
|
{
|
|
if (!result.TryGetValue(out var value, out var error))
|
|
{
|
|
return new Result<IReadOnlyList<T>>(error);
|
|
}
|
|
|
|
list.Add(value);
|
|
}
|
|
|
|
return list;
|
|
}
|
|
|
|
/// <inheritdoc cref="Join{T}(System.Collections.Generic.IEnumerable{Geekeey.Extensions.Result.Result{T}})"/>
|
|
/// <remarks>
|
|
/// For parallel execution of the async tasks, one should await the <c>Task.WhenAll()</c> of the provided list
|
|
/// before calling this function
|
|
/// </remarks>
|
|
/// <seealso cref="Join{T}(System.Collections.Generic.IEnumerable{Geekeey.Extensions.Result.Result{T}})"/>
|
|
// ReSharper disable once InconsistentNaming
|
|
public static async Task<Result<IReadOnlyList<T>>> Join<T>(this IEnumerable<Task<Result<T>>> results)
|
|
{
|
|
_ = results.TryGetNonEnumeratedCount(out var count);
|
|
var list = new List<T>(count);
|
|
|
|
foreach (var result in results)
|
|
{
|
|
if (!(await result).TryGetValue(out var value, out var error))
|
|
{
|
|
return new Result<IReadOnlyList<T>>(error);
|
|
}
|
|
|
|
list.Add(value);
|
|
}
|
|
|
|
return list;
|
|
}
|
|
|
|
/// <inheritdoc cref="Join{T}(System.Collections.Generic.IEnumerable{Geekeey.Extensions.Result.Result{T}})"/>
|
|
/// <remarks>
|
|
/// For parallel execution of the async tasks, one should await the <c>Task.WhenAll()</c> of the provided list
|
|
/// before calling this function
|
|
/// </remarks>
|
|
/// <seealso cref="Join{T}(System.Collections.Generic.IEnumerable{Geekeey.Extensions.Result.Result{T}})"/>
|
|
// ReSharper disable once InconsistentNaming
|
|
public static async ValueTask<Result<IReadOnlyList<T>>> Join<T>(this IEnumerable<ValueTask<Result<T>>> results)
|
|
{
|
|
_ = results.TryGetNonEnumeratedCount(out var count);
|
|
var list = new List<T>(count);
|
|
|
|
foreach (var result in results)
|
|
{
|
|
if (!(await result).TryGetValue(out var value, out var error))
|
|
{
|
|
return new Result<IReadOnlyList<T>>(error);
|
|
}
|
|
|
|
list.Add(value);
|
|
}
|
|
|
|
return list;
|
|
}
|
|
} |