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

92
src/Result/Prelude.cs Normal file
View file

@ -0,0 +1,92 @@
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));
}
}
}