From cb2628c61563afaa5892212f7f617342dc98d2c0 Mon Sep 17 00:00:00 2001 From: Louis Seubert Date: Sun, 24 May 2026 22:40:11 +0200 Subject: [PATCH] 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` --- .../ExtensionsTaskTests.cs | 107 +++ .../IResultFactoryTests.cs | 34 + src/request.result.tests/PreludeTests.cs | 80 ++ .../ResultConversionTests.cs | 12 + .../ResultEqualityTests.cs | 27 + .../ResultMatchingTests.cs | 133 ++++ .../ResultOperatorTests.cs | 104 +++ src/request.result.tests/ResultTests.cs | 61 +- .../ResultTransformTests.cs | 629 ++++++++++++++++ src/request.result/IResultFactory.cs | 20 + src/request.result/Prelude.cs | 82 ++ src/request.result/Result.Conversion.cs | 15 +- src/request.result/Result.Equality.cs | 94 ++- src/request.result/Result.Matching.cs | 101 ++- src/request.result/Result.Transform.cs | 699 +++++++++++++++++- src/request.result/Result.Unbox.cs | 2 +- src/request.result/Result.cs | 93 ++- .../_extensions/Extensions.Task.cs | 499 +++++++++++++ 18 files changed, 2748 insertions(+), 44 deletions(-) create mode 100644 src/request.result.tests/ExtensionsTaskTests.cs create mode 100644 src/request.result.tests/IResultFactoryTests.cs create mode 100644 src/request.result.tests/ResultOperatorTests.cs create mode 100644 src/request.result/IResultFactory.cs diff --git a/src/request.result.tests/ExtensionsTaskTests.cs b/src/request.result.tests/ExtensionsTaskTests.cs new file mode 100644 index 0000000..8f34e24 --- /dev/null +++ b/src/request.result.tests/ExtensionsTaskTests.cs @@ -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(value => throw new CustomTestException()); + + using var scope = Assert.Multiple(); + await Assert.That(result.IsSuccess).IsFalse(); + var instance = await Assert.That(result.Error).IsTypeOf(); + await Assert.That(instance?.Exception).IsTypeOf(); + } + + [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"); + } +} diff --git a/src/request.result.tests/IResultFactoryTests.cs b/src/request.result.tests/IResultFactoryTests.cs new file mode 100644 index 0000000..7d4a652 --- /dev/null +++ b/src/request.result.tests/IResultFactoryTests.cs @@ -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).IsAssignableFrom(typeof(Result))).IsTrue(); + await Assert.That(typeof(IResultFactory>).IsAssignableFrom(typeof(Result))).IsTrue(); + } + + [Test] + public async Task I_can_create_failure_result_from_error_with_result_factory() + { + { + var error = new StringError("error"); + var result = CreateFailure(error); + await Assert.That(result.IsFailure).IsTrue(); + } + { + var error = new StringError("error"); + var result = CreateFailure>(error); + await Assert.That(result.IsFailure).IsTrue(); + } + } + + private static TResult CreateFailure(Error error) where TResult : IResultFactory + { + return TResult.Failure(error); + } +} diff --git a/src/request.result.tests/PreludeTests.cs b/src/request.result.tests/PreludeTests.cs index 6d6e098..31c89a0 100644 --- a/src/request.result.tests/PreludeTests.cs +++ b/src/request.result.tests/PreludeTests.cs @@ -97,4 +97,84 @@ internal sealed class PreludeTests var instance = await Assert.That(result.Error).IsTypeOf(); await Assert.That(instance?.Exception).IsTypeOf(); } + + [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(); + await Assert.That(instance?.Exception).IsTypeOf(); + } + + [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(); + await Assert.That(instance?.Exception).IsTypeOf(); + } + + [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(); + await Assert.That(instance?.Exception).IsTypeOf(); + } } diff --git a/src/request.result.tests/ResultConversionTests.cs b/src/request.result.tests/ResultConversionTests.cs index 0033efe..1d47dd7 100644 --- a/src/request.result.tests/ResultConversionTests.cs +++ b/src/request.result.tests/ResultConversionTests.cs @@ -61,4 +61,16 @@ internal sealed class ResultConversionTests await Assert.That(() => (int)result).Throws(); } + + [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(); + } } diff --git a/src/request.result.tests/ResultEqualityTests.cs b/src/request.result.tests/ResultEqualityTests.cs index 2998196..67233dd 100644 --- a/src/request.result.tests/ResultEqualityTests.cs +++ b/src/request.result.tests/ResultEqualityTests.cs @@ -172,4 +172,31 @@ internal sealed class ResultEqualityTests 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(); + } } diff --git a/src/request.result.tests/ResultMatchingTests.cs b/src/request.result.tests/ResultMatchingTests.cs index c383207..e36c57d 100644 --- a/src/request.result.tests/ResultMatchingTests.cs +++ b/src/request.result.tests/ResultMatchingTests.cs @@ -229,4 +229,137 @@ internal sealed class ResultMatchingTests 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(); + } + } } diff --git a/src/request.result.tests/ResultOperatorTests.cs b/src/request.result.tests/ResultOperatorTests.cs new file mode 100644 index 0000000..95918b9 --- /dev/null +++ b/src/request.result.tests/ResultOperatorTests.cs @@ -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("error 1"); + var b = Prelude.Failure("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("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()); + } +} diff --git a/src/request.result.tests/ResultTests.cs b/src/request.result.tests/ResultTests.cs index 4c63168..4d201f0 100644 --- a/src/request.result.tests/ResultTests.cs +++ b/src/request.result.tests/ResultTests.cs @@ -29,18 +29,6 @@ internal sealed class ResultTests await Assert.That(result.Error).IsTypeOf(); } - [Test] - public async Task I_can_distinguish_default_result_from_created() - { - var result = default(Result); - - 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] 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 }"); } + + [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(); + } + + [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(1); + + using var scope = Assert.Multiple(); + await Assert.That(result.IsSuccess).IsTrue(); + await Assert.That(result.IsFailure).IsFalse(); + await Assert.That(result.Error).IsNull(); + } } diff --git a/src/request.result.tests/ResultTransformTests.cs b/src/request.result.tests/ResultTransformTests.cs index 7924bfe..92f2a18 100644 --- a/src/request.result.tests/ResultTransformTests.cs +++ b/src/request.result.tests/ResultTransformTests.cs @@ -641,4 +641,633 @@ internal sealed class ResultTransformTests await Assert.That(result.Error).IsTypeOf(); 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(() => throw new CustomTestException()); + + using var scope = Assert.Multiple(); + await Assert.That(result.IsSuccess).IsFalse(); + var instance = await Assert.That(result.Error).IsTypeOf(); + await Assert.That(instance?.Exception).IsTypeOf(); + } + + [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>)(() => throw new CustomTestException())); + + using var scope = Assert.Multiple(); + await Assert.That(result.IsSuccess).IsFalse(); + var instance = await Assert.That(result.Error).IsTypeOf(); + await Assert.That(instance?.Exception).IsTypeOf(); + } + + [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>)(() => throw new CustomTestException())); + + using var scope = Assert.Multiple(); + await Assert.That(result.IsSuccess).IsFalse(); + var instance = await Assert.That(result.Error).IsTypeOf(); + await Assert.That(instance?.Exception).IsTypeOf(); + } + + [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(); + await Assert.That(instance?.Exception).IsTypeOf(); + } + + [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 (_) => throw new CustomTestException()); + + using var scope = Assert.Multiple(); + await Assert.That(result.IsSuccess).IsFalse(); + var instance = await Assert.That(result.Error).IsTypeOf(); + await Assert.That(instance?.Exception).IsTypeOf(); + } + + [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 (_) => throw new CustomTestException()); + + using var scope = Assert.Multiple(); + await Assert.That(result.IsSuccess).IsFalse(); + var instance = await Assert.That(result.Error).IsTypeOf(); + await Assert.That(instance?.Exception).IsTypeOf(); + } + + [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(); + await Assert.That(instance?.Exception).IsTypeOf(); + } + + [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(() => throw new CustomTestException()); + + using var scope = Assert.Multiple(); + await Assert.That(result.IsSuccess).IsFalse(); + var instance = await Assert.That(result.Error).IsTypeOf(); + await Assert.That(instance?.Exception).IsTypeOf(); + } + + [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>)(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(); + await Assert.That(instance?.Exception).IsTypeOf(); + } + + [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>)(() => throw new CustomTestException())); + + using var scope = Assert.Multiple(); + await Assert.That(result.IsSuccess).IsFalse(); + var instance = await Assert.That(result.Error).IsTypeOf(); + await Assert.That(instance?.Exception).IsTypeOf(); + } + + [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>)(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(); + await Assert.That(instance?.Exception).IsTypeOf(); + } + + [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>>)(() => throw new CustomTestException())); + + using var scope = Assert.Multiple(); + await Assert.That(result.IsSuccess).IsFalse(); + var instance = await Assert.That(result.Error).IsTypeOf(); + await Assert.That(instance?.Exception).IsTypeOf(); + } + + [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>>)(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(); + await Assert.That(instance?.Exception).IsTypeOf(); + } + + [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("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 (_) => + { + 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(); + await Assert.That(instance?.Exception).IsTypeOf(); + } + + [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>)(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(); + await Assert.That(instance?.Exception).IsTypeOf(); + } + + [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>)(() => throw new CustomTestException())); + + using var scope = Assert.Multiple(); + await Assert.That(result.IsSuccess).IsFalse(); + var instance = await Assert.That(result.Error).IsTypeOf(); + await Assert.That(instance?.Exception).IsTypeOf(); + } + + [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>)(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(); + await Assert.That(instance?.Exception).IsTypeOf(); + } + + [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>>)(() => throw new CustomTestException())); + + using var scope = Assert.Multiple(); + await Assert.That(result.IsSuccess).IsFalse(); + var instance = await Assert.That(result.Error).IsTypeOf(); + await Assert.That(instance?.Exception).IsTypeOf(); + } + + [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>>)(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(); + await Assert.That(instance?.Exception).IsTypeOf(); + } } diff --git a/src/request.result/IResultFactory.cs b/src/request.result/IResultFactory.cs new file mode 100644 index 0000000..b46566b --- /dev/null +++ b/src/request.result/IResultFactory.cs @@ -0,0 +1,20 @@ +// Copyright (c) The Geekeey Authors +// SPDX-License-Identifier: EUPL-1.2 + +using System.Diagnostics.CodeAnalysis; + +namespace Geekeey.Request.Result; + +/// +/// An interface for a result. +/// +[SuppressMessage("Naming", "CA1716:Identifiers should not match keywords")] +public interface IResultFactory where TSelf : IResultFactory +{ + /// + /// Creates a new result with a failure value. + /// + /// The error of the result. + /// A new result with a failure value. + static abstract TSelf Failure(Error error); +} diff --git a/src/request.result/Prelude.cs b/src/request.result/Prelude.cs index 9ee4d6c..de48354 100644 --- a/src/request.result/Prelude.cs +++ b/src/request.result/Prelude.cs @@ -14,6 +14,25 @@ namespace Geekeey.Request.Result; /// public static class Prelude { + /// + /// Creates a result containing a success. + /// + [Pure] + public static Result Success() + { + return new Result(); + } + + /// + /// Creates a result containing a failure. + /// + /// The failure value to create the result from. + [Pure] + public static Result Failure(Error error) + { + return new Result(error); + } + /// /// Creates a result containing a success value. /// @@ -36,6 +55,69 @@ public static class Prelude return new Result(error); } + /// + /// Tries to execute an action and return the result. If the action throws an exception, the exception will be + /// returned wrapped in an . + /// + /// The action to try to execute. + /// A result containing success or an containing the exception thrown by the + /// action. + [Pure] + public static Result Try(Action function) + { + try + { + function(); + return new Result(); + } + catch (Exception exception) + { + return new Result(new ExceptionError(exception)); + } + } + + /// + /// Tries to execute an asynchronous function and return the result. If the function throws an exception, the + /// exception will be returned wrapped in an . + /// + /// The function to try to execute. + /// A result containing success or an containing the exception thrown by the + /// function. + [Pure] + public static async ValueTask TryAsync(Func function) + { + try + { + await function(); + return new Result(); + } + catch (Exception exception) + { + return new Result(new ExceptionError(exception)); + } + } + + /// + /// Tries to execute an asynchronous function and return the result. If the function throws an exception, the + /// exception will be returned wrapped in an . + /// + /// The function to try to execute. + /// A result containing success or an containing the exception thrown by the + /// function. + [Pure] + public static async Task TryAsync(Func function) + { + try + { + await function(); + return new Result(); + } + catch (Exception exception) + { + return new Result(new ExceptionError(exception)); + } + } + /// /// Tries to execute a function and return the result. If the function throws an exception, the exception will be /// returned wrapped in an . diff --git a/src/request.result/Result.Conversion.cs b/src/request.result/Result.Conversion.cs index 292a643..6305c6e 100644 --- a/src/request.result/Result.Conversion.cs +++ b/src/request.result/Result.Conversion.cs @@ -5,7 +5,20 @@ using System.Diagnostics.Contracts; namespace Geekeey.Request.Result; -public readonly partial struct Result +public partial class Result +{ + /// + /// Implicitly constructs a result from a failure value. + /// + /// The error to construct the result from. + [Pure] + public static implicit operator Result(Error error) + { + return new Result(error); + } +} + +public partial class Result { /// /// Implicitly constructs a result from a success value. diff --git a/src/request.result/Result.Equality.cs b/src/request.result/Result.Equality.cs index 1e5a3ba..0253b88 100644 --- a/src/request.result/Result.Equality.cs +++ b/src/request.result/Result.Equality.cs @@ -7,7 +7,73 @@ using System.Numerics; namespace Geekeey.Request.Result; -public readonly partial struct Result : IEquatable>, IEquatable +public partial class Result : IEquatable +{ + /// + [Pure] + public bool Equals(Result? other) + { + return Equals(this, other); + } + + /// + [Pure] + public override bool Equals(object? obj) + { + return obj is Result r && Equals(r); + } + + /// + [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 +{ + /// + /// Checks whether two results are equal. Results are equal if they are both success or both failure. + /// + /// The first result to compare. + /// The second result to compare. + [Pure] + [ExcludeFromCodeCoverage] + public static bool operator ==(Result? a, Result? b) + { + return Equals(a, b); + } + + /// + /// Checks whether two results are not equal. Results are equal if they are both success or both failure. + /// + /// The first result to compare. + /// The second result to compare. + [Pure] + [ExcludeFromCodeCoverage] + public static bool operator !=(Result? a, Result? b) + { + return !Equals(a, b); + } +} + +public partial class Result : IEquatable>, IEquatable { /// /// 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 : IEquatable>, IEquatable /// /// The result to check for equality with the current result. [Pure] - public bool Equals(Result other) + public bool Equals(Result? other) { return Equals(this, other, EqualityComparer.Default); } @@ -68,8 +134,13 @@ public readonly partial struct Result : IEquatable>, IEquatable return GetHashCode(this, EqualityComparer.Default); } - internal static bool Equals(Result a, Result b, IEqualityComparer comparer) + internal static bool Equals(Result? a, Result? b, IEqualityComparer comparer) { + if (a is null || b is null) + { + return a is null && b is null; + } + if (!a.IsSuccess || !b.IsSuccess) { return !a.IsSuccess && !b.IsSuccess; @@ -83,8 +154,13 @@ public readonly partial struct Result : IEquatable>, IEquatable return comparer.Equals(a.Value, b.Value); } - internal static bool Equals(Result a, T? b, IEqualityComparer comparer) + internal static bool Equals(Result? a, T? b, IEqualityComparer comparer) { + if (a is null) + { + return b is null; + } + if (!a.IsSuccess) { return false; @@ -109,7 +185,7 @@ public readonly partial struct Result : IEquatable>, IEquatable } } -public readonly partial struct Result : IEqualityOperators, Result, bool>, IEqualityOperators, T, bool> +public partial class Result : IEqualityOperators, Result, bool>, IEqualityOperators, T, bool> { /// /// 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 : IEqualityOperators, Result< /// The second result to compare. [Pure] [ExcludeFromCodeCoverage] - public static bool operator ==(Result a, Result b) + public static bool operator ==(Result? a, Result? b) { return Equals(a, b, EqualityComparer.Default); } @@ -132,7 +208,7 @@ public readonly partial struct Result : IEqualityOperators, Result< /// The second result to compare. [Pure] [ExcludeFromCodeCoverage] - public static bool operator !=(Result a, Result b) + public static bool operator !=(Result? a, Result? b) { return !Equals(a, b, EqualityComparer.Default); } @@ -144,7 +220,7 @@ public readonly partial struct Result : IEqualityOperators, Result< /// The value to check for equality with the success value in the result. [Pure] [ExcludeFromCodeCoverage] - public static bool operator ==(Result a, T? b) + public static bool operator ==(Result? a, T? b) { return Equals(a, b, EqualityComparer.Default); } @@ -156,7 +232,7 @@ public readonly partial struct Result : IEqualityOperators, Result< /// The value to check for inequality with the success value in the result. [Pure] [ExcludeFromCodeCoverage] - public static bool operator !=(Result a, T? b) + public static bool operator !=(Result? a, T? b) { return !Equals(a, b, EqualityComparer.Default); } diff --git a/src/request.result/Result.Matching.cs b/src/request.result/Result.Matching.cs index cc4aac6..28e5a58 100644 --- a/src/request.result/Result.Matching.cs +++ b/src/request.result/Result.Matching.cs @@ -5,7 +5,102 @@ using System.Diagnostics.Contracts; namespace Geekeey.Request.Result; -public readonly partial struct Result +public partial class Result +{ + /// + /// Matches over the success or failure of the result and returns another value. Can be conceptualized as an + /// exhaustive switch expression matching all possible states of the type. + /// + /// The type to return from the match. + /// The function to invoke if the result is a success. + /// The function to apply to the failure value of the result if the result is a failure. + /// The result of applying either or on the result. + [Pure] + public TResult Match(Func success, Func failure) + { + return IsSuccess ? success() : failure(Error); + } + + /// + /// Matches over the success or failure of the result and invokes an effectful action. Can be conceptualized as an + /// exhaustive switch statement matching all possible states of the type. + /// + /// The function to call if the result is a success. + /// The function to call with the failure value of the result if the result is a failure. + public void Switch(Action success, Action failure) + { + if (IsSuccess) + { + success(); + } + else + { + failure(Error); + } + } +} + +public partial class Result +{ + /// + /// Asynchronously matches over the success or failure of the result and returns another value. Can be + /// conceptualized as an exhaustive switch expression matching all possible states of the type. + /// + /// The type to return from the match. + /// The function to invoke if the result is a success. + /// The function to apply to the failure value of the result if the result is a failure. + /// A task completing with the result of applying either or + /// on the failure value of the result. + [Pure] + public async Task MatchAsync(Func> success, Func> failure) + { + return IsSuccess ? await success() : await failure(Error); + } + + /// + /// 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 switch statement matching all possible states of + /// the type. + /// + /// The function to call if the result is a success. + /// The function to call with the failure value of the result if the result is a failure. + public Task SwitchAsync(Func success, Func failure) + { + return IsSuccess ? success() : failure(Error); + } +} + +public partial class Result +{ + /// + /// Asynchronously matches over the success or failure of the result and returns another value. Can be + /// conceptualized as an exhaustive switch expression matching all possible states of the type. + /// + /// The type to return from the match. + /// The function to invoke if the result is a success. + /// The function to apply to the failure value of the result if the result is a failure. + /// A task completing with the result of applying either or + /// on the failure value of the result. + [Pure] + public ValueTask MatchAsync(Func> success, Func> failure) + { + return IsSuccess ? success() : failure(Error); + } + + /// + /// 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 switch statement matching all possible states of + /// the type. + /// + /// The function to call if the result is a success. + /// The function to call with the failure value of the result if the result is a failure. + public ValueTask SwitchAsync(Func success, Func failure) + { + return IsSuccess ? success() : failure(Error); + } +} + +public partial class Result { /// /// 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 } } -public readonly partial struct Result +public partial class Result { /// /// 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 } } -public readonly partial struct Result +public partial class Result { /// /// Asynchronously matches over the success value or failure value of the result and returns another value. Can be diff --git a/src/request.result/Result.Transform.cs b/src/request.result/Result.Transform.cs index a78058e..9885daa 100644 --- a/src/request.result/Result.Transform.cs +++ b/src/request.result/Result.Transform.cs @@ -5,7 +5,7 @@ using System.Diagnostics.Contracts; namespace Geekeey.Request.Result; -public readonly partial struct Result +public partial class Result { /// /// 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 } } -public readonly partial struct Result +public partial class Result { /// /// 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 } } -public readonly partial struct Result +public partial class Result { /// /// 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 } } } + +public partial class Result +{ + /// + /// Chains a non-generic result to this result if it is a success, or does nothing if the result is a failure. + /// + /// The function used to create the next result. + /// A result which is either the mapped result or a new result containing the failure value of the original + /// result. + [Pure] + public Result Then(Func func) + { + return IsSuccess ? func(Value) : new Result(Error); + } + + /// + /// 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 + /// . + /// + /// The function used to create the next result. + /// A result which is either the mapped result, the exception thrown by wrapped in + /// an , or a new result containing the failure value of the original result. + [Pure] + public Result ThenTry(Func func) + { + try + { + return Then(func); + } + catch (Exception exception) + { + return new Result(new ExceptionError(exception)); + } + } +} + +public partial class Result +{ + /// + /// Chains a non-generic result to this result using an asynchronous mapping function, or does nothing if the result is + /// a failure. + /// + /// The function used to create the next result. + /// A 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. + [Pure] + public Task ThenAsync(Func> func) + { + if (!IsSuccess) + { + return Task.FromResult(new Result(Error)); + } + + var task = func(Value); + return CreateResult(task); + + static async Task CreateResult(Task task) + { + var result = await task; + return result; + } + } + + /// + /// 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 . + /// + /// The function used to create the next result. + /// A which either completes asynchronously by invoking the mapping function, + /// returning any exception thrown by wrapped in an , or + /// completes synchronously by returning a new result containing the failure value of the original result. + [Pure] + public Task ThenTryAsync(Func> 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 CreateResult(Task task) + { + try + { + var value = await task; + return value; + } + catch (Exception exception) + { + return new Result(new ExceptionError(exception)); + } + } + } +} + +public partial class Result +{ + /// + /// Chains a non-generic result to this result using an asynchronous mapping function, or does nothing if the result is + /// a failure. + /// + /// The function used to create the next result. + /// A 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. + [Pure] + public ValueTask ThenAsync(Func> func) + { + if (!IsSuccess) + { + return ValueTask.FromResult(new Result(Error)); + } + + var task = func(Value); + return CreateResult(task); + + static async ValueTask CreateResult(ValueTask task) + { + var result = await task; + return result; + } + } + + /// + /// 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 . + /// + /// The function used to create the next result. + /// A which either completes asynchronously by invoking the mapping function, + /// returning any exception thrown by wrapped in an , or + /// completes synchronously by returning a new result containing the failure value of the original result. + [Pure] + public ValueTask ThenTryAsync(Func> 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 CreateResult(ValueTask task) + { + try + { + var value = await task; + return value; + } + catch (Exception exception) + { + return new Result(new ExceptionError(exception)); + } + } + } +} + +public partial class Result +{ + /// + /// Maps the success of the result to a new success value using a mapping function, or does nothing if the result is + /// a failure. + /// + /// The function used to map the success to a new value. + /// The type of the new value. + /// A new result containing either the mapped success value or the failure value of the original + /// result. + [Pure] + public Result Map(Func func) + { + return IsSuccess ? new Result(func()) : new Result(Error); + } + + /// + /// 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 + /// . + /// + /// The function used to map the success to a new value. + /// The type of the new value. + /// A new result containing either the mapped value, the exception thrown by + /// wrapped in an , or the failure value of the original result. + [Pure] + public Result TryMap(Func func) + { + try + { + return Map(func); + } + catch (Exception exception) + { + return new Result(new ExceptionError(exception)); + } + } + + /// + /// Chains another result to this result if it is a success, or does nothing if the result is a failure. + /// + /// The function used to create the next result. + /// A result which is either the mapped result or a new result containing the failure value of the original + /// result. + [Pure] + public Result Then(Func func) + { + return IsSuccess ? func() : new Result(Error); + } + + /// + /// 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 . + /// + /// The function used to create the next result. + /// A result which is either the mapped result, the exception thrown by wrapped in + /// an , or a new result containing the failure value of the original result. + [Pure] + public Result ThenTry(Func func) + { + try + { + return Then(func); + } + catch (Exception exception) + { + return new Result(new ExceptionError(exception)); + } + } + + /// + /// Chains a generic result to this result if it is a success, or does nothing if the result is a failure. + /// + /// The function used to create the next generic result. + /// The type of the new value. + /// A result which is either the mapped result or a new result containing the failure value of the original + /// result. + [Pure] + public Result Then(Func> func) + { + return IsSuccess ? func() : new Result(Error); + } + + /// + /// 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 . + /// + /// The function used to create the next generic result. + /// The type of the new value. + /// A result which is either the mapped result, the exception thrown by wrapped in + /// an , or a new result containing the failure value of the original result. + [Pure] + public Result ThenTry(Func> func) + { + try + { + return Then(func); + } + catch (Exception exception) + { + return new Result(new ExceptionError(exception)); + } + } +} + +public partial class Result +{ + /// + /// Maps the success of the result using an asynchronous mapping function, or does nothing if the result is + /// a failure. + /// + /// The function used to map the success. + /// The type of the new value. + /// A 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. + [Pure] + public Task> MapAsync(Func> func) + { + if (!IsSuccess) + { + return Task.FromResult(new Result(Error)); + } + + var task = func(); + return CreateResult(task); + + static async Task> CreateResult(Task task) + { + var value = await task; + return new Result(value); + } + } + + /// + /// 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 + /// . + /// + /// The function used to map the success. + /// The type of the new value. + /// A which either completes asynchronously by invoking the mapping function and + /// constructing a new result containing the mapped value, returning any exception thrown by + /// wrapped in an or completes synchronously by returning a new result containing the + /// failure value of the original result. + [Pure] + public Task> TryMapAsync(Func> 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> CreateResult(Task task) + { + try + { + var value = await task; + return new Result(value); + } + catch (Exception exception) + { + return new Result(new ExceptionError(exception)); + } + } + } + + /// + /// Chains another result to this result using an asynchronous mapping function, or does nothing if the result is + /// a failure. + /// + /// The function used to create the next result. + /// A 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. + [Pure] + public Task ThenAsync(Func> func) + { + if (!IsSuccess) + { + return Task.FromResult(new Result(Error)); + } + + var task = func(); + return CreateResult(task); + + static async Task CreateResult(Task task) + { + var result = await task; + return result; + } + } + + /// + /// 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 . + /// + /// The function used to create the next result. + /// A which either completes asynchronously by invoking the mapping function, + /// returning any exception thrown by wrapped in an , or + /// completes synchronously by returning a new result containing the failure value of the original result. + [Pure] + public Task ThenTryAsync(Func> 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 CreateResult(Task task) + { + try + { + var value = await task; + return value; + } + catch (Exception exception) + { + return new Result(new ExceptionError(exception)); + } + } + } + + /// + /// Chains a generic result to this result using an asynchronous mapping function, or does nothing if the result is + /// a failure. + /// + /// The function used to create the next result. + /// The type of the new value. + /// A 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. + [Pure] + public Task> ThenAsync(Func>> func) + { + if (!IsSuccess) + { + return Task.FromResult(new Result(Error)); + } + + var task = func(); + return CreateResult(task); + + static async Task> CreateResult(Task> task) + { + var result = await task; + return result; + } + } + + /// + /// 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 . + /// + /// The function used to create the next result. + /// The type of the new value. + /// A which either completes asynchronously by invoking the mapping function, + /// returning any exception thrown by wrapped in an , or + /// completes synchronously by returning a new result containing the failure value of the original result. + [Pure] + public Task> ThenTryAsync(Func>> 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> CreateResult(Task> task) + { + try + { + var value = await task; + return value; + } + catch (Exception exception) + { + return new Result(new ExceptionError(exception)); + } + } + } +} + +public partial class Result +{ + /// + /// Maps the success of the result using an asynchronous mapping function, or does nothing if the result is + /// a failure. + /// + /// The function used to map the success. + /// The type of the new value. + /// A 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. + [Pure] + public ValueTask> MapAsync(Func> func) + { + if (!IsSuccess) + { + return ValueTask.FromResult(new Result(Error)); + } + + var task = func(); + return CreateResult(task); + + static async ValueTask> CreateResult(ValueTask task) + { + var value = await task; + return new Result(value); + } + } + + /// + /// 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 + /// . + /// + /// The function used to map the success. + /// The type of the new value. + /// A which either completes asynchronously by invoking the mapping function and + /// constructing a new result containing the mapped value, returning any exception thrown by + /// wrapped in an or completes synchronously by returning a new result containing the + /// failure value of the original result. + [Pure] + public ValueTask> TryMapAsync(Func> 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> CreateResult(ValueTask task) + { + try + { + var value = await task; + return new Result(value); + } + catch (Exception exception) + { + return new Result(new ExceptionError(exception)); + } + } + } + + /// + /// Chains another result to this result using an asynchronous mapping function, or does nothing if the result is + /// a failure. + /// + /// The function used to create the next result. + /// A 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. + [Pure] + public ValueTask ThenAsync(Func> func) + { + if (!IsSuccess) + { + return ValueTask.FromResult(new Result(Error)); + } + + var task = func(); + return CreateResult(task); + + static async ValueTask CreateResult(ValueTask task) + { + var result = await task; + return result; + } + } + + /// + /// 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 . + /// + /// The function used to create the next result. + /// A which either completes asynchronously by invoking the mapping function, + /// returning any exception thrown by wrapped in an , or + /// completes synchronously by returning a new result containing the failure value of the original result. + [Pure] + public ValueTask ThenTryAsync(Func> 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 CreateResult(ValueTask task) + { + try + { + var value = await task; + return value; + } + catch (Exception exception) + { + return new Result(new ExceptionError(exception)); + } + } + } + + /// + /// Chains a generic result to this result using an asynchronous mapping function, or does nothing if the result is + /// a failure. + /// + /// The function used to create the next result. + /// The type of the new value. + /// A 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. + [Pure] + public ValueTask> ThenAsync(Func>> func) + { + if (!IsSuccess) + { + return ValueTask.FromResult(new Result(Error)); + } + + var task = func(); + return CreateResult(task); + + static async ValueTask> CreateResult(ValueTask> task) + { + var result = await task; + return result; + } + } + + /// + /// 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 . + /// + /// The function used to create the next result. + /// The type of the new value. + /// A which either completes asynchronously by invoking the mapping function, + /// returning any exception thrown by wrapped in an , or + /// completes synchronously by returning a new result containing the failure value of the original result. + [Pure] + public ValueTask> ThenTryAsync(Func>> 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> CreateResult(ValueTask> task) + { + try + { + var value = await task; + return value; + } + catch (Exception exception) + { + return new Result(new ExceptionError(exception)); + } + } + } +} diff --git a/src/request.result/Result.Unbox.cs b/src/request.result/Result.Unbox.cs index 2533905..b633bdd 100644 --- a/src/request.result/Result.Unbox.cs +++ b/src/request.result/Result.Unbox.cs @@ -6,7 +6,7 @@ using System.Diagnostics.Contracts; namespace Geekeey.Request.Result; -public readonly partial struct Result +public partial class Result { /// /// Tries to get the success value from the result. diff --git a/src/request.result/Result.cs b/src/request.result/Result.cs index 0f2d5ea..1265cd0 100644 --- a/src/request.result/Result.cs +++ b/src/request.result/Result.cs @@ -10,18 +10,13 @@ namespace Geekeey.Request.Result; /// /// A type which contains either a success value or a failure value, which is represented by an . /// -/// The type of the success value. -[DebuggerTypeProxy(typeof(Result<>.ResultDebugProxy))] -public readonly partial struct Result +public partial class Result : IResultFactory { /// /// Creates a new result with a success value. /// - /// The success value. - public Result(T value) + public Result() { - IsSuccess = true; - Value = value; Error = default; } @@ -31,14 +26,17 @@ public readonly partial struct Result /// The error of the result. public Result(Error error) { - IsSuccess = false; - Value = default; Error = error; } - internal T? Value { get; } - - internal Error? Error => IsSuccess ? null : (field ?? Error.DefaultValueError); + /// + /// Represents the error associated with a failed result. + /// + /// + /// When a result is unsuccessful, this property will hold an instance of a type derived from . + /// It is null when the result is successful. + /// + public Error? Error { get; } /// /// Whether the result is a success. @@ -46,9 +44,8 @@ public readonly partial struct Result /// /// This is always the inverse of but is more specific about intent. /// - [MemberNotNullWhen(true, nameof(Value))] [MemberNotNullWhen(false, nameof(Error))] - public bool IsSuccess { get; } + public virtual bool IsSuccess => Error is null; /// /// Whether the result is a failure. @@ -57,8 +54,74 @@ public readonly partial struct Result /// This is always the inverse of but is more specific about intent. /// [MemberNotNullWhen(true, nameof(Error))] + public virtual bool IsFailure => !IsSuccess; + + /// + [Pure] + static Result IResultFactory.Failure(Error error) + { + return new Result(error); + } + + /// + /// Gets a string representation of the result. + /// + [Pure] + public override string ToString() + { + return IsSuccess ? $"Success" : $"Failure {{ {Error} }}"; + } +} + +/// +/// A type which contains either a success value or a failure value, which is represented by an . +/// +/// The type of the success value. +[DebuggerTypeProxy(typeof(Result<>.ResultDebugProxy))] +public partial class Result : Result, IResultFactory> +{ + /// + /// Creates a new result with a success value. + /// + /// The success value. + public Result(T value) : base() + { + Value = value; + } + + /// + /// Creates a new result with a failure value. + /// + /// The error of the result. + public Result(Error error) : base(error) + { + Value = default; + } + + /// + /// Gets the success value of the result if the operation was successful. + /// + /// + /// This property contains the value of type 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. + /// + public T? Value { get; } + + /// + [MemberNotNullWhen(true, nameof(Value))] + public override bool IsSuccess => base.IsSuccess; + + /// [MemberNotNullWhen(false, nameof(Value))] - public bool IsFailure => !IsSuccess; + public override bool IsFailure => base.IsFailure; + + /// + [Pure] + static Result IResultFactory>.Failure(Error error) + { + return new Result(error); + } /// /// Gets a string representation of the result. diff --git a/src/request.result/_extensions/Extensions.Task.cs b/src/request.result/_extensions/Extensions.Task.cs index 1889b6e..8ce54b9 100644 --- a/src/request.result/_extensions/Extensions.Task.cs +++ b/src/request.result/_extensions/Extensions.Task.cs @@ -31,6 +31,21 @@ public static partial class Extensions return (await result).Map(func); } + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + // ReSharper disable once InconsistentNaming + public static async Task> TryMap(this Task> result, Func func) + { + return (await result).TryMap(func); + } + + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static async Task> MapAsync(this Task> result, Func> func) + { + return await (await result).MapAsync(func); + } + /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public static async Task> MapAsync(this Task> result, Func> func) @@ -38,6 +53,20 @@ public static partial class Extensions return await (await result).MapAsync(func); } + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static async Task> TryMapAsync(this Task> result, Func> func) + { + return await (await result).TryMapAsync(func); + } + + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static async Task> TryMapAsync(this Task> result, Func> func) + { + return await (await result).TryMapAsync(func); + } + /// /// 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. @@ -55,6 +84,21 @@ public static partial class Extensions return (await result).Then(func); } + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + // ReSharper disable once InconsistentNaming + public static async Task> ThenTry(this Task> result, Func> func) + { + return (await result).ThenTry(func); + } + + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static async Task> ThenAsync(this Task> result, Func>> func) + { + return await (await result).ThenAsync(func); + } + /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public static async Task> ThenAsync(this Task> result, Func>> func) @@ -62,6 +106,64 @@ public static partial class Extensions return await (await result).ThenAsync(func); } + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static async Task> ThenTryAsync(this Task> result, Func>> func) + { + return await (await result).ThenTryAsync(func); + } + + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static async Task> ThenTryAsync(this Task> result, Func>> func) + { + return await (await result).ThenTryAsync(func); + } + + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + // ReSharper disable once InconsistentNaming + public static async Task Then(this Task> result, Func func) + { + return (await result).Then(func); + } + + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + // ReSharper disable once InconsistentNaming + public static async Task ThenTry(this Task> result, Func func) + { + return (await result).ThenTry(func); + } + + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static async Task ThenAsync(this Task> result, Func> func) + { + return await (await result).ThenAsync(func); + } + + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static async Task ThenAsync(this Task> result, Func> func) + { + return await (await result).ThenAsync(func); + } + + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static async Task ThenTryAsync(this Task> result, Func> func) + { + return await (await result).ThenTryAsync(func); + } + + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static async Task ThenTryAsync(this Task> result, Func> func) + { + return await (await result).ThenTryAsync(func); + } + #endregion #region ValueTask> @@ -74,6 +176,21 @@ public static partial class Extensions return (await result).Map(func); } + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + // ReSharper disable once InconsistentNaming + public static async ValueTask> TryMap(this ValueTask> result, Func func) + { + return (await result).TryMap(func); + } + + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static async ValueTask> MapAsync(this ValueTask> result, Func> func) + { + return await (await result).MapAsync(func); + } + /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public static async ValueTask> MapAsync(this ValueTask> result, Func> func) @@ -81,6 +198,20 @@ public static partial class Extensions return await (await result).MapAsync(func); } + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static async ValueTask> TryMapAsync(this ValueTask> result, Func> func) + { + return await (await result).TryMapAsync(func); + } + + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static async ValueTask> TryMapAsync(this ValueTask> result, Func> func) + { + return await (await result).TryMapAsync(func); + } + /// [MethodImpl(MethodImplOptions.AggressiveInlining)] // ReSharper disable once InconsistentNaming @@ -89,6 +220,21 @@ public static partial class Extensions return (await result).Then(func); } + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + // ReSharper disable once InconsistentNaming + public static async ValueTask> ThenTry(this ValueTask> result, Func> func) + { + return (await result).ThenTry(func); + } + + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static async ValueTask> ThenAsync(this ValueTask> result, Func>> func) + { + return await (await result).ThenAsync(func); + } + /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public static async ValueTask> ThenAsync(this ValueTask> result, Func>> func) @@ -96,5 +242,358 @@ public static partial class Extensions return await (await result).ThenAsync(func); } + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static async ValueTask> ThenTryAsync(this ValueTask> result, Func>> func) + { + return await (await result).ThenTryAsync(func); + } + + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static async ValueTask> ThenTryAsync(this ValueTask> result, Func>> func) + { + return await (await result).ThenTryAsync(func); + } + + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + // ReSharper disable once InconsistentNaming + public static async ValueTask Then(this ValueTask> result, Func func) + { + return (await result).Then(func); + } + + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + // ReSharper disable once InconsistentNaming + public static async ValueTask ThenTry(this ValueTask> result, Func func) + { + return (await result).ThenTry(func); + } + + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static async ValueTask ThenAsync(this ValueTask> result, Func> func) + { + return await (await result).ThenAsync(func); + } + + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static async ValueTask ThenAsync(this ValueTask> result, Func> func) + { + return await (await result).ThenAsync(func); + } + + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static async ValueTask ThenTryAsync(this ValueTask> result, Func> func) + { + return await (await result).ThenTryAsync(func); + } + + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static async ValueTask ThenTryAsync(this ValueTask> result, Func> func) + { + return await (await result).ThenTryAsync(func); + } + + #endregion + + #region Task + + /// + /// 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. + /// + /// A task object returning a result object when completing. + /// The function used to map the success to a new value. + /// The type of the new value. + /// A new result containing either the mapped success value or the failure value of the original + /// result. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + // ReSharper disable once InconsistentNaming + public static async Task> Map(this Task result, Func func) + { + return (await result).Map(func); + } + + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + // ReSharper disable once InconsistentNaming + public static async Task> TryMap(this Task result, Func func) + { + return (await result).TryMap(func); + } + + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static async Task> MapAsync(this Task result, Func> func) + { + return await (await result).MapAsync(func); + } + + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static async Task> MapAsync(this Task result, Func> func) + { + return await (await result).MapAsync(func); + } + + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static async Task> TryMapAsync(this Task result, Func> func) + { + return await (await result).TryMapAsync(func); + } + + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static async Task> TryMapAsync(this Task result, Func> func) + { + return await (await result).TryMapAsync(func); + } + + /// + /// 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. + /// + /// A task object returning a result object when completing. + /// The function used to create the next result. + /// A result which is either the mapped result or a new result containing the failure value of the original + /// result. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + // ReSharper disable once InconsistentNaming + public static async Task Then(this Task result, Func func) + { + return (await result).Then(func); + } + + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + // ReSharper disable once InconsistentNaming + public static async Task ThenTry(this Task result, Func func) + { + return (await result).ThenTry(func); + } + + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static async Task ThenAsync(this Task result, Func> func) + { + return await (await result).ThenAsync(func); + } + + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static async Task ThenAsync(this Task result, Func> func) + { + return await (await result).ThenAsync(func); + } + + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static async Task ThenTryAsync(this Task result, Func> func) + { + return await (await result).ThenTryAsync(func); + } + + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static async Task ThenTryAsync(this Task result, Func> func) + { + return await (await result).ThenTryAsync(func); + } + + /// + /// 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. + /// + /// A task object returning a result object when completing. + /// The function used to create the next generic result. + /// The type of the new value. + /// A result which is either the mapped result or a new result containing the failure value of the original + /// result. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + // ReSharper disable once InconsistentNaming + public static async Task> Then(this Task result, Func> func) + { + return (await result).Then(func); + } + + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + // ReSharper disable once InconsistentNaming + public static async Task> ThenTry(this Task result, Func> func) + { + return (await result).ThenTry(func); + } + + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static async Task> ThenAsync(this Task result, Func>> func) + { + return await (await result).ThenAsync(func); + } + + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static async Task> ThenAsync(this Task result, Func>> func) + { + return await (await result).ThenAsync(func); + } + + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static async Task> ThenTryAsync(this Task result, Func>> func) + { + return await (await result).ThenTryAsync(func); + } + + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static async Task> ThenTryAsync(this Task result, Func>> func) + { + return await (await result).ThenTryAsync(func); + } + + #endregion + + #region ValueTask + + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + // ReSharper disable once InconsistentNaming + public static async ValueTask> Map(this ValueTask result, Func func) + { + return (await result).Map(func); + } + + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + // ReSharper disable once InconsistentNaming + public static async ValueTask> TryMap(this ValueTask result, Func func) + { + return (await result).TryMap(func); + } + + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static async ValueTask> MapAsync(this ValueTask result, Func> func) + { + return await (await result).MapAsync(func); + } + + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static async ValueTask> MapAsync(this ValueTask result, Func> func) + { + return await (await result).MapAsync(func); + } + + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static async ValueTask> TryMapAsync(this ValueTask result, Func> func) + { + return await (await result).TryMapAsync(func); + } + + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static async ValueTask> TryMapAsync(this ValueTask result, Func> func) + { + return await (await result).TryMapAsync(func); + } + + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + // ReSharper disable once InconsistentNaming + public static async ValueTask Then(this ValueTask result, Func func) + { + return (await result).Then(func); + } + + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + // ReSharper disable once InconsistentNaming + public static async ValueTask ThenTry(this ValueTask result, Func func) + { + return (await result).ThenTry(func); + } + + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static async ValueTask ThenAsync(this ValueTask result, Func> func) + { + return await (await result).ThenAsync(func); + } + + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static async ValueTask ThenAsync(this ValueTask result, Func> func) + { + return await (await result).ThenAsync(func); + } + + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static async ValueTask ThenTryAsync(this ValueTask result, Func> func) + { + return await (await result).ThenTryAsync(func); + } + + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static async ValueTask ThenTryAsync(this ValueTask result, Func> func) + { + return await (await result).ThenTryAsync(func); + } + + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + // ReSharper disable once InconsistentNaming + public static async ValueTask> Then(this ValueTask result, Func> func) + { + return (await result).Then(func); + } + + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + // ReSharper disable once InconsistentNaming + public static async ValueTask> ThenTry(this ValueTask result, Func> func) + { + return (await result).ThenTry(func); + } + + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static async ValueTask> ThenAsync(this ValueTask result, Func>> func) + { + return await (await result).ThenAsync(func); + } + + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static async ValueTask> ThenAsync(this ValueTask result, Func>> func) + { + return await (await result).ThenAsync(func); + } + + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static async ValueTask> ThenTryAsync(this ValueTask result, Func>> func) + { + return await (await result).ThenTryAsync(func); + } + + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static async ValueTask> ThenTryAsync(this ValueTask result, Func>> func) + { + return await (await result).ThenTryAsync(func); + } + #endregion }