result/src/Result/Prelude.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

95 lines
No EOL
3.3 KiB
C#

// Copyright (c) The Geekeey Authors
// SPDX-License-Identifier: EUPL-1.2
using System.Diagnostics.Contracts;
namespace Geekeey.Extensions.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 an 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) => new(value);
/// <summary>
/// Creates a result containing a failure value.
/// </summary>
/// <typeparam name="T">The type of an 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) => new(error);
/// <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 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 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 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));
}
}
}