feat: add inital in memory dispatcher
All checks were successful
default / dotnet-default-workflow (push) Successful in 1m27s
All checks were successful
default / dotnet-default-workflow (push) Successful in 1m27s
Add a simple in memory dispatcher for scalar requests and stream request.
This commit is contained in:
commit
28c07c5c3f
145 changed files with 6380 additions and 0 deletions
101
src/request.result/Prelude.cs
Normal file
101
src/request.result/Prelude.cs
Normal file
|
|
@ -0,0 +1,101 @@
|
|||
// 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 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 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));
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue