feat: add none generic result type
This adds a none generic result type and refactors the result to be a class. The Result has the same inheritance like `Task` and `Task<T>`
This commit is contained in:
parent
f77c1ff29b
commit
cb2628c615
18 changed files with 2745 additions and 41 deletions
107
src/request.result.tests/ExtensionsTaskTests.cs
Normal file
107
src/request.result.tests/ExtensionsTaskTests.cs
Normal file
|
|
@ -0,0 +1,107 @@
|
||||||
|
// Copyright (c) The Geekeey Authors
|
||||||
|
// SPDX-License-Identifier: EUPL-1.2
|
||||||
|
|
||||||
|
using System.Globalization;
|
||||||
|
|
||||||
|
namespace Geekeey.Request.Result.Tests;
|
||||||
|
|
||||||
|
internal sealed class ExtensionsTaskTests
|
||||||
|
{
|
||||||
|
[Test]
|
||||||
|
public async Task I_can_map_on_task_and_it_returns_success_for_success()
|
||||||
|
{
|
||||||
|
var start = Task.FromResult(Prelude.Success(2));
|
||||||
|
var result = await start.Map(value => value.ToString(CultureInfo.InvariantCulture));
|
||||||
|
|
||||||
|
using var scope = Assert.Multiple();
|
||||||
|
await Assert.That(result.IsSuccess).IsTrue();
|
||||||
|
await Assert.That(result.Value).IsEqualTo("2");
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public async Task I_can_try_map_on_task_and_it_returns_failure_for_success_with_throwing()
|
||||||
|
{
|
||||||
|
var start = Task.FromResult(Prelude.Success(2));
|
||||||
|
var result = await start.TryMap<int, string>(value => throw new CustomTestException());
|
||||||
|
|
||||||
|
using var scope = Assert.Multiple();
|
||||||
|
await Assert.That(result.IsSuccess).IsFalse();
|
||||||
|
var instance = await Assert.That(result.Error).IsTypeOf<ExceptionError>();
|
||||||
|
await Assert.That(instance?.Exception).IsTypeOf<CustomTestException>();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public async Task I_can_map_async_on_task_and_it_returns_success_for_success()
|
||||||
|
{
|
||||||
|
var start = Task.FromResult(Prelude.Success(2));
|
||||||
|
var result = await start.MapAsync(value => Task.FromResult(value.ToString(CultureInfo.InvariantCulture)));
|
||||||
|
|
||||||
|
using var scope = Assert.Multiple();
|
||||||
|
await Assert.That(result.IsSuccess).IsTrue();
|
||||||
|
await Assert.That(result.Value).IsEqualTo("2");
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public async Task I_can_then_on_task_and_it_returns_success_for_success()
|
||||||
|
{
|
||||||
|
var start = Task.FromResult(Prelude.Success(2));
|
||||||
|
var result = await start.Then(value => Prelude.Success(value.ToString(CultureInfo.InvariantCulture)));
|
||||||
|
|
||||||
|
using var scope = Assert.Multiple();
|
||||||
|
await Assert.That(result.IsSuccess).IsTrue();
|
||||||
|
await Assert.That(result.Value).IsEqualTo("2");
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public async Task I_can_then_non_generic_on_task_and_it_returns_success_for_success()
|
||||||
|
{
|
||||||
|
var start = Task.FromResult(Prelude.Success(2));
|
||||||
|
var result = await start.Then(value => Prelude.Success());
|
||||||
|
|
||||||
|
using var scope = Assert.Multiple();
|
||||||
|
await Assert.That(result.IsSuccess).IsTrue();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public async Task I_can_map_on_value_task_and_it_returns_success_for_success()
|
||||||
|
{
|
||||||
|
var start = ValueTask.FromResult(Prelude.Success(2));
|
||||||
|
var result = await start.Map(value => value.ToString(CultureInfo.InvariantCulture));
|
||||||
|
|
||||||
|
using var scope = Assert.Multiple();
|
||||||
|
await Assert.That(result.IsSuccess).IsTrue();
|
||||||
|
await Assert.That(result.Value).IsEqualTo("2");
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public async Task I_can_map_on_task_result_and_it_returns_success_for_success()
|
||||||
|
{
|
||||||
|
var start = Task.FromResult(Prelude.Success());
|
||||||
|
var result = await start.Map(() => "2");
|
||||||
|
|
||||||
|
using var scope = Assert.Multiple();
|
||||||
|
await Assert.That(result.IsSuccess).IsTrue();
|
||||||
|
await Assert.That(result.Value).IsEqualTo("2");
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public async Task I_can_then_on_task_result_and_it_returns_success_for_success()
|
||||||
|
{
|
||||||
|
var start = Task.FromResult(Prelude.Success());
|
||||||
|
var result = await start.Then(Prelude.Success);
|
||||||
|
|
||||||
|
using var scope = Assert.Multiple();
|
||||||
|
await Assert.That(result.IsSuccess).IsTrue();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public async Task I_can_then_generic_on_task_result_and_it_returns_success_for_success()
|
||||||
|
{
|
||||||
|
var start = Task.FromResult(Prelude.Success());
|
||||||
|
var result = await start.Then(() => Prelude.Success("2"));
|
||||||
|
|
||||||
|
using var scope = Assert.Multiple();
|
||||||
|
await Assert.That(result.IsSuccess).IsTrue();
|
||||||
|
await Assert.That(result.Value).IsEqualTo("2");
|
||||||
|
}
|
||||||
|
}
|
||||||
34
src/request.result.tests/IResultFactoryTests.cs
Normal file
34
src/request.result.tests/IResultFactoryTests.cs
Normal file
|
|
@ -0,0 +1,34 @@
|
||||||
|
// Copyright (c) The Geekeey Authors
|
||||||
|
// SPDX-License-Identifier: EUPL-1.2
|
||||||
|
|
||||||
|
namespace Geekeey.Request.Result.Tests;
|
||||||
|
|
||||||
|
public class IResultFactoryTests
|
||||||
|
{
|
||||||
|
[Test]
|
||||||
|
public async Task I_can_assign_result_to_result_factory()
|
||||||
|
{
|
||||||
|
await Assert.That(typeof(IResultFactory<Result>).IsAssignableFrom(typeof(Result))).IsTrue();
|
||||||
|
await Assert.That(typeof(IResultFactory<Result<int>>).IsAssignableFrom(typeof(Result<int>))).IsTrue();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public async Task I_can_create_failure_result_from_error_with_result_factory()
|
||||||
|
{
|
||||||
|
{
|
||||||
|
var error = new StringError("error");
|
||||||
|
var result = CreateFailure<Result>(error);
|
||||||
|
await Assert.That(result.IsFailure).IsTrue();
|
||||||
|
}
|
||||||
|
{
|
||||||
|
var error = new StringError("error");
|
||||||
|
var result = CreateFailure<Result<int>>(error);
|
||||||
|
await Assert.That(result.IsFailure).IsTrue();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static TResult CreateFailure<TResult>(Error error) where TResult : IResultFactory<TResult>
|
||||||
|
{
|
||||||
|
return TResult.Failure(error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -97,4 +97,84 @@ internal sealed class PreludeTests
|
||||||
var instance = await Assert.That(result.Error).IsTypeOf<ExceptionError>();
|
var instance = await Assert.That(result.Error).IsTypeOf<ExceptionError>();
|
||||||
await Assert.That(instance?.Exception).IsTypeOf<CustomTestException>();
|
await Assert.That(instance?.Exception).IsTypeOf<CustomTestException>();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public async Task I_can_create_success_result_using_prelude()
|
||||||
|
{
|
||||||
|
var result = Prelude.Success();
|
||||||
|
|
||||||
|
using var scope = Assert.Multiple();
|
||||||
|
await Assert.That(result.IsSuccess).IsTrue();
|
||||||
|
await Assert.That(result.IsFailure).IsFalse();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public async Task I_can_create_failure_result_using_prelude()
|
||||||
|
{
|
||||||
|
var result = Prelude.Failure("error");
|
||||||
|
|
||||||
|
using var scope = Assert.Multiple();
|
||||||
|
await Assert.That(result.IsSuccess).IsFalse();
|
||||||
|
await Assert.That(result.IsFailure).IsTrue();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public async Task I_can_try_action_with_success_and_get_a_success_result()
|
||||||
|
{
|
||||||
|
var result = Prelude.Try(() => { });
|
||||||
|
|
||||||
|
using var scope = Assert.Multiple();
|
||||||
|
await Assert.That(result.IsSuccess).IsTrue();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public async Task I_can_try_action_with_throwing_exception_and_get_a_failure_result()
|
||||||
|
{
|
||||||
|
var result = Prelude.Try(() => throw new CustomTestException());
|
||||||
|
|
||||||
|
using var scope = Assert.Multiple();
|
||||||
|
await Assert.That(result.IsSuccess).IsFalse();
|
||||||
|
var instance = await Assert.That(result.Error).IsTypeOf<ExceptionError>();
|
||||||
|
await Assert.That(instance?.Exception).IsTypeOf<CustomTestException>();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public async Task I_can_try_async_action_with_success_and_get_a_success_result()
|
||||||
|
{
|
||||||
|
var result = await Prelude.TryAsync(() => Task.CompletedTask);
|
||||||
|
|
||||||
|
using var scope = Assert.Multiple();
|
||||||
|
await Assert.That(result.IsSuccess).IsTrue();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public async Task I_can_try_async_action_with_throwing_exception_and_get_a_failure_result()
|
||||||
|
{
|
||||||
|
var result = await Prelude.TryAsync(Task () => throw new CustomTestException());
|
||||||
|
|
||||||
|
using var scope = Assert.Multiple();
|
||||||
|
await Assert.That(result.IsSuccess).IsFalse();
|
||||||
|
var instance = await Assert.That(result.Error).IsTypeOf<ExceptionError>();
|
||||||
|
await Assert.That(instance?.Exception).IsTypeOf<CustomTestException>();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public async Task I_can_try_async_value_action_with_success_and_get_a_success_result()
|
||||||
|
{
|
||||||
|
var result = await Prelude.TryAsync(() => ValueTask.CompletedTask);
|
||||||
|
|
||||||
|
using var scope = Assert.Multiple();
|
||||||
|
await Assert.That(result.IsSuccess).IsTrue();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public async Task I_can_try_async_value_action_with_throwing_exception_and_get_a_failure_result()
|
||||||
|
{
|
||||||
|
var result = await Prelude.TryAsync(ValueTask () => throw new CustomTestException());
|
||||||
|
|
||||||
|
using var scope = Assert.Multiple();
|
||||||
|
await Assert.That(result.IsSuccess).IsFalse();
|
||||||
|
var instance = await Assert.That(result.Error).IsTypeOf<ExceptionError>();
|
||||||
|
await Assert.That(instance?.Exception).IsTypeOf<CustomTestException>();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -61,4 +61,16 @@ internal sealed class ResultConversionTests
|
||||||
|
|
||||||
await Assert.That(() => (int)result).Throws<UnwrapException>();
|
await Assert.That(() => (int)result).Throws<UnwrapException>();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public async Task I_can_implicitly_convert_from_error_to_non_generic_result()
|
||||||
|
{
|
||||||
|
var error = new CustomTestError();
|
||||||
|
Result result = error;
|
||||||
|
|
||||||
|
using var scope = Assert.Multiple();
|
||||||
|
await Assert.That(result.IsSuccess).IsFalse();
|
||||||
|
await Assert.That(result.IsFailure).IsTrue();
|
||||||
|
await Assert.That(result.Error).IsTypeOf<CustomTestError>();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -172,4 +172,31 @@ internal sealed class ResultEqualityTests
|
||||||
|
|
||||||
await Assert.That(result.GetHashCode()).IsZero();
|
await Assert.That(result.GetHashCode()).IsZero();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public async Task I_can_equal_non_generic_result_and_get_true_for_success_and_success()
|
||||||
|
{
|
||||||
|
var a = Prelude.Success();
|
||||||
|
var b = Prelude.Success();
|
||||||
|
|
||||||
|
await Assert.That(a.Equals(b)).IsTrue();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public async Task I_can_equal_non_generic_result_and_get_true_for_failure_and_failure()
|
||||||
|
{
|
||||||
|
var a = Prelude.Failure("error 1");
|
||||||
|
var b = Prelude.Failure("error 2");
|
||||||
|
|
||||||
|
await Assert.That(a.Equals(b)).IsTrue();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public async Task I_can_equal_non_generic_result_and_get_false_for_success_and_failure()
|
||||||
|
{
|
||||||
|
var a = Prelude.Success();
|
||||||
|
var b = Prelude.Failure("error");
|
||||||
|
|
||||||
|
await Assert.That(a.Equals(b)).IsFalse();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -229,4 +229,137 @@ internal sealed class ResultMatchingTests
|
||||||
return ValueTask.CompletedTask;
|
return ValueTask.CompletedTask;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public async Task I_can_match_non_generic_result_and_it_calls_success_func_for_success()
|
||||||
|
{
|
||||||
|
var result = Prelude.Success();
|
||||||
|
var match = result.Match(
|
||||||
|
() => "success",
|
||||||
|
_ => throw new InvalidOperationException());
|
||||||
|
|
||||||
|
await Assert.That(match).IsEqualTo("success");
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public async Task I_can_match_non_generic_result_and_it_calls_failure_func_for_failure()
|
||||||
|
{
|
||||||
|
var result = Prelude.Failure("error");
|
||||||
|
var match = result.Match(
|
||||||
|
() => throw new InvalidOperationException(),
|
||||||
|
e => e);
|
||||||
|
|
||||||
|
await Assert.That(match.Message).IsEqualTo("error");
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public async Task I_can_switch_non_generic_result_and_it_calls_success_action_for_success()
|
||||||
|
{
|
||||||
|
var called = false;
|
||||||
|
|
||||||
|
var result = Prelude.Success();
|
||||||
|
result.Switch(OnSuccess, OnFailure);
|
||||||
|
|
||||||
|
await Assert.That(called).IsTrue();
|
||||||
|
|
||||||
|
void OnSuccess()
|
||||||
|
{
|
||||||
|
called = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void OnFailure(Error e)
|
||||||
|
{
|
||||||
|
throw new InvalidOperationException();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public async Task I_can_switch_non_generic_result_and_it_calls_failure_action_for_failure()
|
||||||
|
{
|
||||||
|
var called = false;
|
||||||
|
var value = default(Error);
|
||||||
|
|
||||||
|
var result = Prelude.Failure("error");
|
||||||
|
result.Switch(OnSuccess, OnFailure);
|
||||||
|
|
||||||
|
await Assert.That(called).IsTrue();
|
||||||
|
await Assert.That(value?.Message).IsEqualTo("error");
|
||||||
|
|
||||||
|
void OnSuccess()
|
||||||
|
{
|
||||||
|
throw new InvalidOperationException();
|
||||||
|
}
|
||||||
|
|
||||||
|
void OnFailure(Error e)
|
||||||
|
{
|
||||||
|
value = e;
|
||||||
|
called = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public async Task I_can_match_async_non_generic_result_and_it_calls_success_func_for_success()
|
||||||
|
{
|
||||||
|
var result = Prelude.Success();
|
||||||
|
var match = await result.MatchAsync(
|
||||||
|
() => Task.FromResult("success"),
|
||||||
|
_ => throw new InvalidOperationException());
|
||||||
|
|
||||||
|
await Assert.That(match).IsEqualTo("success");
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public async Task I_can_switch_async_non_generic_result_and_it_calls_success_action_for_success()
|
||||||
|
{
|
||||||
|
var called = false;
|
||||||
|
|
||||||
|
var result = Prelude.Success();
|
||||||
|
await result.SwitchAsync(OnSuccess, OnFailure);
|
||||||
|
|
||||||
|
await Assert.That(called).IsTrue();
|
||||||
|
|
||||||
|
Task OnSuccess()
|
||||||
|
{
|
||||||
|
called = true;
|
||||||
|
return Task.CompletedTask;
|
||||||
|
}
|
||||||
|
|
||||||
|
Task OnFailure(Error e)
|
||||||
|
{
|
||||||
|
throw new InvalidOperationException();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public async Task I_can_match_async_non_generic_result_and_it_calls_success_func_for_success_ValueTask()
|
||||||
|
{
|
||||||
|
var result = Prelude.Success();
|
||||||
|
var match = await result.MatchAsync(
|
||||||
|
() => ValueTask.FromResult("success"),
|
||||||
|
_ => throw new InvalidOperationException());
|
||||||
|
|
||||||
|
await Assert.That(match).IsEqualTo("success");
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public async Task I_can_switch_async_non_generic_result_and_it_calls_success_action_for_success_ValueTask()
|
||||||
|
{
|
||||||
|
var called = false;
|
||||||
|
|
||||||
|
var result = Prelude.Success();
|
||||||
|
await result.SwitchAsync(OnSuccess, OnFailure);
|
||||||
|
|
||||||
|
await Assert.That(called).IsTrue();
|
||||||
|
|
||||||
|
ValueTask OnSuccess()
|
||||||
|
{
|
||||||
|
called = true;
|
||||||
|
return ValueTask.CompletedTask;
|
||||||
|
}
|
||||||
|
|
||||||
|
ValueTask OnFailure(Error e)
|
||||||
|
{
|
||||||
|
throw new InvalidOperationException();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
104
src/request.result.tests/ResultOperatorTests.cs
Normal file
104
src/request.result.tests/ResultOperatorTests.cs
Normal file
|
|
@ -0,0 +1,104 @@
|
||||||
|
// Copyright (c) The Geekeey Authors
|
||||||
|
// SPDX-License-Identifier: EUPL-1.2
|
||||||
|
|
||||||
|
using Geekeey.Request.Result;
|
||||||
|
|
||||||
|
namespace Geekeey.Request.Result.Tests;
|
||||||
|
|
||||||
|
internal sealed class ResultOperatorTests
|
||||||
|
{
|
||||||
|
[Test]
|
||||||
|
public async Task I_can_compare_non_generic_results_with_operator_and_get_true_for_success_and_success()
|
||||||
|
{
|
||||||
|
var a = Prelude.Success();
|
||||||
|
var b = Prelude.Success();
|
||||||
|
|
||||||
|
await Assert.That(a == b).IsTrue();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public async Task I_can_compare_non_generic_results_with_operator_and_get_true_for_failure_and_failure()
|
||||||
|
{
|
||||||
|
var a = Prelude.Failure("error 1");
|
||||||
|
var b = Prelude.Failure("error 2");
|
||||||
|
|
||||||
|
await Assert.That(a == b).IsTrue();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public async Task I_can_compare_non_generic_results_with_operator_and_get_false_for_success_and_failure()
|
||||||
|
{
|
||||||
|
var a = Prelude.Success();
|
||||||
|
var b = Prelude.Failure("error");
|
||||||
|
|
||||||
|
await Assert.That(a == b).IsFalse();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public async Task I_can_compare_non_generic_results_with_operator_and_handle_nulls()
|
||||||
|
{
|
||||||
|
Result? a = null;
|
||||||
|
Result? b = null;
|
||||||
|
var c = Prelude.Success();
|
||||||
|
|
||||||
|
await Assert.That(a == b).IsTrue();
|
||||||
|
await Assert.That(a == c).IsFalse();
|
||||||
|
await Assert.That(c == a).IsFalse();
|
||||||
|
await Assert.That(a != b).IsFalse();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public async Task I_can_compare_generic_results_with_operator_and_get_true_for_success_with_equal_value()
|
||||||
|
{
|
||||||
|
var a = Prelude.Success(2);
|
||||||
|
var b = Prelude.Success(2);
|
||||||
|
|
||||||
|
await Assert.That(a == b).IsTrue();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public async Task I_can_compare_generic_results_with_operator_and_get_false_for_success_with_unequal_value()
|
||||||
|
{
|
||||||
|
var a = Prelude.Success(2);
|
||||||
|
var b = Prelude.Success(3);
|
||||||
|
|
||||||
|
await Assert.That(a == b).IsFalse();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public async Task I_can_compare_generic_results_with_operator_and_get_true_for_failure_and_failure()
|
||||||
|
{
|
||||||
|
var a = Prelude.Failure<int>("error 1");
|
||||||
|
var b = Prelude.Failure<int>("error 2");
|
||||||
|
|
||||||
|
await Assert.That(a == b).IsTrue();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public async Task I_can_compare_generic_result_with_value_using_operator_true_when_success_and_equal()
|
||||||
|
{
|
||||||
|
var a = Prelude.Success(2);
|
||||||
|
var b = 2;
|
||||||
|
|
||||||
|
await Assert.That(a == b).IsTrue();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public async Task I_can_compare_generic_result_with_value_using_operator_false_when_failure()
|
||||||
|
{
|
||||||
|
var a = Prelude.Failure<int>("error");
|
||||||
|
var b = 2;
|
||||||
|
|
||||||
|
await Assert.That(a == b).IsFalse();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public async Task I_can_get_hashcode_non_generic_success_and_failure()
|
||||||
|
{
|
||||||
|
var success = Prelude.Success();
|
||||||
|
var failure = Prelude.Failure("error");
|
||||||
|
|
||||||
|
await Assert.That(success.GetHashCode()).IsEqualTo(true.GetHashCode());
|
||||||
|
await Assert.That(failure.GetHashCode()).IsEqualTo(false.GetHashCode());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -29,18 +29,6 @@ internal sealed class ResultTests
|
||||||
await Assert.That(result.Error).IsTypeOf<CustomTestError>();
|
await Assert.That(result.Error).IsTypeOf<CustomTestError>();
|
||||||
}
|
}
|
||||||
|
|
||||||
[Test]
|
|
||||||
public async Task I_can_distinguish_default_result_from_created()
|
|
||||||
{
|
|
||||||
var result = default(Result<int>);
|
|
||||||
|
|
||||||
using var scope = Assert.Multiple();
|
|
||||||
await Assert.That(result.IsSuccess).IsFalse();
|
|
||||||
await Assert.That(result.IsFailure).IsTrue();
|
|
||||||
await Assert.That(result.Value).IsEqualTo(default(int));
|
|
||||||
await Assert.That(result.Error).IsEqualTo(Error.DefaultValueError);
|
|
||||||
}
|
|
||||||
|
|
||||||
[Test]
|
[Test]
|
||||||
public async Task I_can_to_string_success_result_value()
|
public async Task I_can_to_string_success_result_value()
|
||||||
{
|
{
|
||||||
|
|
@ -56,4 +44,53 @@ internal sealed class ResultTests
|
||||||
|
|
||||||
await Assert.That(result.ToString()).IsEqualTo("Failure { error }");
|
await Assert.That(result.ToString()).IsEqualTo("Failure { error }");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public async Task I_can_create_new_success_result()
|
||||||
|
{
|
||||||
|
var result = new Result();
|
||||||
|
|
||||||
|
using var scope = Assert.Multiple();
|
||||||
|
await Assert.That(result.IsSuccess).IsTrue();
|
||||||
|
await Assert.That(result.IsFailure).IsFalse();
|
||||||
|
await Assert.That(result.Error).IsNull();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public async Task I_can_create_new_failure_result()
|
||||||
|
{
|
||||||
|
var result = new Result(new CustomTestError());
|
||||||
|
|
||||||
|
using var scope = Assert.Multiple();
|
||||||
|
await Assert.That(result.IsSuccess).IsFalse();
|
||||||
|
await Assert.That(result.IsFailure).IsTrue();
|
||||||
|
await Assert.That(result.Error).IsTypeOf<CustomTestError>();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public async Task I_can_to_string_success_result()
|
||||||
|
{
|
||||||
|
var result = new Result();
|
||||||
|
|
||||||
|
await Assert.That(result.ToString()).IsEqualTo("Success");
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public async Task I_can_to_string_failure_result()
|
||||||
|
{
|
||||||
|
var result = new Result(new StringError("error"));
|
||||||
|
|
||||||
|
await Assert.That(result.ToString()).IsEqualTo("Failure { error }");
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public async Task I_can_use_generic_result_as_non_generic_result()
|
||||||
|
{
|
||||||
|
Result result = new Result<int>(1);
|
||||||
|
|
||||||
|
using var scope = Assert.Multiple();
|
||||||
|
await Assert.That(result.IsSuccess).IsTrue();
|
||||||
|
await Assert.That(result.IsFailure).IsFalse();
|
||||||
|
await Assert.That(result.Error).IsNull();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -641,4 +641,633 @@ internal sealed class ResultTransformTests
|
||||||
await Assert.That(result.Error).IsTypeOf<StringError>();
|
await Assert.That(result.Error).IsTypeOf<StringError>();
|
||||||
await Assert.That(result.Error?.Message).IsEqualTo("error");
|
await Assert.That(result.Error?.Message).IsEqualTo("error");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public async Task I_can_map_non_generic_result_and_it_returns_success_for_success()
|
||||||
|
{
|
||||||
|
var start = Prelude.Success();
|
||||||
|
var result = start.Map(() => 42);
|
||||||
|
|
||||||
|
using var scope = Assert.Multiple();
|
||||||
|
await Assert.That(result.IsSuccess).IsTrue();
|
||||||
|
await Assert.That(result.Value).IsEqualTo(42);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public async Task I_can_map_non_generic_result_and_it_returns_failure_for_failure()
|
||||||
|
{
|
||||||
|
var start = Prelude.Failure("error");
|
||||||
|
var result = start.Map(() => 42);
|
||||||
|
|
||||||
|
using var scope = Assert.Multiple();
|
||||||
|
await Assert.That(result.IsSuccess).IsFalse();
|
||||||
|
await Assert.That(result.Error?.Message).IsEqualTo("error");
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public async Task I_can_try_map_non_generic_result_and_it_returns_failure_for_exception()
|
||||||
|
{
|
||||||
|
var start = Prelude.Success();
|
||||||
|
var result = start.TryMap<int>(() => throw new CustomTestException());
|
||||||
|
|
||||||
|
using var scope = Assert.Multiple();
|
||||||
|
await Assert.That(result.IsSuccess).IsFalse();
|
||||||
|
var instance = await Assert.That(result.Error).IsTypeOf<ExceptionError>();
|
||||||
|
await Assert.That(instance?.Exception).IsTypeOf<CustomTestException>();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public async Task I_can_transform_non_generic_result_with_then_and_it_returns_success_for_success()
|
||||||
|
{
|
||||||
|
var start = Prelude.Success();
|
||||||
|
var result = start.Then(Prelude.Success);
|
||||||
|
|
||||||
|
using var scope = Assert.Multiple();
|
||||||
|
await Assert.That(result.IsSuccess).IsTrue();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public async Task I_can_transform_non_generic_result_with_then_to_generic_and_it_returns_success_for_success()
|
||||||
|
{
|
||||||
|
var start = Prelude.Success();
|
||||||
|
var result = start.Then(() => Prelude.Success(42));
|
||||||
|
|
||||||
|
using var scope = Assert.Multiple();
|
||||||
|
await Assert.That(result.IsSuccess).IsTrue();
|
||||||
|
await Assert.That(result.Value).IsEqualTo(42);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public async Task I_can_transform_generic_result_with_then_to_non_generic_and_it_returns_success_for_success()
|
||||||
|
{
|
||||||
|
var start = Prelude.Success(42);
|
||||||
|
var result = start.Then(_ => Prelude.Success());
|
||||||
|
|
||||||
|
using var scope = Assert.Multiple();
|
||||||
|
await Assert.That(result.IsSuccess).IsTrue();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public async Task I_can_map_non_generic_result_async_and_it_returns_success_for_success_Task()
|
||||||
|
{
|
||||||
|
var start = Prelude.Success();
|
||||||
|
var result = await start.MapAsync(() => Task.FromResult(42));
|
||||||
|
|
||||||
|
using var scope = Assert.Multiple();
|
||||||
|
await Assert.That(result.IsSuccess).IsTrue();
|
||||||
|
await Assert.That(result.Value).IsEqualTo(42);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public async Task I_can_try_map_non_generic_result_async_and_it_returns_failure_for_exception_Task()
|
||||||
|
{
|
||||||
|
var start = Prelude.Success();
|
||||||
|
var result = await start.TryMapAsync((Func<Task<int>>)(() => throw new CustomTestException()));
|
||||||
|
|
||||||
|
using var scope = Assert.Multiple();
|
||||||
|
await Assert.That(result.IsSuccess).IsFalse();
|
||||||
|
var instance = await Assert.That(result.Error).IsTypeOf<ExceptionError>();
|
||||||
|
await Assert.That(instance?.Exception).IsTypeOf<CustomTestException>();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public async Task I_can_transform_non_generic_result_async_with_then_and_it_returns_success_for_success_Task()
|
||||||
|
{
|
||||||
|
var start = Prelude.Success();
|
||||||
|
var result = await start.ThenAsync(() => Task.FromResult(Prelude.Success()));
|
||||||
|
|
||||||
|
using var scope = Assert.Multiple();
|
||||||
|
await Assert.That(result.IsSuccess).IsTrue();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public async Task I_can_transform_generic_result_async_with_then_to_non_generic_and_it_returns_success_for_success_Task()
|
||||||
|
{
|
||||||
|
var start = Prelude.Success(42);
|
||||||
|
var result = await start.ThenAsync(_ => Task.FromResult(Prelude.Success()));
|
||||||
|
|
||||||
|
using var scope = Assert.Multiple();
|
||||||
|
await Assert.That(result.IsSuccess).IsTrue();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public async Task I_can_map_non_generic_result_async_and_it_returns_success_for_success_ValueTask()
|
||||||
|
{
|
||||||
|
var start = Prelude.Success();
|
||||||
|
var result = await start.MapAsync(() => ValueTask.FromResult(42));
|
||||||
|
|
||||||
|
using var scope = Assert.Multiple();
|
||||||
|
await Assert.That(result.IsSuccess).IsTrue();
|
||||||
|
await Assert.That(result.Value).IsEqualTo(42);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public async Task I_can_try_map_non_generic_result_async_and_it_returns_failure_for_exception_ValueTask()
|
||||||
|
{
|
||||||
|
var start = Prelude.Success();
|
||||||
|
var result = await start.TryMapAsync((Func<ValueTask<int>>)(() => throw new CustomTestException()));
|
||||||
|
|
||||||
|
using var scope = Assert.Multiple();
|
||||||
|
await Assert.That(result.IsSuccess).IsFalse();
|
||||||
|
var instance = await Assert.That(result.Error).IsTypeOf<ExceptionError>();
|
||||||
|
await Assert.That(instance?.Exception).IsTypeOf<CustomTestException>();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public async Task I_can_transform_generic_result_async_with_then_to_non_generic_and_it_returns_success_for_success_ValueTask()
|
||||||
|
{
|
||||||
|
var start = Prelude.Success(42);
|
||||||
|
var result = await start.ThenAsync(_ => ValueTask.FromResult(Prelude.Success()));
|
||||||
|
|
||||||
|
using var scope = Assert.Multiple();
|
||||||
|
await Assert.That(result.IsSuccess).IsTrue();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public async Task I_can_try_transform_result_to_non_generic_and_it_returns_success_for_success()
|
||||||
|
{
|
||||||
|
var start = Prelude.Success(2);
|
||||||
|
var result = start.ThenTry(value => Prelude.Success());
|
||||||
|
|
||||||
|
using var scope = Assert.Multiple();
|
||||||
|
await Assert.That(result.IsSuccess).IsTrue();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public async Task I_can_try_transform_result_to_non_generic_and_it_returns_failure_for_success_and_mapping_returning_failure()
|
||||||
|
{
|
||||||
|
var start = Prelude.Success(2);
|
||||||
|
var result = start.ThenTry(_ => Prelude.Failure("error"));
|
||||||
|
|
||||||
|
using var scope = Assert.Multiple();
|
||||||
|
await Assert.That(result.IsSuccess).IsFalse();
|
||||||
|
await Assert.That(result.Error?.Message).IsEqualTo("error");
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public async Task I_can_try_transform_result_to_non_generic_and_it_returns_failure_for_success_and_mapping_throwing()
|
||||||
|
{
|
||||||
|
var start = Prelude.Success(2);
|
||||||
|
var result = start.ThenTry(_ => throw new CustomTestException());
|
||||||
|
|
||||||
|
using var scope = Assert.Multiple();
|
||||||
|
await Assert.That(result.IsSuccess).IsFalse();
|
||||||
|
var instance = await Assert.That(result.Error).IsTypeOf<ExceptionError>();
|
||||||
|
await Assert.That(instance?.Exception).IsTypeOf<CustomTestException>();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public async Task I_can_try_transform_result_async_to_non_generic_and_it_returns_success_for_success_Task()
|
||||||
|
{
|
||||||
|
var start = Prelude.Success(2);
|
||||||
|
var result = await start.ThenTryAsync(value => Task.FromResult(Prelude.Success()));
|
||||||
|
|
||||||
|
using var scope = Assert.Multiple();
|
||||||
|
await Assert.That(result.IsSuccess).IsTrue();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public async Task I_can_try_transform_result_async_to_non_generic_and_it_returns_failure_for_success_and_mapping_throwing_Task()
|
||||||
|
{
|
||||||
|
var start = Prelude.Success(2);
|
||||||
|
var result = await start.ThenTryAsync(Task<Result> (_) => throw new CustomTestException());
|
||||||
|
|
||||||
|
using var scope = Assert.Multiple();
|
||||||
|
await Assert.That(result.IsSuccess).IsFalse();
|
||||||
|
var instance = await Assert.That(result.Error).IsTypeOf<ExceptionError>();
|
||||||
|
await Assert.That(instance?.Exception).IsTypeOf<CustomTestException>();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public async Task I_can_try_transform_result_async_to_non_generic_and_it_returns_success_for_success_ValueTask()
|
||||||
|
{
|
||||||
|
var start = Prelude.Success(2);
|
||||||
|
var result = await start.ThenTryAsync(value => ValueTask.FromResult(Prelude.Success()));
|
||||||
|
|
||||||
|
using var scope = Assert.Multiple();
|
||||||
|
await Assert.That(result.IsSuccess).IsTrue();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public async Task I_can_try_transform_result_async_to_non_generic_and_it_returns_failure_for_success_and_mapping_throwing_ValueTask()
|
||||||
|
{
|
||||||
|
var start = Prelude.Success(2);
|
||||||
|
var result = await start.ThenTryAsync(ValueTask<Result> (_) => throw new CustomTestException());
|
||||||
|
|
||||||
|
using var scope = Assert.Multiple();
|
||||||
|
await Assert.That(result.IsSuccess).IsFalse();
|
||||||
|
var instance = await Assert.That(result.Error).IsTypeOf<ExceptionError>();
|
||||||
|
await Assert.That(instance?.Exception).IsTypeOf<CustomTestException>();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public async Task I_can_try_transform_non_generic_result_with_then_and_it_returns_success_for_success()
|
||||||
|
{
|
||||||
|
var start = Prelude.Success();
|
||||||
|
var result = start.ThenTry(Prelude.Success);
|
||||||
|
|
||||||
|
using var scope = Assert.Multiple();
|
||||||
|
await Assert.That(result.IsSuccess).IsTrue();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public async Task I_can_try_transform_non_generic_result_with_then_and_it_returns_failure_for_exception()
|
||||||
|
{
|
||||||
|
var start = Prelude.Success();
|
||||||
|
var result = start.ThenTry(() => throw new CustomTestException());
|
||||||
|
|
||||||
|
using var scope = Assert.Multiple();
|
||||||
|
await Assert.That(result.IsSuccess).IsFalse();
|
||||||
|
var instance = await Assert.That(result.Error).IsTypeOf<ExceptionError>();
|
||||||
|
await Assert.That(instance?.Exception).IsTypeOf<CustomTestException>();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public async Task I_can_try_transform_non_generic_result_with_then_to_generic_and_it_returns_success_for_success()
|
||||||
|
{
|
||||||
|
var start = Prelude.Success();
|
||||||
|
var result = start.ThenTry(() => Prelude.Success(42));
|
||||||
|
|
||||||
|
using var scope = Assert.Multiple();
|
||||||
|
await Assert.That(result.IsSuccess).IsTrue();
|
||||||
|
await Assert.That(result.Value).IsEqualTo(42);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public async Task I_can_try_transform_non_generic_result_with_then_to_generic_and_it_returns_failure_for_exception()
|
||||||
|
{
|
||||||
|
var start = Prelude.Success();
|
||||||
|
var result = start.ThenTry<int>(() => throw new CustomTestException());
|
||||||
|
|
||||||
|
using var scope = Assert.Multiple();
|
||||||
|
await Assert.That(result.IsSuccess).IsFalse();
|
||||||
|
var instance = await Assert.That(result.Error).IsTypeOf<ExceptionError>();
|
||||||
|
await Assert.That(instance?.Exception).IsTypeOf<CustomTestException>();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public async Task I_can_map_non_generic_result_async_and_it_returns_failure_for_failure_Task()
|
||||||
|
{
|
||||||
|
var start = Prelude.Failure("error");
|
||||||
|
var result = await start.MapAsync(() => Task.FromResult(42));
|
||||||
|
|
||||||
|
using var scope = Assert.Multiple();
|
||||||
|
await Assert.That(result.IsSuccess).IsFalse();
|
||||||
|
await Assert.That(result.Error?.Message).IsEqualTo("error");
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public async Task I_can_try_map_non_generic_result_async_and_it_returns_success_for_success_Task()
|
||||||
|
{
|
||||||
|
var start = Prelude.Success();
|
||||||
|
var result = await start.TryMapAsync(() => Task.FromResult(42));
|
||||||
|
|
||||||
|
using var scope = Assert.Multiple();
|
||||||
|
await Assert.That(result.IsSuccess).IsTrue();
|
||||||
|
await Assert.That(result.Value).IsEqualTo(42);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public async Task I_can_try_map_non_generic_result_async_and_it_returns_failure_for_failure_Task()
|
||||||
|
{
|
||||||
|
var start = Prelude.Failure("error");
|
||||||
|
var result = await start.TryMapAsync(() => Task.FromResult(42));
|
||||||
|
|
||||||
|
using var scope = Assert.Multiple();
|
||||||
|
await Assert.That(result.IsSuccess).IsFalse();
|
||||||
|
await Assert.That(result.Error?.Message).IsEqualTo("error");
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public async Task I_can_try_map_non_generic_result_async_and_it_returns_failure_for_await_exception_Task()
|
||||||
|
{
|
||||||
|
var start = Prelude.Success();
|
||||||
|
var result = await start.TryMapAsync((Func<Task<int>>)(async () =>
|
||||||
|
{
|
||||||
|
await Task.CompletedTask;
|
||||||
|
throw new CustomTestException();
|
||||||
|
}));
|
||||||
|
|
||||||
|
using var scope = Assert.Multiple();
|
||||||
|
await Assert.That(result.IsSuccess).IsFalse();
|
||||||
|
var instance = await Assert.That(result.Error).IsTypeOf<ExceptionError>();
|
||||||
|
await Assert.That(instance?.Exception).IsTypeOf<CustomTestException>();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public async Task I_can_transform_non_generic_result_async_with_then_and_it_returns_failure_for_failure_Task()
|
||||||
|
{
|
||||||
|
var start = Prelude.Failure("error");
|
||||||
|
var result = await start.ThenAsync(() => Task.FromResult(Prelude.Success()));
|
||||||
|
|
||||||
|
using var scope = Assert.Multiple();
|
||||||
|
await Assert.That(result.IsSuccess).IsFalse();
|
||||||
|
await Assert.That(result.Error?.Message).IsEqualTo("error");
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public async Task I_can_try_transform_non_generic_result_async_with_then_and_it_returns_success_for_success_Task()
|
||||||
|
{
|
||||||
|
var start = Prelude.Success();
|
||||||
|
var result = await start.ThenTryAsync(() => Task.FromResult(Prelude.Success()));
|
||||||
|
|
||||||
|
using var scope = Assert.Multiple();
|
||||||
|
await Assert.That(result.IsSuccess).IsTrue();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public async Task I_can_try_transform_non_generic_result_async_with_then_and_it_returns_failure_for_exception_Task()
|
||||||
|
{
|
||||||
|
var start = Prelude.Success();
|
||||||
|
var result = await start.ThenTryAsync((Func<Task<Result>>)(() => throw new CustomTestException()));
|
||||||
|
|
||||||
|
using var scope = Assert.Multiple();
|
||||||
|
await Assert.That(result.IsSuccess).IsFalse();
|
||||||
|
var instance = await Assert.That(result.Error).IsTypeOf<ExceptionError>();
|
||||||
|
await Assert.That(instance?.Exception).IsTypeOf<CustomTestException>();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public async Task I_can_try_transform_non_generic_result_async_with_then_and_it_returns_failure_for_await_exception_Task()
|
||||||
|
{
|
||||||
|
var start = Prelude.Success();
|
||||||
|
var result = await start.ThenTryAsync((Func<Task<Result>>)(async () =>
|
||||||
|
{
|
||||||
|
await Task.CompletedTask;
|
||||||
|
throw new CustomTestException();
|
||||||
|
}));
|
||||||
|
|
||||||
|
using var scope = Assert.Multiple();
|
||||||
|
await Assert.That(result.IsSuccess).IsFalse();
|
||||||
|
var instance = await Assert.That(result.Error).IsTypeOf<ExceptionError>();
|
||||||
|
await Assert.That(instance?.Exception).IsTypeOf<CustomTestException>();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public async Task I_can_transform_non_generic_result_async_with_then_to_generic_and_it_returns_success_for_success_Task()
|
||||||
|
{
|
||||||
|
var start = Prelude.Success();
|
||||||
|
var result = await start.ThenAsync(() => Task.FromResult(Prelude.Success(42)));
|
||||||
|
|
||||||
|
using var scope = Assert.Multiple();
|
||||||
|
await Assert.That(result.IsSuccess).IsTrue();
|
||||||
|
await Assert.That(result.Value).IsEqualTo(42);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public async Task I_can_transform_non_generic_result_async_with_then_to_generic_and_it_returns_failure_for_failure_Task()
|
||||||
|
{
|
||||||
|
var start = Prelude.Failure("error");
|
||||||
|
var result = await start.ThenAsync(() => Task.FromResult(Prelude.Success(42)));
|
||||||
|
|
||||||
|
using var scope = Assert.Multiple();
|
||||||
|
await Assert.That(result.IsSuccess).IsFalse();
|
||||||
|
await Assert.That(result.Error?.Message).IsEqualTo("error");
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public async Task I_can_try_transform_non_generic_result_async_with_then_to_generic_and_it_returns_success_for_success_Task()
|
||||||
|
{
|
||||||
|
var start = Prelude.Success();
|
||||||
|
var result = await start.ThenTryAsync(() => Task.FromResult(Prelude.Success(42)));
|
||||||
|
|
||||||
|
using var scope = Assert.Multiple();
|
||||||
|
await Assert.That(result.IsSuccess).IsTrue();
|
||||||
|
await Assert.That(result.Value).IsEqualTo(42);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public async Task I_can_try_transform_non_generic_result_async_with_then_to_generic_and_it_returns_failure_for_exception_Task()
|
||||||
|
{
|
||||||
|
var start = Prelude.Success();
|
||||||
|
var result = await start.ThenTryAsync((Func<Task<Result<int>>>)(() => throw new CustomTestException()));
|
||||||
|
|
||||||
|
using var scope = Assert.Multiple();
|
||||||
|
await Assert.That(result.IsSuccess).IsFalse();
|
||||||
|
var instance = await Assert.That(result.Error).IsTypeOf<ExceptionError>();
|
||||||
|
await Assert.That(instance?.Exception).IsTypeOf<CustomTestException>();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public async Task I_can_try_transform_non_generic_result_async_with_then_to_generic_and_it_returns_failure_for_await_exception_Task()
|
||||||
|
{
|
||||||
|
var start = Prelude.Success();
|
||||||
|
var result = await start.ThenTryAsync((Func<Task<Result<int>>>)(async () =>
|
||||||
|
{
|
||||||
|
await Task.CompletedTask;
|
||||||
|
throw new CustomTestException();
|
||||||
|
}));
|
||||||
|
|
||||||
|
using var scope = Assert.Multiple();
|
||||||
|
await Assert.That(result.IsSuccess).IsFalse();
|
||||||
|
var instance = await Assert.That(result.Error).IsTypeOf<ExceptionError>();
|
||||||
|
await Assert.That(instance?.Exception).IsTypeOf<CustomTestException>();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public async Task I_can_transform_generic_result_async_with_then_to_non_generic_and_it_returns_failure_for_failure_ValueTask()
|
||||||
|
{
|
||||||
|
var start = Prelude.Failure<int>("error");
|
||||||
|
var result = await start.ThenAsync(_ => ValueTask.FromResult(Prelude.Success()));
|
||||||
|
|
||||||
|
using var scope = Assert.Multiple();
|
||||||
|
await Assert.That(result.IsSuccess).IsFalse();
|
||||||
|
await Assert.That(result.Error?.Message).IsEqualTo("error");
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public async Task I_can_try_transform_result_async_to_non_generic_and_it_returns_failure_for_success_and_mapping_returning_failure_ValueTask()
|
||||||
|
{
|
||||||
|
var start = Prelude.Success(2);
|
||||||
|
var result = await start.ThenTryAsync(_ => ValueTask.FromResult(Prelude.Failure("error")));
|
||||||
|
|
||||||
|
using var scope = Assert.Multiple();
|
||||||
|
await Assert.That(result.IsSuccess).IsFalse();
|
||||||
|
await Assert.That(result.Error?.Message).IsEqualTo("error");
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public async Task I_can_try_transform_result_async_to_non_generic_and_it_returns_failure_for_success_and_mapping_await_throwing_ValueTask()
|
||||||
|
{
|
||||||
|
var start = Prelude.Success(2);
|
||||||
|
var result = await start.ThenTryAsync(async ValueTask<Result> (_) =>
|
||||||
|
{
|
||||||
|
await ValueTask.CompletedTask;
|
||||||
|
throw new CustomTestException();
|
||||||
|
});
|
||||||
|
|
||||||
|
using var scope = Assert.Multiple();
|
||||||
|
await Assert.That(result.IsSuccess).IsFalse();
|
||||||
|
var instance = await Assert.That(result.Error).IsTypeOf<ExceptionError>();
|
||||||
|
await Assert.That(instance?.Exception).IsTypeOf<CustomTestException>();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public async Task I_can_map_non_generic_result_async_and_it_returns_failure_for_failure_ValueTask()
|
||||||
|
{
|
||||||
|
var start = Prelude.Failure("error");
|
||||||
|
var result = await start.MapAsync(() => ValueTask.FromResult(42));
|
||||||
|
|
||||||
|
using var scope = Assert.Multiple();
|
||||||
|
await Assert.That(result.IsSuccess).IsFalse();
|
||||||
|
await Assert.That(result.Error?.Message).IsEqualTo("error");
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public async Task I_can_try_map_non_generic_result_async_and_it_returns_success_for_success_ValueTask()
|
||||||
|
{
|
||||||
|
var start = Prelude.Success();
|
||||||
|
var result = await start.TryMapAsync(() => ValueTask.FromResult(42));
|
||||||
|
|
||||||
|
using var scope = Assert.Multiple();
|
||||||
|
await Assert.That(result.IsSuccess).IsTrue();
|
||||||
|
await Assert.That(result.Value).IsEqualTo(42);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public async Task I_can_try_map_non_generic_result_async_and_it_returns_failure_for_failure_ValueTask()
|
||||||
|
{
|
||||||
|
var start = Prelude.Failure("error");
|
||||||
|
var result = await start.TryMapAsync(() => ValueTask.FromResult(42));
|
||||||
|
|
||||||
|
using var scope = Assert.Multiple();
|
||||||
|
await Assert.That(result.IsSuccess).IsFalse();
|
||||||
|
await Assert.That(result.Error?.Message).IsEqualTo("error");
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public async Task I_can_try_map_non_generic_result_async_and_it_returns_failure_for_await_exception_ValueTask()
|
||||||
|
{
|
||||||
|
var start = Prelude.Success();
|
||||||
|
var result = await start.TryMapAsync((Func<ValueTask<int>>)(async () =>
|
||||||
|
{
|
||||||
|
await ValueTask.CompletedTask;
|
||||||
|
throw new CustomTestException();
|
||||||
|
}));
|
||||||
|
|
||||||
|
using var scope = Assert.Multiple();
|
||||||
|
await Assert.That(result.IsSuccess).IsFalse();
|
||||||
|
var instance = await Assert.That(result.Error).IsTypeOf<ExceptionError>();
|
||||||
|
await Assert.That(instance?.Exception).IsTypeOf<CustomTestException>();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public async Task I_can_transform_non_generic_result_async_with_then_and_it_returns_success_for_success_ValueTask()
|
||||||
|
{
|
||||||
|
var start = Prelude.Success();
|
||||||
|
var result = await start.ThenAsync(() => ValueTask.FromResult(Prelude.Success()));
|
||||||
|
|
||||||
|
using var scope = Assert.Multiple();
|
||||||
|
await Assert.That(result.IsSuccess).IsTrue();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public async Task I_can_transform_non_generic_result_async_with_then_and_it_returns_failure_for_failure_ValueTask()
|
||||||
|
{
|
||||||
|
var start = Prelude.Failure("error");
|
||||||
|
var result = await start.ThenAsync(() => ValueTask.FromResult(Prelude.Success()));
|
||||||
|
|
||||||
|
using var scope = Assert.Multiple();
|
||||||
|
await Assert.That(result.IsSuccess).IsFalse();
|
||||||
|
await Assert.That(result.Error?.Message).IsEqualTo("error");
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public async Task I_can_try_transform_non_generic_result_async_with_then_and_it_returns_success_for_success_ValueTask()
|
||||||
|
{
|
||||||
|
var start = Prelude.Success();
|
||||||
|
var result = await start.ThenTryAsync(() => ValueTask.FromResult(Prelude.Success()));
|
||||||
|
|
||||||
|
using var scope = Assert.Multiple();
|
||||||
|
await Assert.That(result.IsSuccess).IsTrue();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public async Task I_can_try_transform_non_generic_result_async_with_then_and_it_returns_failure_for_exception_ValueTask()
|
||||||
|
{
|
||||||
|
var start = Prelude.Success();
|
||||||
|
var result = await start.ThenTryAsync((Func<ValueTask<Result>>)(() => throw new CustomTestException()));
|
||||||
|
|
||||||
|
using var scope = Assert.Multiple();
|
||||||
|
await Assert.That(result.IsSuccess).IsFalse();
|
||||||
|
var instance = await Assert.That(result.Error).IsTypeOf<ExceptionError>();
|
||||||
|
await Assert.That(instance?.Exception).IsTypeOf<CustomTestException>();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public async Task I_can_try_transform_non_generic_result_async_with_then_and_it_returns_failure_for_await_exception_ValueTask()
|
||||||
|
{
|
||||||
|
var start = Prelude.Success();
|
||||||
|
var result = await start.ThenTryAsync((Func<ValueTask<Result>>)(async () =>
|
||||||
|
{
|
||||||
|
await ValueTask.CompletedTask;
|
||||||
|
throw new CustomTestException();
|
||||||
|
}));
|
||||||
|
|
||||||
|
using var scope = Assert.Multiple();
|
||||||
|
await Assert.That(result.IsSuccess).IsFalse();
|
||||||
|
var instance = await Assert.That(result.Error).IsTypeOf<ExceptionError>();
|
||||||
|
await Assert.That(instance?.Exception).IsTypeOf<CustomTestException>();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public async Task I_can_transform_non_generic_result_async_with_then_to_generic_and_it_returns_success_for_success_ValueTask()
|
||||||
|
{
|
||||||
|
var start = Prelude.Success();
|
||||||
|
var result = await start.ThenAsync(() => ValueTask.FromResult(Prelude.Success(42)));
|
||||||
|
|
||||||
|
using var scope = Assert.Multiple();
|
||||||
|
await Assert.That(result.IsSuccess).IsTrue();
|
||||||
|
await Assert.That(result.Value).IsEqualTo(42);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public async Task I_can_transform_non_generic_result_async_with_then_to_generic_and_it_returns_failure_for_failure_ValueTask()
|
||||||
|
{
|
||||||
|
var start = Prelude.Failure("error");
|
||||||
|
var result = await start.ThenAsync(() => ValueTask.FromResult(Prelude.Success(42)));
|
||||||
|
|
||||||
|
using var scope = Assert.Multiple();
|
||||||
|
await Assert.That(result.IsSuccess).IsFalse();
|
||||||
|
await Assert.That(result.Error?.Message).IsEqualTo("error");
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public async Task I_can_try_transform_non_generic_result_async_with_then_to_generic_and_it_returns_success_for_success_ValueTask()
|
||||||
|
{
|
||||||
|
var start = Prelude.Success();
|
||||||
|
var result = await start.ThenTryAsync(() => ValueTask.FromResult(Prelude.Success(42)));
|
||||||
|
|
||||||
|
using var scope = Assert.Multiple();
|
||||||
|
await Assert.That(result.IsSuccess).IsTrue();
|
||||||
|
await Assert.That(result.Value).IsEqualTo(42);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public async Task I_can_try_transform_non_generic_result_async_with_then_to_generic_and_it_returns_failure_for_exception_ValueTask()
|
||||||
|
{
|
||||||
|
var start = Prelude.Success();
|
||||||
|
var result = await start.ThenTryAsync((Func<ValueTask<Result<int>>>)(() => throw new CustomTestException()));
|
||||||
|
|
||||||
|
using var scope = Assert.Multiple();
|
||||||
|
await Assert.That(result.IsSuccess).IsFalse();
|
||||||
|
var instance = await Assert.That(result.Error).IsTypeOf<ExceptionError>();
|
||||||
|
await Assert.That(instance?.Exception).IsTypeOf<CustomTestException>();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public async Task I_can_try_transform_non_generic_result_async_with_then_to_generic_and_it_returns_failure_for_await_exception_ValueTask()
|
||||||
|
{
|
||||||
|
var start = Prelude.Success();
|
||||||
|
var result = await start.ThenTryAsync((Func<ValueTask<Result<int>>>)(async () =>
|
||||||
|
{
|
||||||
|
await ValueTask.CompletedTask;
|
||||||
|
throw new CustomTestException();
|
||||||
|
}));
|
||||||
|
|
||||||
|
using var scope = Assert.Multiple();
|
||||||
|
await Assert.That(result.IsSuccess).IsFalse();
|
||||||
|
var instance = await Assert.That(result.Error).IsTypeOf<ExceptionError>();
|
||||||
|
await Assert.That(instance?.Exception).IsTypeOf<CustomTestException>();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
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);
|
||||||
|
}
|
||||||
|
|
@ -14,6 +14,25 @@ namespace Geekeey.Request.Result;
|
||||||
/// </remarks>
|
/// </remarks>
|
||||||
public static class Prelude
|
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>
|
/// <summary>
|
||||||
/// Creates a result containing a success value.
|
/// Creates a result containing a success value.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|
@ -36,6 +55,69 @@ public static class Prelude
|
||||||
return new Result<T>(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>
|
/// <summary>
|
||||||
/// Tries to execute a function and return the result. If the function throws an exception, the exception will be
|
/// 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"/>.
|
/// returned wrapped in an <see cref="ExceptionError"/>.
|
||||||
|
|
|
||||||
|
|
@ -5,7 +5,20 @@ using System.Diagnostics.Contracts;
|
||||||
|
|
||||||
namespace Geekeey.Request.Result;
|
namespace Geekeey.Request.Result;
|
||||||
|
|
||||||
public readonly partial struct Result<T>
|
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>
|
/// <summary>
|
||||||
/// Implicitly constructs a result from a success value.
|
/// Implicitly constructs a result from a success value.
|
||||||
|
|
|
||||||
|
|
@ -7,7 +7,73 @@ using System.Numerics;
|
||||||
|
|
||||||
namespace Geekeey.Request.Result;
|
namespace Geekeey.Request.Result;
|
||||||
|
|
||||||
public readonly partial struct Result<T> : IEquatable<Result<T>>, IEquatable<T>
|
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>
|
/// <summary>
|
||||||
/// Checks whether the result is equal to another result. Results are equal if both results are success values and
|
/// Checks whether the result is equal to another result. Results are equal if both results are success values and
|
||||||
|
|
@ -15,7 +81,7 @@ public readonly partial struct Result<T> : IEquatable<Result<T>>, IEquatable<T>
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="other">The result to check for equality with the current result.</param>
|
/// <param name="other">The result to check for equality with the current result.</param>
|
||||||
[Pure]
|
[Pure]
|
||||||
public bool Equals(Result<T> other)
|
public bool Equals(Result<T>? other)
|
||||||
{
|
{
|
||||||
return Equals(this, other, EqualityComparer<T>.Default);
|
return Equals(this, other, EqualityComparer<T>.Default);
|
||||||
}
|
}
|
||||||
|
|
@ -68,8 +134,13 @@ public readonly partial struct Result<T> : IEquatable<Result<T>>, IEquatable<T>
|
||||||
return GetHashCode(this, EqualityComparer<T>.Default);
|
return GetHashCode(this, EqualityComparer<T>.Default);
|
||||||
}
|
}
|
||||||
|
|
||||||
internal static bool Equals(Result<T> a, Result<T> b, IEqualityComparer<T> comparer)
|
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)
|
if (!a.IsSuccess || !b.IsSuccess)
|
||||||
{
|
{
|
||||||
return !a.IsSuccess && !b.IsSuccess;
|
return !a.IsSuccess && !b.IsSuccess;
|
||||||
|
|
@ -83,8 +154,13 @@ public readonly partial struct Result<T> : IEquatable<Result<T>>, IEquatable<T>
|
||||||
return comparer.Equals(a.Value, b.Value);
|
return comparer.Equals(a.Value, b.Value);
|
||||||
}
|
}
|
||||||
|
|
||||||
internal static bool Equals(Result<T> a, T? b, IEqualityComparer<T> comparer)
|
internal static bool Equals(Result<T>? a, T? b, IEqualityComparer<T> comparer)
|
||||||
{
|
{
|
||||||
|
if (a is null)
|
||||||
|
{
|
||||||
|
return b is null;
|
||||||
|
}
|
||||||
|
|
||||||
if (!a.IsSuccess)
|
if (!a.IsSuccess)
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
|
|
@ -109,7 +185,7 @@ public readonly partial struct Result<T> : IEquatable<Result<T>>, IEquatable<T>
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public readonly partial struct Result<T> : IEqualityOperators<Result<T>, Result<T>, bool>, IEqualityOperators<Result<T>, T, bool>
|
public partial class Result<T> : IEqualityOperators<Result<T>, Result<T>, bool>, IEqualityOperators<Result<T>, T, bool>
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Checks whether two results are equal. Results are equal if both results are success values and the success
|
/// Checks whether two results are equal. Results are equal if both results are success values and the success
|
||||||
|
|
@ -119,7 +195,7 @@ public readonly partial struct Result<T> : IEqualityOperators<Result<T>, Result<
|
||||||
/// <param name="b">The second result to compare.</param>
|
/// <param name="b">The second result to compare.</param>
|
||||||
[Pure]
|
[Pure]
|
||||||
[ExcludeFromCodeCoverage]
|
[ExcludeFromCodeCoverage]
|
||||||
public static bool operator ==(Result<T> a, Result<T> b)
|
public static bool operator ==(Result<T>? a, Result<T>? b)
|
||||||
{
|
{
|
||||||
return Equals(a, b, EqualityComparer<T>.Default);
|
return Equals(a, b, EqualityComparer<T>.Default);
|
||||||
}
|
}
|
||||||
|
|
@ -132,7 +208,7 @@ public readonly partial struct Result<T> : IEqualityOperators<Result<T>, Result<
|
||||||
/// <param name="b">The second result to compare.</param>
|
/// <param name="b">The second result to compare.</param>
|
||||||
[Pure]
|
[Pure]
|
||||||
[ExcludeFromCodeCoverage]
|
[ExcludeFromCodeCoverage]
|
||||||
public static bool operator !=(Result<T> a, Result<T> b)
|
public static bool operator !=(Result<T>? a, Result<T>? b)
|
||||||
{
|
{
|
||||||
return !Equals(a, b, EqualityComparer<T>.Default);
|
return !Equals(a, b, EqualityComparer<T>.Default);
|
||||||
}
|
}
|
||||||
|
|
@ -144,7 +220,7 @@ public readonly partial struct Result<T> : IEqualityOperators<Result<T>, Result<
|
||||||
/// <param name="b">The value to check for equality with the success value in the result.</param>
|
/// <param name="b">The value to check for equality with the success value in the result.</param>
|
||||||
[Pure]
|
[Pure]
|
||||||
[ExcludeFromCodeCoverage]
|
[ExcludeFromCodeCoverage]
|
||||||
public static bool operator ==(Result<T> a, T? b)
|
public static bool operator ==(Result<T>? a, T? b)
|
||||||
{
|
{
|
||||||
return Equals(a, b, EqualityComparer<T>.Default);
|
return Equals(a, b, EqualityComparer<T>.Default);
|
||||||
}
|
}
|
||||||
|
|
@ -156,7 +232,7 @@ public readonly partial struct Result<T> : IEqualityOperators<Result<T>, Result<
|
||||||
/// <param name="b">The value to check for inequality with the success value in the result.</param>
|
/// <param name="b">The value to check for inequality with the success value in the result.</param>
|
||||||
[Pure]
|
[Pure]
|
||||||
[ExcludeFromCodeCoverage]
|
[ExcludeFromCodeCoverage]
|
||||||
public static bool operator !=(Result<T> a, T? b)
|
public static bool operator !=(Result<T>? a, T? b)
|
||||||
{
|
{
|
||||||
return !Equals(a, b, EqualityComparer<T>.Default);
|
return !Equals(a, b, EqualityComparer<T>.Default);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -5,7 +5,102 @@ using System.Diagnostics.Contracts;
|
||||||
|
|
||||||
namespace Geekeey.Request.Result;
|
namespace Geekeey.Request.Result;
|
||||||
|
|
||||||
public readonly partial struct Result<T>
|
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>
|
/// <summary>
|
||||||
/// Matches over the success value or failure value of the result and returns another value. Can be conceptualized
|
/// Matches over the success value or failure value of the result and returns another value. Can be conceptualized
|
||||||
|
|
@ -42,7 +137,7 @@ public readonly partial struct Result<T>
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public readonly partial struct Result<T>
|
public partial class Result<T>
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Asynchronously matches over the success value or failure value of the result and returns another value. Can be
|
/// Asynchronously matches over the success value or failure value of the result and returns another value. Can be
|
||||||
|
|
@ -73,7 +168,7 @@ public readonly partial struct Result<T>
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public readonly partial struct Result<T>
|
public partial class Result<T>
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Asynchronously matches over the success value or failure value of the result and returns another value. Can be
|
/// Asynchronously matches over the success value or failure value of the result and returns another value. Can be
|
||||||
|
|
|
||||||
|
|
@ -5,7 +5,7 @@ using System.Diagnostics.Contracts;
|
||||||
|
|
||||||
namespace Geekeey.Request.Result;
|
namespace Geekeey.Request.Result;
|
||||||
|
|
||||||
public readonly partial struct Result<T>
|
public partial class Result<T>
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Maps the success value of the result using a mapping function, or does nothing if the result is a failure.
|
/// Maps the success value of the result using a mapping function, or does nothing if the result is a failure.
|
||||||
|
|
@ -79,7 +79,7 @@ public readonly partial struct Result<T>
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public readonly partial struct Result<T>
|
public partial class Result<T>
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Maps the success value of the result using an asynchronous mapping function, or does nothing if the result is
|
/// Maps the success value of the result using an asynchronous mapping function, or does nothing if the result is
|
||||||
|
|
@ -222,7 +222,7 @@ public readonly partial struct Result<T>
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public readonly partial struct Result<T>
|
public partial class Result<T>
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Maps the success value of the result using an asynchronous mapping function, or does nothing if the result is
|
/// Maps the success value of the result using an asynchronous mapping function, or does nothing if the result is
|
||||||
|
|
@ -364,3 +364,696 @@ public readonly partial struct Result<T>
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public partial class Result<T>
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Chains a non-generic result to this result if it is a success, or does nothing if the result is a failure.
|
||||||
|
/// </summary>
|
||||||
|
/// <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>
|
||||||
|
[Pure]
|
||||||
|
public Result Then(Func<T, Result> func)
|
||||||
|
{
|
||||||
|
return IsSuccess ? func(Value) : new Result(Error);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Tries to chain a non-generic result to this result if it is a success, or does nothing if the result is a
|
||||||
|
/// failure. If the function throws an exception, the exception will be returned wrapped in an
|
||||||
|
/// <see cref="ExceptionError"/>.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="func">The function used to create the next result.</param>
|
||||||
|
/// <returns>A result which is either the mapped result, the exception thrown by <paramref name="func"/> wrapped in
|
||||||
|
/// an <see cref="ExceptionError"/>, or a new result containing the failure value of the original result.</returns>
|
||||||
|
[Pure]
|
||||||
|
public Result ThenTry(Func<T, Result> func)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
return Then(func);
|
||||||
|
}
|
||||||
|
catch (Exception exception)
|
||||||
|
{
|
||||||
|
return new Result(new ExceptionError(exception));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public partial class Result<T>
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Chains a non-generic result to this result using an asynchronous mapping function, or does nothing if the result is
|
||||||
|
/// a failure.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="func">The function used to create the next result.</param>
|
||||||
|
/// <returns>A <see cref="Task{T}"/> which either completes asynchronously by invoking the mapping function, or
|
||||||
|
/// completes synchronously by returning a new result containing the failure value of the original result.</returns>
|
||||||
|
[Pure]
|
||||||
|
public Task<Result> ThenAsync(Func<T, Task<Result>> func)
|
||||||
|
{
|
||||||
|
if (!IsSuccess)
|
||||||
|
{
|
||||||
|
return Task.FromResult(new Result(Error));
|
||||||
|
}
|
||||||
|
|
||||||
|
var task = func(Value);
|
||||||
|
return CreateResult(task);
|
||||||
|
|
||||||
|
static async Task<Result> CreateResult(Task<Result> task)
|
||||||
|
{
|
||||||
|
var result = await task;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Tries to chain a non-generic result to this result using an asynchronous mapping function, or does nothing if the
|
||||||
|
/// result is a failure. If the mapping function throws an exception, the exception will be returned wrapped in
|
||||||
|
/// an <see cref="ExceptionError"/>.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="func">The function used to create the next result.</param>
|
||||||
|
/// <returns>A <see cref="Task{T}"/> which either completes asynchronously by invoking the mapping function,
|
||||||
|
/// returning any exception thrown by <paramref name="func"/> wrapped in an <see cref="ExceptionError"/>, or
|
||||||
|
/// completes synchronously by returning a new result containing the failure value of the original result.</returns>
|
||||||
|
[Pure]
|
||||||
|
public Task<Result> ThenTryAsync(Func<T, Task<Result>> func)
|
||||||
|
{
|
||||||
|
if (!IsSuccess)
|
||||||
|
{
|
||||||
|
return Task.FromResult(new Result(Error));
|
||||||
|
}
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var task = func(Value);
|
||||||
|
return CreateResult(task);
|
||||||
|
}
|
||||||
|
catch (Exception exception)
|
||||||
|
{
|
||||||
|
return Task.FromResult(new Result(new ExceptionError(exception)));
|
||||||
|
}
|
||||||
|
|
||||||
|
static async Task<Result> CreateResult(Task<Result> task)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var value = await task;
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
catch (Exception exception)
|
||||||
|
{
|
||||||
|
return new Result(new ExceptionError(exception));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public partial class Result<T>
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Chains a non-generic result to this result using an asynchronous mapping function, or does nothing if the result is
|
||||||
|
/// a failure.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="func">The function used to create the next result.</param>
|
||||||
|
/// <returns>A <see cref="ValueTask{T}"/> which either completes asynchronously by invoking the mapping function, or
|
||||||
|
/// completes synchronously by returning a new result containing the failure value of the original result.</returns>
|
||||||
|
[Pure]
|
||||||
|
public ValueTask<Result> ThenAsync(Func<T, ValueTask<Result>> func)
|
||||||
|
{
|
||||||
|
if (!IsSuccess)
|
||||||
|
{
|
||||||
|
return ValueTask.FromResult(new Result(Error));
|
||||||
|
}
|
||||||
|
|
||||||
|
var task = func(Value);
|
||||||
|
return CreateResult(task);
|
||||||
|
|
||||||
|
static async ValueTask<Result> CreateResult(ValueTask<Result> task)
|
||||||
|
{
|
||||||
|
var result = await task;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Tries to chain a non-generic result to this result using an asynchronous mapping function, or does nothing if the
|
||||||
|
/// result is a failure. If the mapping function throws an exception, the exception will be returned wrapped in
|
||||||
|
/// an <see cref="ExceptionError"/>.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="func">The function used to create the next result.</param>
|
||||||
|
/// <returns>A <see cref="ValueTask{T}"/> which either completes asynchronously by invoking the mapping function,
|
||||||
|
/// returning any exception thrown by <paramref name="func"/> wrapped in an <see cref="ExceptionError"/>, or
|
||||||
|
/// completes synchronously by returning a new result containing the failure value of the original result.</returns>
|
||||||
|
[Pure]
|
||||||
|
public ValueTask<Result> ThenTryAsync(Func<T, ValueTask<Result>> func)
|
||||||
|
{
|
||||||
|
if (!IsSuccess)
|
||||||
|
{
|
||||||
|
return ValueTask.FromResult(new Result(Error));
|
||||||
|
}
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var task = func(Value);
|
||||||
|
return CreateResult(task);
|
||||||
|
}
|
||||||
|
catch (Exception exception)
|
||||||
|
{
|
||||||
|
return ValueTask.FromResult(new Result(new ExceptionError(exception)));
|
||||||
|
}
|
||||||
|
|
||||||
|
static async ValueTask<Result> CreateResult(ValueTask<Result> task)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var value = await task;
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
catch (Exception exception)
|
||||||
|
{
|
||||||
|
return new Result(new ExceptionError(exception));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public partial class Result
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Maps the success of the result to a new success value using a mapping function, or does nothing if the result is
|
||||||
|
/// a failure.
|
||||||
|
/// </summary>
|
||||||
|
/// <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>
|
||||||
|
[Pure]
|
||||||
|
public Result<TNew> Map<TNew>(Func<TNew> func)
|
||||||
|
{
|
||||||
|
return IsSuccess ? new Result<TNew>(func()) : new Result<TNew>(Error);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Tries to map the success of the result to a new success value using a mapping function, or does nothing if the
|
||||||
|
/// result is a failure. If the mapping function throws an exception, the exception will be returned wrapped in an
|
||||||
|
/// <see cref="ExceptionError"/>.
|
||||||
|
/// </summary>
|
||||||
|
/// <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 value, the exception thrown by <paramref name="func"/>
|
||||||
|
/// wrapped in an <see cref="ExceptionError"/>, or the failure value of the original result.</returns>
|
||||||
|
[Pure]
|
||||||
|
public Result<TNew> TryMap<TNew>(Func<TNew> func)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
return Map(func);
|
||||||
|
}
|
||||||
|
catch (Exception exception)
|
||||||
|
{
|
||||||
|
return new Result<TNew>(new ExceptionError(exception));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Chains another result to this result if it is a success, or does nothing if the result is a failure.
|
||||||
|
/// </summary>
|
||||||
|
/// <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>
|
||||||
|
[Pure]
|
||||||
|
public Result Then(Func<Result> func)
|
||||||
|
{
|
||||||
|
return IsSuccess ? func() : new Result(Error);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Tries to chain another result to this result if it is a success, or does nothing if the result is a failure. If
|
||||||
|
/// the function throws an exception, the exception will be returned wrapped in an <see cref="ExceptionError"/>.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="func">The function used to create the next result.</param>
|
||||||
|
/// <returns>A result which is either the mapped result, the exception thrown by <paramref name="func"/> wrapped in
|
||||||
|
/// an <see cref="ExceptionError"/>, or a new result containing the failure value of the original result.</returns>
|
||||||
|
[Pure]
|
||||||
|
public Result ThenTry(Func<Result> func)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
return Then(func);
|
||||||
|
}
|
||||||
|
catch (Exception exception)
|
||||||
|
{
|
||||||
|
return new Result(new ExceptionError(exception));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Chains a generic result to this result if it is a success, or does nothing if the result is a failure.
|
||||||
|
/// </summary>
|
||||||
|
/// <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>
|
||||||
|
[Pure]
|
||||||
|
public Result<TNew> Then<TNew>(Func<Result<TNew>> func)
|
||||||
|
{
|
||||||
|
return IsSuccess ? func() : new Result<TNew>(Error);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Tries to chain a generic result to this result if it is a success, or does nothing if the result is a failure. If
|
||||||
|
/// the function throws an exception, the exception will be returned wrapped in an <see cref="ExceptionError"/>.
|
||||||
|
/// </summary>
|
||||||
|
/// <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, the exception thrown by <paramref name="func"/> wrapped in
|
||||||
|
/// an <see cref="ExceptionError"/>, or a new result containing the failure value of the original result.</returns>
|
||||||
|
[Pure]
|
||||||
|
public Result<TNew> ThenTry<TNew>(Func<Result<TNew>> func)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
return Then(func);
|
||||||
|
}
|
||||||
|
catch (Exception exception)
|
||||||
|
{
|
||||||
|
return new Result<TNew>(new ExceptionError(exception));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public partial class Result
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Maps the success of the result using an asynchronous mapping function, or does nothing if the result is
|
||||||
|
/// a failure.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="func">The function used to map the success.</param>
|
||||||
|
/// <typeparam name="TNew">The type of the new value.</typeparam>
|
||||||
|
/// <returns>A <see cref="Task{T}"/> which either completes asynchronously by invoking the mapping function and
|
||||||
|
/// constructing a new result containing the mapped value, or completes synchronously by returning a new result
|
||||||
|
/// containing the failure value of the original result.</returns>
|
||||||
|
[Pure]
|
||||||
|
public Task<Result<TNew>> MapAsync<TNew>(Func<Task<TNew>> func)
|
||||||
|
{
|
||||||
|
if (!IsSuccess)
|
||||||
|
{
|
||||||
|
return Task.FromResult(new Result<TNew>(Error));
|
||||||
|
}
|
||||||
|
|
||||||
|
var task = func();
|
||||||
|
return CreateResult(task);
|
||||||
|
|
||||||
|
static async Task<Result<TNew>> CreateResult(Task<TNew> task)
|
||||||
|
{
|
||||||
|
var value = await task;
|
||||||
|
return new Result<TNew>(value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Maps the success of the result using an asynchronous mapping function, or does nothing if the result is a
|
||||||
|
/// failure. If the mapping function throws an exception, the exception will be returned wrapped in an
|
||||||
|
/// <see cref="ExceptionError"/>.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="func">The function used to map the success.</param>
|
||||||
|
/// <typeparam name="TNew">The type of the new value.</typeparam>
|
||||||
|
/// <returns>A <see cref="Task{T}"/> which either completes asynchronously by invoking the mapping function and
|
||||||
|
/// constructing a new result containing the mapped value, returning any exception thrown by <paramref name="func"/>
|
||||||
|
/// wrapped in an <see cref="ExceptionError"/> or completes synchronously by returning a new result containing the
|
||||||
|
/// failure value of the original result.</returns>
|
||||||
|
[Pure]
|
||||||
|
public Task<Result<TNew>> TryMapAsync<TNew>(Func<Task<TNew>> func)
|
||||||
|
{
|
||||||
|
if (!IsSuccess)
|
||||||
|
{
|
||||||
|
return Task.FromResult(new Result<TNew>(Error));
|
||||||
|
}
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var task = func();
|
||||||
|
return CreateResult(task);
|
||||||
|
}
|
||||||
|
catch (Exception exception)
|
||||||
|
{
|
||||||
|
return Task.FromResult(new Result<TNew>(new ExceptionError(exception)));
|
||||||
|
}
|
||||||
|
|
||||||
|
static async Task<Result<TNew>> CreateResult(Task<TNew> task)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var value = await task;
|
||||||
|
return new Result<TNew>(value);
|
||||||
|
}
|
||||||
|
catch (Exception exception)
|
||||||
|
{
|
||||||
|
return new Result<TNew>(new ExceptionError(exception));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Chains another result to this result using an asynchronous mapping function, or does nothing if the result is
|
||||||
|
/// a failure.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="func">The function used to create the next result.</param>
|
||||||
|
/// <returns>A <see cref="Task{T}"/> which either completes asynchronously by invoking the mapping function, or
|
||||||
|
/// completes synchronously by returning a new result containing the failure value of the original result.</returns>
|
||||||
|
[Pure]
|
||||||
|
public Task<Result> ThenAsync(Func<Task<Result>> func)
|
||||||
|
{
|
||||||
|
if (!IsSuccess)
|
||||||
|
{
|
||||||
|
return Task.FromResult(new Result(Error));
|
||||||
|
}
|
||||||
|
|
||||||
|
var task = func();
|
||||||
|
return CreateResult(task);
|
||||||
|
|
||||||
|
static async Task<Result> CreateResult(Task<Result> task)
|
||||||
|
{
|
||||||
|
var result = await task;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Tries to chain another result to this result using an asynchronous mapping function, or does nothing if the
|
||||||
|
/// result is a failure. If the mapping function throws an exception, the exception will be returned wrapped in
|
||||||
|
/// an <see cref="ExceptionError"/>.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="func">The function used to create the next result.</param>
|
||||||
|
/// <returns>A <see cref="Task{T}"/> which either completes asynchronously by invoking the mapping function,
|
||||||
|
/// returning any exception thrown by <paramref name="func"/> wrapped in an <see cref="ExceptionError"/>, or
|
||||||
|
/// completes synchronously by returning a new result containing the failure value of the original result.</returns>
|
||||||
|
[Pure]
|
||||||
|
public Task<Result> ThenTryAsync(Func<Task<Result>> func)
|
||||||
|
{
|
||||||
|
if (!IsSuccess)
|
||||||
|
{
|
||||||
|
return Task.FromResult(new Result(Error));
|
||||||
|
}
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var task = func();
|
||||||
|
return CreateResult(task);
|
||||||
|
}
|
||||||
|
catch (Exception exception)
|
||||||
|
{
|
||||||
|
return Task.FromResult(new Result(new ExceptionError(exception)));
|
||||||
|
}
|
||||||
|
|
||||||
|
static async Task<Result> CreateResult(Task<Result> task)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var value = await task;
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
catch (Exception exception)
|
||||||
|
{
|
||||||
|
return new Result(new ExceptionError(exception));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Chains a generic result to this result using an asynchronous mapping function, or does nothing if the result is
|
||||||
|
/// a failure.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="func">The function used to create the next result.</param>
|
||||||
|
/// <typeparam name="TNew">The type of the new value.</typeparam>
|
||||||
|
/// <returns>A <see cref="Task{T}"/> which either completes asynchronously by invoking the mapping function, or
|
||||||
|
/// completes synchronously by returning a new result containing the failure value of the original result.</returns>
|
||||||
|
[Pure]
|
||||||
|
public Task<Result<TNew>> ThenAsync<TNew>(Func<Task<Result<TNew>>> func)
|
||||||
|
{
|
||||||
|
if (!IsSuccess)
|
||||||
|
{
|
||||||
|
return Task.FromResult(new Result<TNew>(Error));
|
||||||
|
}
|
||||||
|
|
||||||
|
var task = func();
|
||||||
|
return CreateResult(task);
|
||||||
|
|
||||||
|
static async Task<Result<TNew>> CreateResult(Task<Result<TNew>> task)
|
||||||
|
{
|
||||||
|
var result = await task;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Tries to chain a generic result to this result using an asynchronous mapping function, or does nothing if the
|
||||||
|
/// result is a failure. If the mapping function throws an exception, the exception will be returned wrapped in
|
||||||
|
/// an <see cref="ExceptionError"/>.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="func">The function used to create the next result.</param>
|
||||||
|
/// <typeparam name="TNew">The type of the new value.</typeparam>
|
||||||
|
/// <returns>A <see cref="Task{T}"/> which either completes asynchronously by invoking the mapping function,
|
||||||
|
/// returning any exception thrown by <paramref name="func"/> wrapped in an <see cref="ExceptionError"/>, or
|
||||||
|
/// completes synchronously by returning a new result containing the failure value of the original result.</returns>
|
||||||
|
[Pure]
|
||||||
|
public Task<Result<TNew>> ThenTryAsync<TNew>(Func<Task<Result<TNew>>> func)
|
||||||
|
{
|
||||||
|
if (!IsSuccess)
|
||||||
|
{
|
||||||
|
return Task.FromResult(new Result<TNew>(Error));
|
||||||
|
}
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var task = func();
|
||||||
|
return CreateResult(task);
|
||||||
|
}
|
||||||
|
catch (Exception exception)
|
||||||
|
{
|
||||||
|
return Task.FromResult(new Result<TNew>(new ExceptionError(exception)));
|
||||||
|
}
|
||||||
|
|
||||||
|
static async Task<Result<TNew>> CreateResult(Task<Result<TNew>> task)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var value = await task;
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
catch (Exception exception)
|
||||||
|
{
|
||||||
|
return new Result<TNew>(new ExceptionError(exception));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public partial class Result
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Maps the success of the result using an asynchronous mapping function, or does nothing if the result is
|
||||||
|
/// a failure.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="func">The function used to map the success.</param>
|
||||||
|
/// <typeparam name="TNew">The type of the new value.</typeparam>
|
||||||
|
/// <returns>A <see cref="ValueTask{T}"/> which either completes asynchronously by invoking the mapping function and
|
||||||
|
/// constructing a new result containing the mapped value, or completes synchronously by returning a new result
|
||||||
|
/// containing the failure value of the original result.</returns>
|
||||||
|
[Pure]
|
||||||
|
public ValueTask<Result<TNew>> MapAsync<TNew>(Func<ValueTask<TNew>> func)
|
||||||
|
{
|
||||||
|
if (!IsSuccess)
|
||||||
|
{
|
||||||
|
return ValueTask.FromResult(new Result<TNew>(Error));
|
||||||
|
}
|
||||||
|
|
||||||
|
var task = func();
|
||||||
|
return CreateResult(task);
|
||||||
|
|
||||||
|
static async ValueTask<Result<TNew>> CreateResult(ValueTask<TNew> task)
|
||||||
|
{
|
||||||
|
var value = await task;
|
||||||
|
return new Result<TNew>(value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Maps the success of the result using an asynchronous mapping function, or does nothing if the result is a
|
||||||
|
/// failure. If the mapping function throws an exception, the exception will be returned wrapped in an
|
||||||
|
/// <see cref="ExceptionError"/>.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="func">The function used to map the success.</param>
|
||||||
|
/// <typeparam name="TNew">The type of the new value.</typeparam>
|
||||||
|
/// <returns>A <see cref="ValueTask{T}"/> which either completes asynchronously by invoking the mapping function and
|
||||||
|
/// constructing a new result containing the mapped value, returning any exception thrown by <paramref name="func"/>
|
||||||
|
/// wrapped in an <see cref="ExceptionError"/> or completes synchronously by returning a new result containing the
|
||||||
|
/// failure value of the original result.</returns>
|
||||||
|
[Pure]
|
||||||
|
public ValueTask<Result<TNew>> TryMapAsync<TNew>(Func<ValueTask<TNew>> func)
|
||||||
|
{
|
||||||
|
if (!IsSuccess)
|
||||||
|
{
|
||||||
|
return ValueTask.FromResult(new Result<TNew>(Error));
|
||||||
|
}
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var task = func();
|
||||||
|
return CreateResult(task);
|
||||||
|
}
|
||||||
|
catch (Exception exception)
|
||||||
|
{
|
||||||
|
return ValueTask.FromResult(new Result<TNew>(new ExceptionError(exception)));
|
||||||
|
}
|
||||||
|
|
||||||
|
static async ValueTask<Result<TNew>> CreateResult(ValueTask<TNew> task)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var value = await task;
|
||||||
|
return new Result<TNew>(value);
|
||||||
|
}
|
||||||
|
catch (Exception exception)
|
||||||
|
{
|
||||||
|
return new Result<TNew>(new ExceptionError(exception));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Chains another result to this result using an asynchronous mapping function, or does nothing if the result is
|
||||||
|
/// a failure.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="func">The function used to create the next result.</param>
|
||||||
|
/// <returns>A <see cref="ValueTask{T}"/> which either completes asynchronously by invoking the mapping function, or
|
||||||
|
/// completes synchronously by returning a new result containing the failure value of the original result.</returns>
|
||||||
|
[Pure]
|
||||||
|
public ValueTask<Result> ThenAsync(Func<ValueTask<Result>> func)
|
||||||
|
{
|
||||||
|
if (!IsSuccess)
|
||||||
|
{
|
||||||
|
return ValueTask.FromResult(new Result(Error));
|
||||||
|
}
|
||||||
|
|
||||||
|
var task = func();
|
||||||
|
return CreateResult(task);
|
||||||
|
|
||||||
|
static async ValueTask<Result> CreateResult(ValueTask<Result> task)
|
||||||
|
{
|
||||||
|
var result = await task;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Tries to chain another result to this result using an asynchronous mapping function, or does nothing if the
|
||||||
|
/// result is a failure. If the mapping function throws an exception, the exception will be returned wrapped in
|
||||||
|
/// an <see cref="ExceptionError"/>.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="func">The function used to create the next result.</param>
|
||||||
|
/// <returns>A <see cref="ValueTask{T}"/> which either completes asynchronously by invoking the mapping function,
|
||||||
|
/// returning any exception thrown by <paramref name="func"/> wrapped in an <see cref="ExceptionError"/>, or
|
||||||
|
/// completes synchronously by returning a new result containing the failure value of the original result.</returns>
|
||||||
|
[Pure]
|
||||||
|
public ValueTask<Result> ThenTryAsync(Func<ValueTask<Result>> func)
|
||||||
|
{
|
||||||
|
if (!IsSuccess)
|
||||||
|
{
|
||||||
|
return ValueTask.FromResult(new Result(Error));
|
||||||
|
}
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var task = func();
|
||||||
|
return CreateResult(task);
|
||||||
|
}
|
||||||
|
catch (Exception exception)
|
||||||
|
{
|
||||||
|
return ValueTask.FromResult(new Result(new ExceptionError(exception)));
|
||||||
|
}
|
||||||
|
|
||||||
|
static async ValueTask<Result> CreateResult(ValueTask<Result> task)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var value = await task;
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
catch (Exception exception)
|
||||||
|
{
|
||||||
|
return new Result(new ExceptionError(exception));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Chains a generic result to this result using an asynchronous mapping function, or does nothing if the result is
|
||||||
|
/// a failure.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="func">The function used to create the next result.</param>
|
||||||
|
/// <typeparam name="TNew">The type of the new value.</typeparam>
|
||||||
|
/// <returns>A <see cref="ValueTask{T}"/> which either completes asynchronously by invoking the mapping function, or
|
||||||
|
/// completes synchronously by returning a new result containing the failure value of the original result.</returns>
|
||||||
|
[Pure]
|
||||||
|
public ValueTask<Result<TNew>> ThenAsync<TNew>(Func<ValueTask<Result<TNew>>> func)
|
||||||
|
{
|
||||||
|
if (!IsSuccess)
|
||||||
|
{
|
||||||
|
return ValueTask.FromResult(new Result<TNew>(Error));
|
||||||
|
}
|
||||||
|
|
||||||
|
var task = func();
|
||||||
|
return CreateResult(task);
|
||||||
|
|
||||||
|
static async ValueTask<Result<TNew>> CreateResult(ValueTask<Result<TNew>> task)
|
||||||
|
{
|
||||||
|
var result = await task;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Tries to chain a generic result to this result using an asynchronous mapping function, or does nothing if the
|
||||||
|
/// result is a failure. If the mapping function throws an exception, the exception will be returned wrapped in
|
||||||
|
/// an <see cref="ExceptionError"/>.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="func">The function used to create the next result.</param>
|
||||||
|
/// <typeparam name="TNew">The type of the new value.</typeparam>
|
||||||
|
/// <returns>A <see cref="ValueTask{T}"/> which either completes asynchronously by invoking the mapping function,
|
||||||
|
/// returning any exception thrown by <paramref name="func"/> wrapped in an <see cref="ExceptionError"/>, or
|
||||||
|
/// completes synchronously by returning a new result containing the failure value of the original result.</returns>
|
||||||
|
[Pure]
|
||||||
|
public ValueTask<Result<TNew>> ThenTryAsync<TNew>(Func<ValueTask<Result<TNew>>> func)
|
||||||
|
{
|
||||||
|
if (!IsSuccess)
|
||||||
|
{
|
||||||
|
return ValueTask.FromResult(new Result<TNew>(Error));
|
||||||
|
}
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var task = func();
|
||||||
|
return CreateResult(task);
|
||||||
|
}
|
||||||
|
catch (Exception exception)
|
||||||
|
{
|
||||||
|
return ValueTask.FromResult(new Result<TNew>(new ExceptionError(exception)));
|
||||||
|
}
|
||||||
|
|
||||||
|
static async ValueTask<Result<TNew>> CreateResult(ValueTask<Result<TNew>> task)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var value = await task;
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
catch (Exception exception)
|
||||||
|
{
|
||||||
|
return new Result<TNew>(new ExceptionError(exception));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@ using System.Diagnostics.Contracts;
|
||||||
|
|
||||||
namespace Geekeey.Request.Result;
|
namespace Geekeey.Request.Result;
|
||||||
|
|
||||||
public readonly partial struct Result<T>
|
public partial class Result<T>
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Tries to get the success value from the result.
|
/// Tries to get the success value from the result.
|
||||||
|
|
|
||||||
|
|
@ -10,18 +10,13 @@ namespace Geekeey.Request.Result;
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// A type which contains either a success value or a failure value, which is represented by an <see cref="Error"/>.
|
/// A type which contains either a success value or a failure value, which is represented by an <see cref="Error"/>.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <typeparam name="T">The type of the success value.</typeparam>
|
public partial class Result : IResultFactory<Result>
|
||||||
[DebuggerTypeProxy(typeof(Result<>.ResultDebugProxy))]
|
|
||||||
public readonly partial struct Result<T>
|
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Creates a new result with a success value.
|
/// Creates a new result with a success value.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="value">The success value.</param>
|
public Result()
|
||||||
public Result(T value)
|
|
||||||
{
|
{
|
||||||
IsSuccess = true;
|
|
||||||
Value = value;
|
|
||||||
Error = default;
|
Error = default;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -31,14 +26,17 @@ public readonly partial struct Result<T>
|
||||||
/// <param name="error">The error of the result.</param>
|
/// <param name="error">The error of the result.</param>
|
||||||
public Result(Error error)
|
public Result(Error error)
|
||||||
{
|
{
|
||||||
IsSuccess = false;
|
|
||||||
Value = default;
|
|
||||||
Error = error;
|
Error = error;
|
||||||
}
|
}
|
||||||
|
|
||||||
internal T? Value { get; }
|
/// <summary>
|
||||||
|
/// Represents the error associated with a failed result.
|
||||||
internal Error? Error => IsSuccess ? null : (field ?? Error.DefaultValueError);
|
/// </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>
|
/// <summary>
|
||||||
/// Whether the result is a success.
|
/// Whether the result is a success.
|
||||||
|
|
@ -46,9 +44,8 @@ public readonly partial struct Result<T>
|
||||||
/// <remarks>
|
/// <remarks>
|
||||||
/// This is always the inverse of <see cref="IsFailure"/> but is more specific about intent.
|
/// This is always the inverse of <see cref="IsFailure"/> but is more specific about intent.
|
||||||
/// </remarks>
|
/// </remarks>
|
||||||
[MemberNotNullWhen(true, nameof(Value))]
|
|
||||||
[MemberNotNullWhen(false, nameof(Error))]
|
[MemberNotNullWhen(false, nameof(Error))]
|
||||||
public bool IsSuccess { get; }
|
public virtual bool IsSuccess => Error is null;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Whether the result is a failure.
|
/// Whether the result is a failure.
|
||||||
|
|
@ -57,8 +54,74 @@ public readonly partial struct Result<T>
|
||||||
/// This is always the inverse of <see cref="IsSuccess"/> but is more specific about intent.
|
/// This is always the inverse of <see cref="IsSuccess"/> but is more specific about intent.
|
||||||
/// </remarks>
|
/// </remarks>
|
||||||
[MemberNotNullWhen(true, nameof(Error))]
|
[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))]
|
[MemberNotNullWhen(false, nameof(Value))]
|
||||||
public bool IsFailure => !IsSuccess;
|
public override bool IsFailure => base.IsFailure;
|
||||||
|
|
||||||
|
/// <inheritdoc/>
|
||||||
|
[Pure]
|
||||||
|
static Result<T> IResultFactory<Result<T>>.Failure(Error error)
|
||||||
|
{
|
||||||
|
return new Result<T>(error);
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets a string representation of the result.
|
/// Gets a string representation of the result.
|
||||||
|
|
|
||||||
|
|
@ -31,6 +31,21 @@ public static partial class Extensions
|
||||||
return (await result).Map(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})"/>
|
/// <inheritdoc cref="Map{T,TNew}(Task{Result{T}},Func{T,TNew})"/>
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
public static async Task<Result<TNew>> MapAsync<T, TNew>(this Task<Result<T>> result, Func<T, ValueTask<TNew>> func)
|
public static async Task<Result<TNew>> MapAsync<T, TNew>(this Task<Result<T>> result, Func<T, ValueTask<TNew>> func)
|
||||||
|
|
@ -38,6 +53,20 @@ public static partial class Extensions
|
||||||
return await (await result).MapAsync(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>
|
/// <summary>
|
||||||
/// Maps the success value of the result object of the completed task to a new result using a mapping function, or
|
/// 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.
|
/// does nothing if the result object of the completed task is a failure.
|
||||||
|
|
@ -55,6 +84,21 @@ public static partial class Extensions
|
||||||
return (await result).Then(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}})"/>
|
/// <inheritdoc cref="Then{T,TNew}(Task{Result{T}},Func{T,Result{TNew}})"/>
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
public static async Task<Result<TNew>> ThenAsync<T, TNew>(this Task<Result<T>> result, Func<T, ValueTask<Result<TNew>>> func)
|
public static async Task<Result<TNew>> ThenAsync<T, TNew>(this Task<Result<T>> result, Func<T, ValueTask<Result<TNew>>> func)
|
||||||
|
|
@ -62,6 +106,64 @@ public static partial class Extensions
|
||||||
return await (await result).ThenAsync(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
|
#endregion
|
||||||
|
|
||||||
#region ValueTask<Result<T>>
|
#region ValueTask<Result<T>>
|
||||||
|
|
@ -74,6 +176,21 @@ public static partial class Extensions
|
||||||
return (await result).Map(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})"/>
|
/// <inheritdoc cref="Map{T,TNew}(Task{Result{T}},Func{T,TNew})"/>
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
public static async ValueTask<Result<TNew>> MapAsync<T, TNew>(this ValueTask<Result<T>> result, Func<T, ValueTask<TNew>> func)
|
public static async ValueTask<Result<TNew>> MapAsync<T, TNew>(this ValueTask<Result<T>> result, Func<T, ValueTask<TNew>> func)
|
||||||
|
|
@ -81,6 +198,20 @@ public static partial class Extensions
|
||||||
return await (await result).MapAsync(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}})"/>
|
/// <inheritdoc cref="Then{T,TNew}(Task{Result{T}},Func{T,Result{TNew}})"/>
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
// ReSharper disable once InconsistentNaming
|
// ReSharper disable once InconsistentNaming
|
||||||
|
|
@ -89,6 +220,21 @@ public static partial class Extensions
|
||||||
return (await result).Then(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}})"/>
|
/// <inheritdoc cref="Then{T,TNew}(Task{Result{T}},Func{T,Result{TNew}})"/>
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
public static async ValueTask<Result<TNew>> ThenAsync<T, TNew>(this ValueTask<Result<T>> result, Func<T, ValueTask<Result<TNew>>> func)
|
public static async ValueTask<Result<TNew>> ThenAsync<T, TNew>(this ValueTask<Result<T>> result, Func<T, ValueTask<Result<TNew>>> func)
|
||||||
|
|
@ -96,5 +242,358 @@ public static partial class Extensions
|
||||||
return await (await result).ThenAsync(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
|
#endregion
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue