feat: create request projects for basic CQRS
This commit is contained in:
commit
d614788e06
190 changed files with 12236 additions and 0 deletions
31
src/request.result/Geekeey.Request.Result.csproj
Normal file
31
src/request.result/Geekeey.Request.Result.csproj
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Library</OutputType>
|
||||
<TargetFramework>net10.0</TargetFramework>
|
||||
<IsPackable>true</IsPackable>
|
||||
</PropertyGroup>
|
||||
|
||||
<PropertyGroup>
|
||||
<GenerateDocumentationFile>true</GenerateDocumentationFile>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup Condition="'$(Configuration)' == 'Debug'">
|
||||
<InternalsVisibleTo Include="Geekeey.Request.Result.Tests" />
|
||||
</ItemGroup>
|
||||
|
||||
<PropertyGroup>
|
||||
<PackageReadmeFile>package-readme.md</PackageReadmeFile>
|
||||
<PackageDescription>Simple result type implementation for C# with utilities for composing success and failure flows.</PackageDescription>
|
||||
<PackageIcon>package-icon.png</PackageIcon>
|
||||
<PackageProjectUrl>https://code.geekeey.de/geekeey/request/src/branch/main/src/request.result</PackageProjectUrl>
|
||||
<PackageLicenseExpression>EUPL-1.2</PackageLicenseExpression>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<None Include=".\package-icon.png" Pack="true" PackagePath="\" Visible="false" />
|
||||
<None Include=".\package-readme.md" Pack="true" PackagePath="\" Visible="false" />
|
||||
<None Include="..\..\LICENSE.md" Pack="true" PackagePath="\" Visible="false" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
20
src/request.result/IResultFactory.cs
Normal file
20
src/request.result/IResultFactory.cs
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
// Copyright (c) The Geekeey Authors
|
||||
// SPDX-License-Identifier: EUPL-1.2
|
||||
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
|
||||
namespace Geekeey.Request.Result;
|
||||
|
||||
/// <summary>
|
||||
/// An interface for a result.
|
||||
/// </summary>
|
||||
[SuppressMessage("Naming", "CA1716:Identifiers should not match keywords")]
|
||||
public interface IResultFactory<out TSelf> where TSelf : IResultFactory<TSelf>
|
||||
{
|
||||
/// <summary>
|
||||
/// Creates a new result with a failure value.
|
||||
/// </summary>
|
||||
/// <param name="error">The error of the result.</param>
|
||||
/// <returns>A new result with a failure value.</returns>
|
||||
static abstract TSelf Failure(Error error);
|
||||
}
|
||||
183
src/request.result/Prelude.cs
Normal file
183
src/request.result/Prelude.cs
Normal file
|
|
@ -0,0 +1,183 @@
|
|||
// Copyright (c) The Geekeey Authors
|
||||
// SPDX-License-Identifier: EUPL-1.2
|
||||
|
||||
using System.Diagnostics.Contracts;
|
||||
|
||||
namespace Geekeey.Request.Result;
|
||||
|
||||
/// <summary>
|
||||
/// A class containing various utility methods, a 'prelude' to the rest of the library.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// This class is meant to be imported statically, e.g. <c>using static Geekeey.Extensions.Result.Prelude;</c>.
|
||||
/// Recommended to be imported globally via a global using statement.
|
||||
/// </remarks>
|
||||
public static class Prelude
|
||||
{
|
||||
/// <summary>
|
||||
/// Creates a result containing a success.
|
||||
/// </summary>
|
||||
[Pure]
|
||||
public static Result Success()
|
||||
{
|
||||
return new Result();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a result containing a failure.
|
||||
/// </summary>
|
||||
/// <param name="error">The failure value to create the result from.</param>
|
||||
[Pure]
|
||||
public static Result Failure(Error error)
|
||||
{
|
||||
return new Result(error);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a result containing a success value.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of the success value.</typeparam>
|
||||
/// <param name="value">The success value to create the result from.</param>
|
||||
[Pure]
|
||||
public static Result<T> Success<T>(T value)
|
||||
{
|
||||
return new Result<T>(value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a result containing a failure value.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of success value in the result.</typeparam>
|
||||
/// <param name="error">The failure value to create the result from.</param>
|
||||
[Pure]
|
||||
public static Result<T> Failure<T>(Error error)
|
||||
{
|
||||
return new Result<T>(error);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tries to execute an action and return the result. If the action throws an exception, the exception will be
|
||||
/// returned wrapped in an <see cref="ExceptionError"/>.
|
||||
/// </summary>
|
||||
/// <param name="function">The action to try to execute.</param>
|
||||
/// <returns>A result containing success or an <see cref="ExceptionError"/> containing the exception thrown by the
|
||||
/// action.</returns>
|
||||
[Pure]
|
||||
public static Result Try(Action function)
|
||||
{
|
||||
try
|
||||
{
|
||||
function();
|
||||
return new Result();
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
return new Result(new ExceptionError(exception));
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tries to execute an asynchronous function and return the result. If the function throws an exception, the
|
||||
/// exception will be returned wrapped in an <see cref="ExceptionError"/>.
|
||||
/// </summary>
|
||||
/// <param name="function">The function to try to execute.</param>
|
||||
/// <returns>A result containing success or an <see cref="ExceptionError"/> containing the exception thrown by the
|
||||
/// function.</returns>
|
||||
[Pure]
|
||||
public static async ValueTask<Result> TryAsync(Func<ValueTask> function)
|
||||
{
|
||||
try
|
||||
{
|
||||
await function();
|
||||
return new Result();
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
return new Result(new ExceptionError(exception));
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tries to execute an asynchronous function and return the result. If the function throws an exception, the
|
||||
/// exception will be returned wrapped in an <see cref="ExceptionError"/>.
|
||||
/// </summary>
|
||||
/// <param name="function">The function to try to execute.</param>
|
||||
/// <returns>A result containing success or an <see cref="ExceptionError"/> containing the exception thrown by the
|
||||
/// function.</returns>
|
||||
[Pure]
|
||||
public static async Task<Result> TryAsync(Func<Task> function)
|
||||
{
|
||||
try
|
||||
{
|
||||
await function();
|
||||
return new Result();
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
return new Result(new ExceptionError(exception));
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tries to execute a function and return the result. If the function throws an exception, the exception will be
|
||||
/// returned wrapped in an <see cref="ExceptionError"/>.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type the function returns.</typeparam>
|
||||
/// <param name="function">The function to try to execute.</param>
|
||||
/// <returns>A result containing the return value of the function or an <see cref="ExceptionError"/> containing the
|
||||
/// exception thrown by the function.</returns>
|
||||
[Pure]
|
||||
public static Result<T> Try<T>(Func<T> function)
|
||||
{
|
||||
try
|
||||
{
|
||||
return new Result<T>(function());
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
return new Result<T>(new ExceptionError(exception));
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tries to execute an asynchronous function and return the result. If the function throws an exception, the
|
||||
/// exception will be returned wrapped in an <see cref="ExceptionError"/>.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type the function returns.</typeparam>
|
||||
/// <param name="function">The function to try to execute.</param>
|
||||
/// <returns>A result containing the return value of the function or an <see cref="ExceptionError"/> containing the
|
||||
/// exception thrown by the function.</returns>
|
||||
[Pure]
|
||||
public static async ValueTask<Result<T>> TryAsync<T>(Func<ValueTask<T>> function)
|
||||
{
|
||||
try
|
||||
{
|
||||
return new Result<T>(await function());
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
return new Result<T>(new ExceptionError(exception));
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tries to execute an asynchronous function and return the result. If the function throws an exception, the
|
||||
/// exception will be returned wrapped in an <see cref="ExceptionError"/>.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type the function returns.</typeparam>
|
||||
/// <param name="function">The function to try to execute.</param>
|
||||
/// <returns>A result containing the return value of the function or an <see cref="ExceptionError"/> containing the
|
||||
/// exception thrown by the function.</returns>
|
||||
[Pure]
|
||||
public static async Task<Result<T>> TryAsync<T>(Func<Task<T>> function)
|
||||
{
|
||||
try
|
||||
{
|
||||
return new Result<T>(await function());
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
return new Result<T>(new ExceptionError(exception));
|
||||
}
|
||||
}
|
||||
}
|
||||
64
src/request.result/Result.Conversion.cs
Normal file
64
src/request.result/Result.Conversion.cs
Normal file
|
|
@ -0,0 +1,64 @@
|
|||
// Copyright (c) The Geekeey Authors
|
||||
// SPDX-License-Identifier: EUPL-1.2
|
||||
|
||||
using System.Diagnostics.Contracts;
|
||||
|
||||
namespace Geekeey.Request.Result;
|
||||
|
||||
public partial class Result
|
||||
{
|
||||
/// <summary>
|
||||
/// Implicitly constructs a result from a failure value.
|
||||
/// </summary>
|
||||
/// <param name="error">The error to construct the result from.</param>
|
||||
[Pure]
|
||||
public static implicit operator Result(Error error)
|
||||
{
|
||||
return new Result(error);
|
||||
}
|
||||
}
|
||||
|
||||
public partial class Result<T>
|
||||
{
|
||||
/// <summary>
|
||||
/// Implicitly constructs a result from a success value.
|
||||
/// </summary>
|
||||
/// <param name="value">The value to construct the result from.</param>
|
||||
[Pure]
|
||||
public static implicit operator Result<T>(T value)
|
||||
{
|
||||
return new Result<T>(value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Implicitly constructs a result from a failure value.
|
||||
/// </summary>
|
||||
/// <param name="error">The error to construct the result from.</param>
|
||||
[Pure]
|
||||
public static implicit operator Result<T>(Error error)
|
||||
{
|
||||
return new Result<T>(error);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Unwraps the success value of the result. Throws an <see cref="UnwrapException"/> if the result is a failure.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// This call is <b>unsafe</b> in the sense that it might intentionally throw an exception. Please only use this
|
||||
/// call if the caller knows that this operation is safe, or that an exception is acceptable to be thrown.
|
||||
/// </remarks>
|
||||
/// <returns>The success value of the result.</returns>
|
||||
/// <exception cref="UnwrapException">The result is not a success.</exception>
|
||||
[Pure]
|
||||
public T Unwrap()
|
||||
{
|
||||
return IsSuccess ? Value : throw new UnwrapException();
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="Unwrap"/>
|
||||
[Pure]
|
||||
public static explicit operator T(Result<T> result)
|
||||
{
|
||||
return result.Unwrap();
|
||||
}
|
||||
}
|
||||
239
src/request.result/Result.Equality.cs
Normal file
239
src/request.result/Result.Equality.cs
Normal file
|
|
@ -0,0 +1,239 @@
|
|||
// Copyright (c) The Geekeey Authors
|
||||
// SPDX-License-Identifier: EUPL-1.2
|
||||
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Diagnostics.Contracts;
|
||||
using System.Numerics;
|
||||
|
||||
namespace Geekeey.Request.Result;
|
||||
|
||||
public partial class Result : IEquatable<Result>
|
||||
{
|
||||
/// <inheritdoc/>
|
||||
[Pure]
|
||||
public bool Equals(Result? other)
|
||||
{
|
||||
return Equals(this, other);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
[Pure]
|
||||
public override bool Equals(object? obj)
|
||||
{
|
||||
return obj is Result r && Equals(r);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
[Pure]
|
||||
public override int GetHashCode()
|
||||
{
|
||||
return GetHashCode(this);
|
||||
}
|
||||
|
||||
internal static bool Equals(Result? a, Result? b)
|
||||
{
|
||||
if (a is null || b is null)
|
||||
{
|
||||
return a is null && b is null;
|
||||
}
|
||||
|
||||
return a.IsSuccess == b.IsSuccess;
|
||||
}
|
||||
|
||||
internal static int GetHashCode(Result? result)
|
||||
{
|
||||
return result?.IsSuccess.GetHashCode() ?? 0;
|
||||
}
|
||||
}
|
||||
|
||||
public partial class Result : IEqualityOperators<Result, Result, bool>
|
||||
{
|
||||
/// <summary>
|
||||
/// Checks whether two results are equal. Results are equal if they are both success or both failure.
|
||||
/// </summary>
|
||||
/// <param name="a">The first result to compare.</param>
|
||||
/// <param name="b">The second result to compare.</param>
|
||||
[Pure]
|
||||
[ExcludeFromCodeCoverage]
|
||||
public static bool operator ==(Result? a, Result? b)
|
||||
{
|
||||
return Equals(a, b);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Checks whether two results are not equal. Results are equal if they are both success or both failure.
|
||||
/// </summary>
|
||||
/// <param name="a">The first result to compare.</param>
|
||||
/// <param name="b">The second result to compare.</param>
|
||||
[Pure]
|
||||
[ExcludeFromCodeCoverage]
|
||||
public static bool operator !=(Result? a, Result? b)
|
||||
{
|
||||
return !Equals(a, b);
|
||||
}
|
||||
}
|
||||
|
||||
public partial class Result<T> : IEquatable<Result<T>>, IEquatable<T>
|
||||
{
|
||||
/// <summary>
|
||||
/// Checks whether the result is equal to another result. Results are equal if both results are success values and
|
||||
/// the success values are equal, or if both results are failures.
|
||||
/// </summary>
|
||||
/// <param name="other">The result to check for equality with the current result.</param>
|
||||
[Pure]
|
||||
public bool Equals(Result<T>? other)
|
||||
{
|
||||
return Equals(this, other, EqualityComparer<T>.Default);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Checks whether the result is equal to another result. Results are equal if both results are success values and
|
||||
/// the success values are equal, or if both results are failures.
|
||||
/// </summary>
|
||||
/// <param name="other">The result to check for equality with the current result.</param>
|
||||
/// <param name="comparer">The equality comparer to use for comparing values.</param>
|
||||
[Pure]
|
||||
public bool Equals(Result<T> other, IEqualityComparer<T> comparer)
|
||||
{
|
||||
return Equals(this, other, comparer);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Checks whether the result is a success value and the success value is equal to another value.
|
||||
/// </summary>
|
||||
/// <param name="other">The value to check for equality with the success value of the result.</param>
|
||||
[Pure]
|
||||
public bool Equals(T? other)
|
||||
{
|
||||
return Equals(this, other, EqualityComparer<T>.Default);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Checks whether the result is a success value and the success value is equal to another value using a specified
|
||||
/// equality comparer.
|
||||
/// </summary>
|
||||
/// <param name="other">The value to check for equality with the success value of the result.</param>
|
||||
/// <param name="comparer">The equality comparer to use for comparing values.</param>
|
||||
[Pure]
|
||||
public bool Equals(T? other, IEqualityComparer<T> comparer)
|
||||
{
|
||||
return Equals(this, other, comparer);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
[Pure]
|
||||
public override bool Equals(object? obj)
|
||||
{
|
||||
return (obj is T x && Equals(x)) || (obj is Result<T> r && Equals(r));
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
[Pure]
|
||||
public override int GetHashCode()
|
||||
{
|
||||
return GetHashCode(this, EqualityComparer<T>.Default);
|
||||
}
|
||||
|
||||
internal static bool Equals(Result<T>? a, Result<T>? b, IEqualityComparer<T> comparer)
|
||||
{
|
||||
if (a is null || b is null)
|
||||
{
|
||||
return a is null && b is null;
|
||||
}
|
||||
|
||||
if (!a.IsSuccess || !b.IsSuccess)
|
||||
{
|
||||
return !a.IsSuccess && !b.IsSuccess;
|
||||
}
|
||||
|
||||
if (a.Value is null || b.Value is null)
|
||||
{
|
||||
return a.Value is null && b.Value is null;
|
||||
}
|
||||
|
||||
return comparer.Equals(a.Value, b.Value);
|
||||
}
|
||||
|
||||
internal static bool Equals(Result<T>? a, T? b, IEqualityComparer<T> comparer)
|
||||
{
|
||||
if (a is null)
|
||||
{
|
||||
return b is null;
|
||||
}
|
||||
|
||||
if (!a.IsSuccess)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (a.Value is null || b is null)
|
||||
{
|
||||
return a.Value is null && b is null;
|
||||
}
|
||||
|
||||
return comparer.Equals(a.Value, b);
|
||||
}
|
||||
|
||||
internal static int GetHashCode(Result<T> result, IEqualityComparer<T> comparer)
|
||||
{
|
||||
if (result is { IsSuccess: true, Value: not null })
|
||||
{
|
||||
return comparer.GetHashCode(result.Value);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
public partial class Result<T> : IEqualityOperators<Result<T>, Result<T>, bool>, IEqualityOperators<Result<T>, T, bool>
|
||||
{
|
||||
/// <summary>
|
||||
/// Checks whether two results are equal. Results are equal if both results are success values and the success
|
||||
/// values are equal, or if both results are failures.
|
||||
/// </summary>
|
||||
/// <param name="a">The first result to compare.</param>
|
||||
/// <param name="b">The second result to compare.</param>
|
||||
[Pure]
|
||||
[ExcludeFromCodeCoverage]
|
||||
public static bool operator ==(Result<T>? a, Result<T>? b)
|
||||
{
|
||||
return Equals(a, b, EqualityComparer<T>.Default);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Checks whether two results are not equal. Results are equal if both results are success values and the success
|
||||
/// values are equal, or if both results are failures.
|
||||
/// </summary>
|
||||
/// <param name="a">The first result to compare.</param>
|
||||
/// <param name="b">The second result to compare.</param>
|
||||
[Pure]
|
||||
[ExcludeFromCodeCoverage]
|
||||
public static bool operator !=(Result<T>? a, Result<T>? b)
|
||||
{
|
||||
return !Equals(a, b, EqualityComparer<T>.Default);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Checks whether a result is a success value and the success value is equal to another value.
|
||||
/// </summary>
|
||||
/// <param name="a">The result to compare.</param>
|
||||
/// <param name="b">The value to check for equality with the success value in the result.</param>
|
||||
[Pure]
|
||||
[ExcludeFromCodeCoverage]
|
||||
public static bool operator ==(Result<T>? a, T? b)
|
||||
{
|
||||
return Equals(a, b, EqualityComparer<T>.Default);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Checks whether a result either does not have a value, or the value is not equal to another value.
|
||||
/// </summary>
|
||||
/// <param name="a">The result to compare.</param>
|
||||
/// <param name="b">The value to check for inequality with the success value in the result.</param>
|
||||
[Pure]
|
||||
[ExcludeFromCodeCoverage]
|
||||
public static bool operator !=(Result<T>? a, T? b)
|
||||
{
|
||||
return !Equals(a, b, EqualityComparer<T>.Default);
|
||||
}
|
||||
}
|
||||
199
src/request.result/Result.Matching.cs
Normal file
199
src/request.result/Result.Matching.cs
Normal file
|
|
@ -0,0 +1,199 @@
|
|||
// Copyright (c) The Geekeey Authors
|
||||
// SPDX-License-Identifier: EUPL-1.2
|
||||
|
||||
using System.Diagnostics.Contracts;
|
||||
|
||||
namespace Geekeey.Request.Result;
|
||||
|
||||
public partial class Result
|
||||
{
|
||||
/// <summary>
|
||||
/// Matches over the success or failure of the result and returns another value. Can be conceptualized as an
|
||||
/// exhaustive <c>switch</c> expression matching all possible states of the type.
|
||||
/// </summary>
|
||||
/// <typeparam name="TResult">The type to return from the match.</typeparam>
|
||||
/// <param name="success">The function to invoke if the result is a success.</param>
|
||||
/// <param name="failure">The function to apply to the failure value of the result if the result is a failure.</param>
|
||||
/// <returns>The result of applying either <paramref name="success"/> or <paramref name="failure"/> on the result.</returns>
|
||||
[Pure]
|
||||
public TResult Match<TResult>(Func<TResult> success, Func<Error, TResult> failure)
|
||||
{
|
||||
return IsSuccess ? success() : failure(Error);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Matches over the success or failure of the result and invokes an effectful action. Can be conceptualized as an
|
||||
/// exhaustive <c>switch</c> statement matching all possible states of the type.
|
||||
/// </summary>
|
||||
/// <param name="success">The function to call if the result is a success.</param>
|
||||
/// <param name="failure">The function to call with the failure value of the result if the result is a failure.</param>
|
||||
public void Switch(Action success, Action<Error> failure)
|
||||
{
|
||||
if (IsSuccess)
|
||||
{
|
||||
success();
|
||||
}
|
||||
else
|
||||
{
|
||||
failure(Error);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public partial class Result
|
||||
{
|
||||
/// <summary>
|
||||
/// Asynchronously matches over the success or failure of the result and returns another value. Can be
|
||||
/// conceptualized as an exhaustive <c>switch</c> expression matching all possible states of the type.
|
||||
/// </summary>
|
||||
/// <typeparam name="TResult">The type to return from the match.</typeparam>
|
||||
/// <param name="success">The function to invoke if the result is a success.</param>
|
||||
/// <param name="failure">The function to apply to the failure value of the result if the result is a failure.</param>
|
||||
/// <returns>A task completing with the result of applying either <paramref name="success"/> or
|
||||
/// <paramref name="failure"/> on the failure value of the result.</returns>
|
||||
[Pure]
|
||||
public async Task<TResult> MatchAsync<TResult>(Func<Task<TResult>> success, Func<Error, Task<TResult>> failure)
|
||||
{
|
||||
return IsSuccess ? await success() : await failure(Error);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Asynchronously matches over the success or failure of the result and invokes an effectful action onto the
|
||||
/// failure value. Can be conceptualized as an exhaustive <c>switch</c> statement matching all possible states of
|
||||
/// the type.
|
||||
/// </summary>
|
||||
/// <param name="success">The function to call if the result is a success.</param>
|
||||
/// <param name="failure">The function to call with the failure value of the result if the result is a failure.</param>
|
||||
public Task SwitchAsync(Func<Task> success, Func<Error, Task> failure)
|
||||
{
|
||||
return IsSuccess ? success() : failure(Error);
|
||||
}
|
||||
}
|
||||
|
||||
public partial class Result
|
||||
{
|
||||
/// <summary>
|
||||
/// Asynchronously matches over the success or failure of the result and returns another value. Can be
|
||||
/// conceptualized as an exhaustive <c>switch</c> expression matching all possible states of the type.
|
||||
/// </summary>
|
||||
/// <typeparam name="TResult">The type to return from the match.</typeparam>
|
||||
/// <param name="success">The function to invoke if the result is a success.</param>
|
||||
/// <param name="failure">The function to apply to the failure value of the result if the result is a failure.</param>
|
||||
/// <returns>A task completing with the result of applying either <paramref name="success"/> or
|
||||
/// <paramref name="failure"/> on the failure value of the result.</returns>
|
||||
[Pure]
|
||||
public ValueTask<TResult> MatchAsync<TResult>(Func<ValueTask<TResult>> success, Func<Error, ValueTask<TResult>> failure)
|
||||
{
|
||||
return IsSuccess ? success() : failure(Error);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Asynchronously matches over the success or failure of the result and invokes an effectful action onto the
|
||||
/// failure value. Can be conceptualized as an exhaustive <c>switch</c> statement matching all possible states of
|
||||
/// the type.
|
||||
/// </summary>
|
||||
/// <param name="success">The function to call if the result is a success.</param>
|
||||
/// <param name="failure">The function to call with the failure value of the result if the result is a failure.</param>
|
||||
public ValueTask SwitchAsync(Func<ValueTask> success, Func<Error, ValueTask> failure)
|
||||
{
|
||||
return IsSuccess ? success() : failure(Error);
|
||||
}
|
||||
}
|
||||
|
||||
public partial class Result<T>
|
||||
{
|
||||
/// <summary>
|
||||
/// Matches over the success value or failure value of the result and returns another value. Can be conceptualized
|
||||
/// as an exhaustive <c>switch</c> expression matching all possible values of the type.
|
||||
/// </summary>
|
||||
/// <typeparam name="TResult">The type to return from the match.</typeparam>
|
||||
/// <param name="success">The function to apply to the success value of the result if the result is a success.</param>
|
||||
/// <param name="failure">The function to apply to the failure value of the result if the result is a failure.</param>
|
||||
/// <returns>The result of applying either <paramref name="success"/> or <paramref name="failure"/> on the success
|
||||
/// value or failure value of the result.</returns>
|
||||
[Pure]
|
||||
public TResult Match<TResult>(Func<T, TResult> success, Func<Error, TResult> failure)
|
||||
{
|
||||
return IsSuccess ? success(Value) : failure(Error);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Matches over the success value or failure value of the result and invokes an effectful action onto the success
|
||||
/// value or failure value. Can be conceptualized as an exhaustive <c>switch</c> statement matching all possible
|
||||
/// values of the type.
|
||||
/// </summary>
|
||||
/// <param name="success">The function to call with the success value of the result if the result is a success.</param>
|
||||
/// <param name="failure">The function to call with the failure value of the result if the result is a failure.</param>
|
||||
public void Switch(Action<T> success, Action<Error> failure)
|
||||
{
|
||||
if (IsSuccess)
|
||||
{
|
||||
success(Value);
|
||||
}
|
||||
else
|
||||
{
|
||||
failure(Error);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public partial class Result<T>
|
||||
{
|
||||
/// <summary>
|
||||
/// Asynchronously matches over the success value or failure value of the result and returns another value. Can be
|
||||
/// conceptualized as an exhaustive <c>switch</c> expression matching all possible values of the type.
|
||||
/// </summary>
|
||||
/// <typeparam name="TResult">The type to return from the match.</typeparam>
|
||||
/// <param name="success">The function to apply to the success value of the result if the result is a success.</param>
|
||||
/// <param name="failure">The function to apply to the failure value of the result if the result is a failure.</param>
|
||||
/// <returns>A task completing with the result of applying either <paramref name="success"/> or
|
||||
/// <paramref name="failure"/> on the success value or failure value of the result.</returns>
|
||||
[Pure]
|
||||
public async Task<TResult> MatchAsync<TResult>(Func<T, Task<TResult>> success, Func<Error, Task<TResult>> failure)
|
||||
{
|
||||
return IsSuccess ? await success(Value) : await failure(Error);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Asynchronously matches over the success value or failure value of the result and invokes an effectful action
|
||||
/// onto the success value or failure value. Can be conceptualized as an exhaustive <c>switch</c> statement matching
|
||||
/// all possible values of
|
||||
/// the type.
|
||||
/// </summary>
|
||||
/// <param name="success">The function to call with the success value of the result if the result is a success.</param>
|
||||
/// <param name="failure">The function to call with the failure value of the result if the result is a failure.</param>
|
||||
public Task SwitchAsync(Func<T, Task> success, Func<Error, Task> failure)
|
||||
{
|
||||
return IsSuccess ? success(Value) : failure(Error);
|
||||
}
|
||||
}
|
||||
|
||||
public partial class Result<T>
|
||||
{
|
||||
/// <summary>
|
||||
/// Asynchronously matches over the success value or failure value of the result and returns another value. Can be
|
||||
/// conceptualized as an exhaustive <c>switch</c> expression matching all possible values of the type.
|
||||
/// </summary>
|
||||
/// <typeparam name="TResult">The type to return from the match.</typeparam>
|
||||
/// <param name="success">The function to apply to the success value of the result if the result is a success.</param>
|
||||
/// <param name="failure">The function to apply to the failure value of the result if the result is a failure.</param>
|
||||
/// <returns>A task completing with the result of applying either <paramref name="success"/> or
|
||||
/// <paramref name="failure"/> on the success value or failure value of the result.</returns>
|
||||
[Pure]
|
||||
public ValueTask<TResult> MatchAsync<TResult>(Func<T, ValueTask<TResult>> success, Func<Error, ValueTask<TResult>> failure)
|
||||
{
|
||||
return IsSuccess ? success(Value) : failure(Error);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Asynchronously matches over the success value or failure value of the result and invokes an effectful action
|
||||
/// onto the success value or the failure value. Can be conceptualized as an exhaustive <c>switch</c> statement
|
||||
/// matching all possible values of the type.
|
||||
/// </summary>
|
||||
/// <param name="success">The function to call with the success value of the result if the result is a success.</param>
|
||||
/// <param name="failure">The function to call with the failure value of the result if the result is a failure.</param>
|
||||
public ValueTask SwitchAsync(Func<T, ValueTask> success, Func<Error, ValueTask> failure)
|
||||
{
|
||||
return IsSuccess ? success(Value) : failure(Error);
|
||||
}
|
||||
}
|
||||
1059
src/request.result/Result.Transform.cs
Normal file
1059
src/request.result/Result.Transform.cs
Normal file
File diff suppressed because it is too large
Load diff
66
src/request.result/Result.Unbox.cs
Normal file
66
src/request.result/Result.Unbox.cs
Normal file
|
|
@ -0,0 +1,66 @@
|
|||
// Copyright (c) The Geekeey Authors
|
||||
// SPDX-License-Identifier: EUPL-1.2
|
||||
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Diagnostics.Contracts;
|
||||
|
||||
namespace Geekeey.Request.Result;
|
||||
|
||||
public partial class Result<T>
|
||||
{
|
||||
/// <summary>
|
||||
/// Tries to get the success value from the result.
|
||||
/// </summary>
|
||||
/// <param name="value">The success value of the result.</param>
|
||||
/// <returns>Whether the result has success value.</returns>
|
||||
[Pure]
|
||||
public bool TryGetValue([MaybeNullWhen(false)] out T value)
|
||||
{
|
||||
value = Value;
|
||||
|
||||
return IsSuccess;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tries to get the success value from the result.
|
||||
/// </summary>
|
||||
/// <param name="value">The success value of the result.</param>
|
||||
/// <param name="error">The failure value of the result.</param>
|
||||
/// <returns>Whether the result has a success value.</returns>
|
||||
[Pure]
|
||||
public bool TryGetValue([MaybeNullWhen(false)] out T value, [MaybeNullWhen(true)] out Error error)
|
||||
{
|
||||
value = Value;
|
||||
error = !IsSuccess ? Error : null;
|
||||
|
||||
return IsSuccess;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tries to get the failure value from the result.
|
||||
/// </summary>
|
||||
/// <param name="error">The failure value of the result.</param>
|
||||
/// <returns>Whether the result has a failure value.</returns>
|
||||
[Pure]
|
||||
public bool TryGetValue([MaybeNullWhen(false)] out Error error)
|
||||
{
|
||||
error = !IsSuccess ? Error : null;
|
||||
|
||||
return !IsSuccess;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tries to get the failure value from the result.
|
||||
/// </summary>
|
||||
/// <param name="error">The failure value of the result.</param>
|
||||
/// <param name="value">The success value of the result.</param>
|
||||
/// <returns>Whether the result a failure value.</returns>
|
||||
[Pure]
|
||||
public bool TryGetValue([MaybeNullWhen(false)] out Error error, [MaybeNullWhen(true)] out T value)
|
||||
{
|
||||
error = !IsSuccess ? Error : null;
|
||||
value = Value;
|
||||
|
||||
return !IsSuccess;
|
||||
}
|
||||
}
|
||||
141
src/request.result/Result.cs
Normal file
141
src/request.result/Result.cs
Normal file
|
|
@ -0,0 +1,141 @@
|
|||
// Copyright (c) The Geekeey Authors
|
||||
// SPDX-License-Identifier: EUPL-1.2
|
||||
|
||||
using System.Diagnostics;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Diagnostics.Contracts;
|
||||
|
||||
namespace Geekeey.Request.Result;
|
||||
|
||||
/// <summary>
|
||||
/// A type which contains either a success value or a failure value, which is represented by an <see cref="Error"/>.
|
||||
/// </summary>
|
||||
public partial class Result : IResultFactory<Result>
|
||||
{
|
||||
/// <summary>
|
||||
/// Creates a new result with a success value.
|
||||
/// </summary>
|
||||
public Result()
|
||||
{
|
||||
Error = default;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new result with a failure value.
|
||||
/// </summary>
|
||||
/// <param name="error">The error of the result.</param>
|
||||
public Result(Error error)
|
||||
{
|
||||
Error = error;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Represents the error associated with a failed result.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// When a result is unsuccessful, this property will hold an instance of a type derived from <see cref="Error"/>.
|
||||
/// It is null when the result is successful.
|
||||
/// </remarks>
|
||||
public Error? Error { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Whether the result is a success.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// This is always the inverse of <see cref="IsFailure"/> but is more specific about intent.
|
||||
/// </remarks>
|
||||
[MemberNotNullWhen(false, nameof(Error))]
|
||||
public virtual bool IsSuccess => Error is null;
|
||||
|
||||
/// <summary>
|
||||
/// Whether the result is a failure.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// This is always the inverse of <see cref="IsSuccess"/> but is more specific about intent.
|
||||
/// </remarks>
|
||||
[MemberNotNullWhen(true, nameof(Error))]
|
||||
public virtual bool IsFailure => !IsSuccess;
|
||||
|
||||
/// <inheritdoc/>
|
||||
[Pure]
|
||||
static Result IResultFactory<Result>.Failure(Error error)
|
||||
{
|
||||
return new Result(error);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a string representation of the result.
|
||||
/// </summary>
|
||||
[Pure]
|
||||
public override string ToString()
|
||||
{
|
||||
return IsSuccess ? $"Success" : $"Failure {{ {Error} }}";
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A type which contains either a success value or a failure value, which is represented by an <see cref="Error"/>.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of the success value.</typeparam>
|
||||
[DebuggerTypeProxy(typeof(Result<>.ResultDebugProxy))]
|
||||
public partial class Result<T> : Result, IResultFactory<Result<T>>
|
||||
{
|
||||
/// <summary>
|
||||
/// Creates a new result with a success value.
|
||||
/// </summary>
|
||||
/// <param name="value">The success value.</param>
|
||||
public Result(T value) : base()
|
||||
{
|
||||
Value = value;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new result with a failure value.
|
||||
/// </summary>
|
||||
/// <param name="error">The error of the result.</param>
|
||||
public Result(Error error) : base(error)
|
||||
{
|
||||
Value = default;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the success value of the result if the operation was successful.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// This property contains the value of type <typeparamref name="T"/> when the result is successful.
|
||||
/// If the result is unsuccessful, this property will be null or default, depending on the type.
|
||||
/// Accessing this property when the result is unsuccessful may raise an exception if not properly handled.
|
||||
/// </remarks>
|
||||
public T? Value { get; }
|
||||
|
||||
/// <inheritdoc/>
|
||||
[MemberNotNullWhen(true, nameof(Value))]
|
||||
public override bool IsSuccess => base.IsSuccess;
|
||||
|
||||
/// <inheritdoc/>
|
||||
[MemberNotNullWhen(false, nameof(Value))]
|
||||
public override bool IsFailure => base.IsFailure;
|
||||
|
||||
/// <inheritdoc/>
|
||||
[Pure]
|
||||
static Result<T> IResultFactory<Result<T>>.Failure(Error error)
|
||||
{
|
||||
return new Result<T>(error);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a string representation of the result.
|
||||
/// </summary>
|
||||
[Pure]
|
||||
public override string ToString()
|
||||
{
|
||||
return IsSuccess ? $"Success {{ {Value} }}" : $"Failure {{ {Error} }}";
|
||||
}
|
||||
|
||||
private sealed class ResultDebugProxy(Result<T> result)
|
||||
{
|
||||
public bool IsSuccess => result.IsSuccess;
|
||||
|
||||
public object? Value => result.IsSuccess ? result.Value : result.Error;
|
||||
}
|
||||
}
|
||||
27
src/request.result/_errors/AggregateError.cs
Normal file
27
src/request.result/_errors/AggregateError.cs
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
// Copyright (c) The Geekeey Authors
|
||||
// SPDX-License-Identifier: EUPL-1.2
|
||||
|
||||
namespace Geekeey.Request.Result;
|
||||
|
||||
/// <summary>
|
||||
/// An error which is a combination of other errors.
|
||||
/// </summary>
|
||||
public sealed class AggregateError : Error
|
||||
{
|
||||
/// <summary>
|
||||
/// An error which is a combination of other errors.
|
||||
/// </summary>
|
||||
/// <param name="errors">The errors the error consists of.</param>
|
||||
public AggregateError(IEnumerable<Error> errors)
|
||||
{
|
||||
Errors = [.. errors];
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The errors the error consists of.
|
||||
/// </summary>
|
||||
public IReadOnlyCollection<Error> Errors { get; }
|
||||
|
||||
/// <inheritdoc/>
|
||||
public override string Message => string.Join(Environment.NewLine, Errors.Select(error => error.Message));
|
||||
}
|
||||
56
src/request.result/_errors/Error.cs
Normal file
56
src/request.result/_errors/Error.cs
Normal file
|
|
@ -0,0 +1,56 @@
|
|||
// Copyright (c) The Geekeey Authors
|
||||
// SPDX-License-Identifier: EUPL-1.2
|
||||
|
||||
#pragma warning disable CA1716
|
||||
|
||||
namespace Geekeey.Request.Result;
|
||||
|
||||
/// <summary>
|
||||
/// An error containing a simple message. Makes up the other half of a <see cref="Result{T}"/> which might be an error.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// An error is conceptually very similar to an exception but without the ability to be thrown, meant to be a more
|
||||
/// lightweight type meant to be wrapped in a <see cref="Result{T}"/>.
|
||||
/// An error fundamentally only contains a single string message, however other more concrete types such as
|
||||
/// <see cref="ExceptionError"/> or <see cref="AggregateError"/> may define other properties.
|
||||
/// Errors are meant to be small, specific, and descriptive, such that they are easy to match over and provide specific
|
||||
/// handling for specific kinds of errors.
|
||||
/// </remarks>
|
||||
public abstract class Error
|
||||
{
|
||||
/// <summary>
|
||||
/// A statically accessible default "Result has no value." error.
|
||||
/// </summary>
|
||||
internal static Error DefaultValueError { get; } = new StringError("The result has no value.");
|
||||
|
||||
/// <summary>
|
||||
/// The message used to display the error.
|
||||
/// </summary>
|
||||
public abstract string Message { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets a string representation of the error. Returns <see cref="Message"/> by default.
|
||||
/// </summary>
|
||||
public override string ToString()
|
||||
{
|
||||
return Message;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Implicitly converts a string into a <see cref="StringError"/>.
|
||||
/// </summary>
|
||||
/// <param name="message">The message of the error.</param>
|
||||
public static implicit operator Error(string message)
|
||||
{
|
||||
return new StringError(message);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Implicitly converts an exception into an <see cref="ExceptionError"/>.
|
||||
/// </summary>
|
||||
/// <param name="exception">The exception to convert.</param>
|
||||
public static implicit operator Error(Exception exception)
|
||||
{
|
||||
return new ExceptionError(exception);
|
||||
}
|
||||
}
|
||||
29
src/request.result/_errors/ExceptionError.cs
Normal file
29
src/request.result/_errors/ExceptionError.cs
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
// Copyright (c) The Geekeey Authors
|
||||
// SPDX-License-Identifier: EUPL-1.2
|
||||
|
||||
namespace Geekeey.Request.Result;
|
||||
|
||||
/// <summary>
|
||||
/// An error which is constructed from an exception.
|
||||
/// </summary>
|
||||
public sealed class ExceptionError : Error
|
||||
{
|
||||
/// <summary>
|
||||
/// An error which is constructed from an exception.
|
||||
/// </summary>
|
||||
/// <param name="exception">The exception in the error.</param>
|
||||
public ExceptionError(Exception exception)
|
||||
{
|
||||
Exception = exception;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The exception in the error.
|
||||
/// </summary>
|
||||
public Exception Exception { get; }
|
||||
|
||||
/// <summary>
|
||||
/// The exception in the error.
|
||||
/// </summary>
|
||||
public override string Message => Exception.Message;
|
||||
}
|
||||
24
src/request.result/_errors/StringError.cs
Normal file
24
src/request.result/_errors/StringError.cs
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
// Copyright (c) The Geekeey Authors
|
||||
// SPDX-License-Identifier: EUPL-1.2
|
||||
|
||||
namespace Geekeey.Request.Result;
|
||||
|
||||
/// <summary>
|
||||
/// An error which displays a simple string.
|
||||
/// </summary>
|
||||
public sealed class StringError : Error
|
||||
{
|
||||
private readonly string _message;
|
||||
|
||||
/// <summary>
|
||||
/// An error which displays a simple string.
|
||||
/// </summary>
|
||||
/// <param name="message">The message to display.</param>
|
||||
public StringError(string message)
|
||||
{
|
||||
_message = message;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public override string Message => _message;
|
||||
}
|
||||
26
src/request.result/_exceptions/UnwrapException.cs
Normal file
26
src/request.result/_exceptions/UnwrapException.cs
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
// Copyright (c) The Geekeey Authors
|
||||
// SPDX-License-Identifier: EUPL-1.2
|
||||
|
||||
namespace Geekeey.Request.Result;
|
||||
|
||||
/// <summary>
|
||||
/// The exception is thrown when an <see cref="Result{T}"/> is attempted to be unwrapped contains only a failure value.
|
||||
/// </summary>
|
||||
public sealed class UnwrapException : Exception
|
||||
{
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="UnwrapException"/>.
|
||||
/// </summary>
|
||||
public UnwrapException()
|
||||
: base("Cannot unwrap result because it does not have a value.")
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="UnwrapException"/>.
|
||||
/// </summary>
|
||||
/// <param name="error">An error message.</param>
|
||||
public UnwrapException(string error) : base(error)
|
||||
{
|
||||
}
|
||||
}
|
||||
90
src/request.result/_extensions/Extensions.Enumerable.cs
Normal file
90
src/request.result/_extensions/Extensions.Enumerable.cs
Normal file
|
|
@ -0,0 +1,90 @@
|
|||
// Copyright (c) The Geekeey Authors
|
||||
// SPDX-License-Identifier: EUPL-1.2
|
||||
|
||||
namespace Geekeey.Request.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 T? value, out var error))
|
||||
{
|
||||
return new Result<IReadOnlyList<T>>(error);
|
||||
}
|
||||
|
||||
list.Add(value);
|
||||
}
|
||||
|
||||
return list;
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="Join{T}(IEnumerable{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}(IEnumerable{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 T? value, out var error))
|
||||
{
|
||||
return new Result<IReadOnlyList<T>>(error);
|
||||
}
|
||||
|
||||
list.Add(value);
|
||||
}
|
||||
|
||||
return list;
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="Join{T}(IEnumerable{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}(IEnumerable{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 T? value, out var error))
|
||||
{
|
||||
return new Result<IReadOnlyList<T>>(error);
|
||||
}
|
||||
|
||||
list.Add(value);
|
||||
}
|
||||
|
||||
return list;
|
||||
}
|
||||
}
|
||||
599
src/request.result/_extensions/Extensions.Task.cs
Normal file
599
src/request.result/_extensions/Extensions.Task.cs
Normal file
|
|
@ -0,0 +1,599 @@
|
|||
// Copyright (c) The Geekeey Authors
|
||||
// SPDX-License-Identifier: EUPL-1.2
|
||||
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
namespace Geekeey.Request.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 returning 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>> Map<T, TNew>(this Task<Result<T>> result, Func<T, TNew> func)
|
||||
{
|
||||
return (await result).Map(func);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="Map{T,TNew}(Task{Result{T}},Func{T,TNew})"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
// ReSharper disable once InconsistentNaming
|
||||
public static async Task<Result<TNew>> TryMap<T, TNew>(this Task<Result<T>> result, Func<T, TNew> func)
|
||||
{
|
||||
return (await result).TryMap(func);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="Map{T,TNew}(Task{Result{T}},Func{T,TNew})"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static async Task<Result<TNew>> MapAsync<T, TNew>(this Task<Result<T>> result, Func<T, Task<TNew>> func)
|
||||
{
|
||||
return await (await result).MapAsync(func);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="Map{T,TNew}(Task{Result{T}},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)
|
||||
{
|
||||
return await (await result).MapAsync(func);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="Map{T,TNew}(Task{Result{T}},Func{T,TNew})"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static async Task<Result<TNew>> TryMapAsync<T, TNew>(this Task<Result<T>> result, Func<T, Task<TNew>> func)
|
||||
{
|
||||
return await (await result).TryMapAsync(func);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="Map{T,TNew}(Task{Result{T}},Func{T,TNew})"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static async Task<Result<TNew>> TryMapAsync<T, TNew>(this Task<Result<T>> result, Func<T, ValueTask<TNew>> func)
|
||||
{
|
||||
return await (await result).TryMapAsync(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 returning 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)
|
||||
{
|
||||
return (await result).Then(func);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="Then{T,TNew}(Task{Result{T}},Func{T,Result{TNew}})"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
// ReSharper disable once InconsistentNaming
|
||||
public static async Task<Result<TNew>> ThenTry<T, TNew>(this Task<Result<T>> result, Func<T, Result<TNew>> func)
|
||||
{
|
||||
return (await result).ThenTry(func);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="Then{T,TNew}(Task{Result{T}},Func{T,Result{TNew}})"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static async Task<Result<TNew>> ThenAsync<T, TNew>(this Task<Result<T>> result, Func<T, Task<Result<TNew>>> func)
|
||||
{
|
||||
return await (await result).ThenAsync(func);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="Then{T,TNew}(Task{Result{T}},Func{T,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)
|
||||
{
|
||||
return await (await result).ThenAsync(func);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="Then{T,TNew}(Task{Result{T}},Func{T,Result{TNew}})"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static async Task<Result<TNew>> ThenTryAsync<T, TNew>(this Task<Result<T>> result, Func<T, Task<Result<TNew>>> func)
|
||||
{
|
||||
return await (await result).ThenTryAsync(func);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="Then{T,TNew}(Task{Result{T}},Func{T,Result{TNew}})"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static async Task<Result<TNew>> ThenTryAsync<T, TNew>(this Task<Result<T>> result, Func<T, ValueTask<Result<TNew>>> func)
|
||||
{
|
||||
return await (await result).ThenTryAsync(func);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="Then{T,TNew}(Task{Result{T}},Func{T,Result{TNew}})"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
// ReSharper disable once InconsistentNaming
|
||||
public static async Task<Result> Then<T>(this Task<Result<T>> result, Func<T, Result> func)
|
||||
{
|
||||
return (await result).Then(func);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="Then{T,TNew}(Task{Result{T}},Func{T,Result{TNew}})"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
// ReSharper disable once InconsistentNaming
|
||||
public static async Task<Result> ThenTry<T>(this Task<Result<T>> result, Func<T, Result> func)
|
||||
{
|
||||
return (await result).ThenTry(func);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="Then{T,TNew}(Task{Result{T}},Func{T,Result{TNew}})"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static async Task<Result> ThenAsync<T>(this Task<Result<T>> result, Func<T, Task<Result>> func)
|
||||
{
|
||||
return await (await result).ThenAsync(func);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="Then{T,TNew}(Task{Result{T}},Func{T,Result{TNew}})"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static async Task<Result> ThenAsync<T>(this Task<Result<T>> result, Func<T, ValueTask<Result>> func)
|
||||
{
|
||||
return await (await result).ThenAsync(func);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="Then{T,TNew}(Task{Result{T}},Func{T,Result{TNew}})"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static async Task<Result> ThenTryAsync<T>(this Task<Result<T>> result, Func<T, Task<Result>> func)
|
||||
{
|
||||
return await (await result).ThenTryAsync(func);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="Then{T,TNew}(Task{Result{T}},Func{T,Result{TNew}})"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static async Task<Result> ThenTryAsync<T>(this Task<Result<T>> result, Func<T, ValueTask<Result>> func)
|
||||
{
|
||||
return await (await result).ThenTryAsync(func);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region ValueTask<Result<T>>
|
||||
|
||||
/// <inheritdoc cref="Map{T,TNew}(Task{Result{T}},Func{T,TNew})"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
// ReSharper disable once InconsistentNaming
|
||||
public static async ValueTask<Result<TNew>> Map<T, TNew>(this ValueTask<Result<T>> result, Func<T, TNew> func)
|
||||
{
|
||||
return (await result).Map(func);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="Map{T,TNew}(Task{Result{T}},Func{T,TNew})"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
// ReSharper disable once InconsistentNaming
|
||||
public static async ValueTask<Result<TNew>> TryMap<T, TNew>(this ValueTask<Result<T>> result, Func<T, TNew> func)
|
||||
{
|
||||
return (await result).TryMap(func);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="Map{T,TNew}(Task{Result{T}},Func{T,TNew})"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static async ValueTask<Result<TNew>> MapAsync<T, TNew>(this ValueTask<Result<T>> result, Func<T, Task<TNew>> func)
|
||||
{
|
||||
return await (await result).MapAsync(func);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="Map{T,TNew}(Task{Result{T}},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)
|
||||
{
|
||||
return await (await result).MapAsync(func);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="Map{T,TNew}(Task{Result{T}},Func{T,TNew})"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static async ValueTask<Result<TNew>> TryMapAsync<T, TNew>(this ValueTask<Result<T>> result, Func<T, Task<TNew>> func)
|
||||
{
|
||||
return await (await result).TryMapAsync(func);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="Map{T,TNew}(Task{Result{T}},Func{T,TNew})"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static async ValueTask<Result<TNew>> TryMapAsync<T, TNew>(this ValueTask<Result<T>> result, Func<T, ValueTask<TNew>> func)
|
||||
{
|
||||
return await (await result).TryMapAsync(func);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="Then{T,TNew}(Task{Result{T}},Func{T,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)
|
||||
{
|
||||
return (await result).Then(func);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="Then{T,TNew}(Task{Result{T}},Func{T,Result{TNew}})"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
// ReSharper disable once InconsistentNaming
|
||||
public static async ValueTask<Result<TNew>> ThenTry<T, TNew>(this ValueTask<Result<T>> result, Func<T, Result<TNew>> func)
|
||||
{
|
||||
return (await result).ThenTry(func);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="Then{T,TNew}(Task{Result{T}},Func{T,Result{TNew}})"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static async ValueTask<Result<TNew>> ThenAsync<T, TNew>(this ValueTask<Result<T>> result, Func<T, Task<Result<TNew>>> func)
|
||||
{
|
||||
return await (await result).ThenAsync(func);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="Then{T,TNew}(Task{Result{T}},Func{T,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)
|
||||
{
|
||||
return await (await result).ThenAsync(func);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="Then{T,TNew}(Task{Result{T}},Func{T,Result{TNew}})"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static async ValueTask<Result<TNew>> ThenTryAsync<T, TNew>(this ValueTask<Result<T>> result, Func<T, Task<Result<TNew>>> func)
|
||||
{
|
||||
return await (await result).ThenTryAsync(func);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="Then{T,TNew}(Task{Result{T}},Func{T,Result{TNew}})"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static async ValueTask<Result<TNew>> ThenTryAsync<T, TNew>(this ValueTask<Result<T>> result, Func<T, ValueTask<Result<TNew>>> func)
|
||||
{
|
||||
return await (await result).ThenTryAsync(func);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="Then{T,TNew}(Task{Result{T}},Func{T,Result{TNew}})"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
// ReSharper disable once InconsistentNaming
|
||||
public static async ValueTask<Result> Then<T>(this ValueTask<Result<T>> result, Func<T, Result> func)
|
||||
{
|
||||
return (await result).Then(func);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="Then{T,TNew}(Task{Result{T}},Func{T,Result{TNew}})"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
// ReSharper disable once InconsistentNaming
|
||||
public static async ValueTask<Result> ThenTry<T>(this ValueTask<Result<T>> result, Func<T, Result> func)
|
||||
{
|
||||
return (await result).ThenTry(func);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="Then{T,TNew}(Task{Result{T}},Func{T,Result{TNew}})"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static async ValueTask<Result> ThenAsync<T>(this ValueTask<Result<T>> result, Func<T, Task<Result>> func)
|
||||
{
|
||||
return await (await result).ThenAsync(func);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="Then{T,TNew}(Task{Result{T}},Func{T,Result{TNew}})"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static async ValueTask<Result> ThenAsync<T>(this ValueTask<Result<T>> result, Func<T, ValueTask<Result>> func)
|
||||
{
|
||||
return await (await result).ThenAsync(func);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="Then{T,TNew}(Task{Result{T}},Func{T,Result{TNew}})"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static async ValueTask<Result> ThenTryAsync<T>(this ValueTask<Result<T>> result, Func<T, Task<Result>> func)
|
||||
{
|
||||
return await (await result).ThenTryAsync(func);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="Then{T,TNew}(Task{Result{T}},Func{T,Result{TNew}})"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static async ValueTask<Result> ThenTryAsync<T>(this ValueTask<Result<T>> result, Func<T, ValueTask<Result>> func)
|
||||
{
|
||||
return await (await result).ThenTryAsync(func);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Task<Result>
|
||||
|
||||
/// <summary>
|
||||
/// Maps the success of the result object of the completed task to a new success value 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 returning a result object when completing.</param>
|
||||
/// <param name="func">The function used to map the success to a new value.</param>
|
||||
/// <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>> Map<TNew>(this Task<Result> result, Func<TNew> func)
|
||||
{
|
||||
return (await result).Map(func);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="Map{TNew}(Task{Result},Func{TNew})"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
// ReSharper disable once InconsistentNaming
|
||||
public static async Task<Result<TNew>> TryMap<TNew>(this Task<Result> result, Func<TNew> func)
|
||||
{
|
||||
return (await result).TryMap(func);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="Map{TNew}(Task{Result},Func{TNew})"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static async Task<Result<TNew>> MapAsync<TNew>(this Task<Result> result, Func<Task<TNew>> func)
|
||||
{
|
||||
return await (await result).MapAsync(func);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="Map{TNew}(Task{Result},Func{TNew})"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static async Task<Result<TNew>> MapAsync<TNew>(this Task<Result> result, Func<ValueTask<TNew>> func)
|
||||
{
|
||||
return await (await result).MapAsync(func);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="Map{TNew}(Task{Result},Func{TNew})"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static async Task<Result<TNew>> TryMapAsync<TNew>(this Task<Result> result, Func<Task<TNew>> func)
|
||||
{
|
||||
return await (await result).TryMapAsync(func);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="Map{TNew}(Task{Result},Func{TNew})"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static async Task<Result<TNew>> TryMapAsync<TNew>(this Task<Result> result, Func<ValueTask<TNew>> func)
|
||||
{
|
||||
return await (await result).TryMapAsync(func);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Chains another result to the result object of the completed task if it is a success, or does nothing if the
|
||||
/// result object of the completed task is a failure.
|
||||
/// </summary>
|
||||
/// <param name="result">A task object returning a result object when completing.</param>
|
||||
/// <param name="func">The function used to create the next result.</param>
|
||||
/// <returns>A result which is either the mapped result or a new result containing the failure value of the original
|
||||
/// result.</returns>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
// ReSharper disable once InconsistentNaming
|
||||
public static async Task<Result> Then(this Task<Result> result, Func<Result> func)
|
||||
{
|
||||
return (await result).Then(func);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="Then(Task{Result},Func{Result})"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
// ReSharper disable once InconsistentNaming
|
||||
public static async Task<Result> ThenTry(this Task<Result> result, Func<Result> func)
|
||||
{
|
||||
return (await result).ThenTry(func);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="Then(Task{Result},Func{Result})"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static async Task<Result> ThenAsync(this Task<Result> result, Func<Task<Result>> func)
|
||||
{
|
||||
return await (await result).ThenAsync(func);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="Then(Task{Result},Func{Result})"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static async Task<Result> ThenAsync(this Task<Result> result, Func<ValueTask<Result>> func)
|
||||
{
|
||||
return await (await result).ThenAsync(func);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="Then(Task{Result},Func{Result})"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static async Task<Result> ThenTryAsync(this Task<Result> result, Func<Task<Result>> func)
|
||||
{
|
||||
return await (await result).ThenTryAsync(func);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="Then(Task{Result},Func{Result})"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static async Task<Result> ThenTryAsync(this Task<Result> result, Func<ValueTask<Result>> func)
|
||||
{
|
||||
return await (await result).ThenTryAsync(func);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Chains a generic result to the result object of the completed task if it is a success, or does nothing if the
|
||||
/// result object of the completed task is a failure.
|
||||
/// </summary>
|
||||
/// <param name="result">A task object returning a result object when completing.</param>
|
||||
/// <param name="func">The function used to create the next generic result.</param>
|
||||
/// <typeparam name="TNew">The type of the new value.</typeparam>
|
||||
/// <returns>A result which is either the mapped result or a new result containing the failure value of the original
|
||||
/// result.</returns>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
// ReSharper disable once InconsistentNaming
|
||||
public static async Task<Result<TNew>> Then<TNew>(this Task<Result> result, Func<Result<TNew>> func)
|
||||
{
|
||||
return (await result).Then(func);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="Then{TNew}(Task{Result},Func{Result{TNew}})"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
// ReSharper disable once InconsistentNaming
|
||||
public static async Task<Result<TNew>> ThenTry<TNew>(this Task<Result> result, Func<Result<TNew>> func)
|
||||
{
|
||||
return (await result).ThenTry(func);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="Then{TNew}(Task{Result},Func{Result{TNew}})"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static async Task<Result<TNew>> ThenAsync<TNew>(this Task<Result> result, Func<Task<Result<TNew>>> func)
|
||||
{
|
||||
return await (await result).ThenAsync(func);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="Then{TNew}(Task{Result},Func{Result{TNew}})"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static async Task<Result<TNew>> ThenAsync<TNew>(this Task<Result> result, Func<ValueTask<Result<TNew>>> func)
|
||||
{
|
||||
return await (await result).ThenAsync(func);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="Then{TNew}(Task{Result},Func{Result{TNew}})"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static async Task<Result<TNew>> ThenTryAsync<TNew>(this Task<Result> result, Func<Task<Result<TNew>>> func)
|
||||
{
|
||||
return await (await result).ThenTryAsync(func);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="Then{TNew}(Task{Result},Func{Result{TNew}})"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static async Task<Result<TNew>> ThenTryAsync<TNew>(this Task<Result> result, Func<ValueTask<Result<TNew>>> func)
|
||||
{
|
||||
return await (await result).ThenTryAsync(func);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region ValueTask<Result>
|
||||
|
||||
/// <inheritdoc cref="Map{TNew}(Task{Result},Func{TNew})"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
// ReSharper disable once InconsistentNaming
|
||||
public static async ValueTask<Result<TNew>> Map<TNew>(this ValueTask<Result> result, Func<TNew> func)
|
||||
{
|
||||
return (await result).Map(func);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="Map{TNew}(Task{Result},Func{TNew})"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
// ReSharper disable once InconsistentNaming
|
||||
public static async ValueTask<Result<TNew>> TryMap<TNew>(this ValueTask<Result> result, Func<TNew> func)
|
||||
{
|
||||
return (await result).TryMap(func);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="Map{TNew}(Task{Result},Func{TNew})"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static async ValueTask<Result<TNew>> MapAsync<TNew>(this ValueTask<Result> result, Func<Task<TNew>> func)
|
||||
{
|
||||
return await (await result).MapAsync(func);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="Map{TNew}(Task{Result},Func{TNew})"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static async ValueTask<Result<TNew>> MapAsync<TNew>(this ValueTask<Result> result, Func<ValueTask<TNew>> func)
|
||||
{
|
||||
return await (await result).MapAsync(func);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="Map{TNew}(Task{Result},Func{TNew})"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static async ValueTask<Result<TNew>> TryMapAsync<TNew>(this ValueTask<Result> result, Func<Task<TNew>> func)
|
||||
{
|
||||
return await (await result).TryMapAsync(func);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="Map{TNew}(Task{Result},Func{TNew})"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static async ValueTask<Result<TNew>> TryMapAsync<TNew>(this ValueTask<Result> result, Func<ValueTask<TNew>> func)
|
||||
{
|
||||
return await (await result).TryMapAsync(func);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="Then(Task{Result},Func{Result})"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
// ReSharper disable once InconsistentNaming
|
||||
public static async ValueTask<Result> Then(this ValueTask<Result> result, Func<Result> func)
|
||||
{
|
||||
return (await result).Then(func);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="Then(Task{Result},Func{Result})"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
// ReSharper disable once InconsistentNaming
|
||||
public static async ValueTask<Result> ThenTry(this ValueTask<Result> result, Func<Result> func)
|
||||
{
|
||||
return (await result).ThenTry(func);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="Then(Task{Result},Func{Result})"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static async ValueTask<Result> ThenAsync(this ValueTask<Result> result, Func<Task<Result>> func)
|
||||
{
|
||||
return await (await result).ThenAsync(func);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="Then(Task{Result},Func{Result})"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static async ValueTask<Result> ThenAsync(this ValueTask<Result> result, Func<ValueTask<Result>> func)
|
||||
{
|
||||
return await (await result).ThenAsync(func);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="Then(Task{Result},Func{Result})"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static async ValueTask<Result> ThenTryAsync(this ValueTask<Result> result, Func<Task<Result>> func)
|
||||
{
|
||||
return await (await result).ThenTryAsync(func);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="Then(Task{Result},Func{Result})"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static async ValueTask<Result> ThenTryAsync(this ValueTask<Result> result, Func<ValueTask<Result>> func)
|
||||
{
|
||||
return await (await result).ThenTryAsync(func);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="Then{TNew}(Task{Result},Func{Result{TNew}})"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
// ReSharper disable once InconsistentNaming
|
||||
public static async ValueTask<Result<TNew>> Then<TNew>(this ValueTask<Result> result, Func<Result<TNew>> func)
|
||||
{
|
||||
return (await result).Then(func);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="Then{TNew}(Task{Result},Func{Result{TNew}})"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
// ReSharper disable once InconsistentNaming
|
||||
public static async ValueTask<Result<TNew>> ThenTry<TNew>(this ValueTask<Result> result, Func<Result<TNew>> func)
|
||||
{
|
||||
return (await result).ThenTry(func);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="Then{TNew}(Task{Result},Func{Result{TNew}})"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static async ValueTask<Result<TNew>> ThenAsync<TNew>(this ValueTask<Result> result, Func<Task<Result<TNew>>> func)
|
||||
{
|
||||
return await (await result).ThenAsync(func);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="Then{TNew}(Task{Result},Func{Result{TNew}})"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static async ValueTask<Result<TNew>> ThenAsync<TNew>(this ValueTask<Result> result, Func<ValueTask<Result<TNew>>> func)
|
||||
{
|
||||
return await (await result).ThenAsync(func);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="Then{TNew}(Task{Result},Func{Result{TNew}})"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static async ValueTask<Result<TNew>> ThenTryAsync<TNew>(this ValueTask<Result> result, Func<Task<Result<TNew>>> func)
|
||||
{
|
||||
return await (await result).ThenTryAsync(func);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="Then{TNew}(Task{Result},Func{Result{TNew}})"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static async ValueTask<Result<TNew>> ThenTryAsync<TNew>(this ValueTask<Result> result, Func<ValueTask<Result<TNew>>> func)
|
||||
{
|
||||
return await (await result).ThenTryAsync(func);
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
BIN
src/request.result/package-icon.png
Normal file
BIN
src/request.result/package-icon.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 15 KiB |
79
src/request.result/package-readme.md
Normal file
79
src/request.result/package-readme.md
Normal file
|
|
@ -0,0 +1,79 @@
|
|||
## Features
|
||||
|
||||
- **Success and Failure States:** Represent successful outcomes with a value (`Prelude.Success()`) or failures with an
|
||||
error (`Prelude.Failure()`).
|
||||
- **Immutability:** `Result<T>` objects are immutable, ensuring thread safety and preventing accidental modification.
|
||||
- **Chaining Operations:** Methods like `Map` and `Then` allow for chaining operations on successful results, promoting
|
||||
a functional programming style.
|
||||
|
||||
## Getting Started
|
||||
|
||||
### Install the NuGet package:
|
||||
|
||||
```shell
|
||||
dotnet add package Geekeey.Request.Result
|
||||
```
|
||||
|
||||
You may need to add our NuGet feed to your nuget.config this can be done by running the following command:
|
||||
|
||||
```shell
|
||||
dotnet nuget add source -n geekeey https://code.geekeey.de/api/packages/geekeey/nuget/index.json
|
||||
```
|
||||
|
||||
### Usage
|
||||
|
||||
**Basic success/failure handling:**
|
||||
|
||||
```csharp
|
||||
public Result<int> Divide(int dividend, int divisor)
|
||||
{
|
||||
return divisor == 0
|
||||
? Prelude.Failure<int>("Division by zero")
|
||||
: Prelude.Success(dividend / divisor);
|
||||
}
|
||||
|
||||
var result = Divide(10, 2);
|
||||
var message = result.IsSuccess
|
||||
? $"Result: {result.Value}"
|
||||
: $"Error: {result.Error}";
|
||||
```
|
||||
|
||||
**Chaining with Map and Then:**
|
||||
|
||||
```csharp
|
||||
var result = Prelude.Success(10)
|
||||
.Map(x => x * 2) // 20
|
||||
.Map(x => x + 5) // 25
|
||||
.Then(x => x > 20
|
||||
? Prelude.Success(x)
|
||||
: Prelude.Failure<int>("Value too small"));
|
||||
```
|
||||
|
||||
**Async operations with error handling:**
|
||||
|
||||
```csharp
|
||||
public async Task<Result<User>> FetchUserAsync(int userId)
|
||||
{
|
||||
return await Prelude.TryAsync(() => _httpClient.GetAsync($"/api/users/{userId}"))
|
||||
.ThenAsync(static async response =>
|
||||
{
|
||||
var json = await response.Content.ReadAsStringAsync();
|
||||
return Prelude.Try(() => JsonSerializer.Deserialize<User>(json)!);
|
||||
});
|
||||
}
|
||||
```
|
||||
|
||||
**Collecting multiple results:**
|
||||
|
||||
```csharp
|
||||
var results = await Task.WhenAll(userIds.Select(FetchUserAsync));
|
||||
var combined = results.Join(); // Result<IReadOnlyList<User>>
|
||||
|
||||
if (combined.IsSuccess)
|
||||
{
|
||||
foreach (var user in combined.Value)
|
||||
{
|
||||
Console.WriteLine($"User: {user.Name}");
|
||||
}
|
||||
}
|
||||
```
|
||||
Loading…
Add table
Add a link
Reference in a new issue