From 3a6c4725296278af904fee1245ef739800431e51 Mon Sep 17 00:00:00 2001 From: Louis Seubert Date: Sat, 9 May 2026 19:32:23 +0200 Subject: [PATCH] feat: add generic result library --- Directory.Build.props | 2 +- request.slnx | 2 + src/Request.Result.Tests/.editorconfig | 9 + src/Request.Result.Tests/ErrorTests.cs | 29 + .../ExtensionsEnumerableTests.cs | 52 ++ .../Geekeey.Request.Result.Tests.csproj | 25 + src/Request.Result.Tests/PreludeTests.cs | 102 +++ .../ResultConversionTests.cs | 66 ++ .../ResultEqualityTests.cs | 177 +++++ .../ResultMatchingTests.cs | 234 +++++++ src/Request.Result.Tests/ResultTests.cs | 61 ++ .../ResultTransformTests.cs | 644 ++++++++++++++++++ src/Request.Result.Tests/ResultUnboxTests.cs | 101 +++ .../_fixtures/CustomTestError.cs | 13 + .../_fixtures/CustomTestException.cs | 8 + .../Geekeey.Request.Result.csproj | 30 + src/Request.Result/Prelude.cs | 101 +++ src/Request.Result/Result.Conversion.cs | 51 ++ src/Request.Result/Result.Equality.cs | 163 +++++ src/Request.Result/Result.Matching.cs | 104 +++ src/Request.Result/Result.Transform.cs | 366 ++++++++++ src/Request.Result/Result.Unbox.cs | 66 ++ src/Request.Result/Result.cs | 76 +++ src/Request.Result/_errors/AggregateError.cs | 27 + src/Request.Result/_errors/Error.cs | 54 ++ src/Request.Result/_errors/ExceptionError.cs | 29 + src/Request.Result/_errors/StringError.cs | 24 + .../_exceptions/UnwrapException.cs | 26 + .../_extensions/Extensions.Enumerable.cs | 90 +++ .../_extensions/Extensions.Task.cs | 100 +++ src/Request.Result/package-icon.png | Bin 0 -> 15556 bytes src/Request.Result/package-readme.md | 62 ++ 32 files changed, 2893 insertions(+), 1 deletion(-) create mode 100644 src/Request.Result.Tests/.editorconfig create mode 100644 src/Request.Result.Tests/ErrorTests.cs create mode 100644 src/Request.Result.Tests/ExtensionsEnumerableTests.cs create mode 100644 src/Request.Result.Tests/Geekeey.Request.Result.Tests.csproj create mode 100644 src/Request.Result.Tests/PreludeTests.cs create mode 100644 src/Request.Result.Tests/ResultConversionTests.cs create mode 100644 src/Request.Result.Tests/ResultEqualityTests.cs create mode 100644 src/Request.Result.Tests/ResultMatchingTests.cs create mode 100644 src/Request.Result.Tests/ResultTests.cs create mode 100644 src/Request.Result.Tests/ResultTransformTests.cs create mode 100644 src/Request.Result.Tests/ResultUnboxTests.cs create mode 100644 src/Request.Result.Tests/_fixtures/CustomTestError.cs create mode 100644 src/Request.Result.Tests/_fixtures/CustomTestException.cs create mode 100644 src/Request.Result/Geekeey.Request.Result.csproj create mode 100644 src/Request.Result/Prelude.cs create mode 100644 src/Request.Result/Result.Conversion.cs create mode 100644 src/Request.Result/Result.Equality.cs create mode 100644 src/Request.Result/Result.Matching.cs create mode 100644 src/Request.Result/Result.Transform.cs create mode 100644 src/Request.Result/Result.Unbox.cs create mode 100644 src/Request.Result/Result.cs create mode 100644 src/Request.Result/_errors/AggregateError.cs create mode 100644 src/Request.Result/_errors/Error.cs create mode 100644 src/Request.Result/_errors/ExceptionError.cs create mode 100644 src/Request.Result/_errors/StringError.cs create mode 100644 src/Request.Result/_exceptions/UnwrapException.cs create mode 100644 src/Request.Result/_extensions/Extensions.Enumerable.cs create mode 100644 src/Request.Result/_extensions/Extensions.Task.cs create mode 100644 src/Request.Result/package-icon.png create mode 100644 src/Request.Result/package-readme.md diff --git a/Directory.Build.props b/Directory.Build.props index 7996aa7..cf00c22 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -34,4 +34,4 @@ moderate all - \ No newline at end of file + diff --git a/request.slnx b/request.slnx index af7d750..9ec504a 100644 --- a/request.slnx +++ b/request.slnx @@ -1,4 +1,6 @@ + + diff --git a/src/Request.Result.Tests/.editorconfig b/src/Request.Result.Tests/.editorconfig new file mode 100644 index 0000000..78f1f31 --- /dev/null +++ b/src/Request.Result.Tests/.editorconfig @@ -0,0 +1,9 @@ + +[*.{cs,vb}] +# disable CA1822: Mark members as static +# -> TUnit requiring instance methods for test cases +dotnet_diagnostic.CA1822.severity = none +# disable CA1707: Identifiers should not contain underscores +dotnet_diagnostic.CA1707.severity = none +# disable IDE0060: Remove unused parameter +dotnet_diagnostic.IDE0060.severity = none diff --git a/src/Request.Result.Tests/ErrorTests.cs b/src/Request.Result.Tests/ErrorTests.cs new file mode 100644 index 0000000..01bbeed --- /dev/null +++ b/src/Request.Result.Tests/ErrorTests.cs @@ -0,0 +1,29 @@ +// Copyright (c) The Geekeey Authors +// SPDX-License-Identifier: EUPL-1.2 + +using Geekeey.Request.Result; + +namespace Request.Result.Tests; + +internal sealed class ErrorTests +{ + [Test] + public async Task I_can_implicitly_convert_from_string_and_get_string_error() + { + Error error = "error"; + + using var scope = Assert.Multiple(); + await Assert.That(error).IsTypeOf(); + await Assert.That(error.Message).IsEqualTo("error"); + } + + [Test] + public async Task I_can_implicitly_convert_from_exception_and_get_exception_error() + { + Error error = new CustomTestException(); + + using var scope = Assert.Multiple(); + var instance = await Assert.That(error).IsTypeOf(); + await Assert.That(instance?.Exception).IsTypeOf(); + } +} \ No newline at end of file diff --git a/src/Request.Result.Tests/ExtensionsEnumerableTests.cs b/src/Request.Result.Tests/ExtensionsEnumerableTests.cs new file mode 100644 index 0000000..64ac5ff --- /dev/null +++ b/src/Request.Result.Tests/ExtensionsEnumerableTests.cs @@ -0,0 +1,52 @@ +// Copyright (c) The Geekeey Authors +// SPDX-License-Identifier: EUPL-1.2 + +using Geekeey.Request.Result; + +namespace Request.Result.Tests; + +internal sealed class ExtensionsEnumerableTests +{ + [Test] + public async Task I_can_join_sequence_and_get_all_success_when_all_elements_are_success() + { + IEnumerable> xs = [1, 2, 3, 4, 5]; + + var result = xs.Join(); + + using var scope = Assert.Multiple(); + await Assert.That(result.IsSuccess).IsTrue(); + await Assert.That(result.Value).IsEquivalentTo([1, 2, 3, 4, 5]); + } + + [Test] + public async Task I_can_join_sequence_and_get_first_failure_when_sequence_contains_failure() + { + IEnumerable> xs = + [ + Prelude.Success(1), + Prelude.Success(2), + Prelude.Failure("error 1"), + Prelude.Success(4), + Prelude.Failure("error 2") + ]; + + var result = xs.Join(); + + using var scope = Assert.Multiple(); + await Assert.That(result.IsSuccess).IsFalse(); + await Assert.That(result.Error?.Message).IsEqualTo("error 1"); + } + + [Test] + public async Task I_can_join_empty_sequence_and_get_success() + { + IEnumerable> xs = []; + + var result = xs.Join(); + + using var scope = Assert.Multiple(); + await Assert.That(result.IsSuccess).IsTrue(); + await Assert.That(result.Value).IsEmpty(); + } +} \ No newline at end of file diff --git a/src/Request.Result.Tests/Geekeey.Request.Result.Tests.csproj b/src/Request.Result.Tests/Geekeey.Request.Result.Tests.csproj new file mode 100644 index 0000000..6781d57 --- /dev/null +++ b/src/Request.Result.Tests/Geekeey.Request.Result.Tests.csproj @@ -0,0 +1,25 @@ + + + + Exe + net10.0 + false + + + + Geekeey.Request.Result + + + + + + + + + + + + + + + diff --git a/src/Request.Result.Tests/PreludeTests.cs b/src/Request.Result.Tests/PreludeTests.cs new file mode 100644 index 0000000..5065395 --- /dev/null +++ b/src/Request.Result.Tests/PreludeTests.cs @@ -0,0 +1,102 @@ +// Copyright (c) The Geekeey Authors +// SPDX-License-Identifier: EUPL-1.2 + +using Geekeey.Request.Result; + +namespace Request.Result.Tests; + +internal sealed class PreludeTests +{ + [Test] + public async Task I_can_try_with_success_value_and_get_a_success_result() + { + var result = Prelude.Try(() => 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_try_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_with_async_success_value_and_get_a_success_result() + { + var result = await Prelude.TryAsync(() => Task.FromResult(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_try_with_async_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_with_async_await_throwing_exception_and_get_a_failure_result() + { + var result = await Prelude.TryAsync(async Task () => + { + 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_try_with_async_success_value_and_get_a_success_result_of_type_ValueTask() + { + var result = await Prelude.TryAsync(() => ValueTask.FromResult(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_try_with_async_throwing_exception_and_get_a_failure_result_of_type_ValueTask() + { + 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(); + } + + [Test] + public async Task I_can_try_with_async_await_throwing_exception_and_get_a_failure_result_of_type_ValueTask() + { + var result = await Prelude.TryAsync(async ValueTask () => + { + 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(); + } +} \ No newline at end of file diff --git a/src/Request.Result.Tests/ResultConversionTests.cs b/src/Request.Result.Tests/ResultConversionTests.cs new file mode 100644 index 0000000..2fea25f --- /dev/null +++ b/src/Request.Result.Tests/ResultConversionTests.cs @@ -0,0 +1,66 @@ +// Copyright (c) The Geekeey Authors +// SPDX-License-Identifier: EUPL-1.2 + +using Geekeey.Request.Result; + +namespace Request.Result.Tests; + +internal sealed class ResultConversionTests +{ + [Test] + public async Task I_can_implicitly_convert_from_value_and_get_success() + { + var result = Prelude.Success(2); + + using var scope = Assert.Multiple(); + await Assert.That(result.IsSuccess).IsTrue(); + await Assert.That(result.IsFailure).IsFalse(); + await Assert.That(result.Value).IsEqualTo(2); + } + + [Test] + public async Task I_can_implicitly_convert_from_error_and_get_failure() + { + var error = new CustomTestError(); + var result = Prelude.Failure(error); + + 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_unwrap_and_get_value_for_success() + { + var result = Prelude.Success(2); + var value = result.Unwrap(); + + await Assert.That(value).IsEqualTo(2); + } + + [Test] + public async Task I_can_unwrap_and_get_exception_for_failure() + { + var result = Prelude.Failure("error"); + + await Assert.That(result.Unwrap).Throws(); + } + + [Test] + public async Task I_can_explicitly_convert_and_get_value_for_success() + { + var result = Prelude.Success(2); + var value = (int)result; + + await Assert.That(value).IsEqualTo(2); + } + + [Test] + public async Task I_can_explicitly_convert_and_get_exception_for_failure() + { + var result = Prelude.Failure("error"); + + await Assert.That(() => (int)result).Throws(); + } +} \ No newline at end of file diff --git a/src/Request.Result.Tests/ResultEqualityTests.cs b/src/Request.Result.Tests/ResultEqualityTests.cs new file mode 100644 index 0000000..ca136be --- /dev/null +++ b/src/Request.Result.Tests/ResultEqualityTests.cs @@ -0,0 +1,177 @@ +// Copyright (c) The Geekeey Authors +// SPDX-License-Identifier: EUPL-1.2 + +using Geekeey.Request.Result; + +namespace Request.Result.Tests; + +internal sealed class ResultEqualityTests +{ + [Test] + public async Task I_can_equal_t_and_get_true_for_success_with_equal_value() + { + var a = Prelude.Success(2); + var b = 2; + + await Assert.That(a.Equals(b)).IsTrue(); + } + + [Test] + public async Task I_can_equal_t_and_get_false_for_success_with_unequal_value() + { + var a = Prelude.Success(2); + var b = 3; + + await Assert.That(a.Equals(b)).IsFalse(); + } + + [Test] + public async Task I_can_equal_t_and_get_false_for_failure() + { + var a = Prelude.Failure("error"); + var b = 2; + + await Assert.That(a.Equals(b)).IsFalse(); + } + + [Test] + public async Task I_can_equal_result_and_get_true_for_success_and_success_with_equal_value() + { + var a = Prelude.Success(2); + var b = Prelude.Success(2); + + await Assert.That(a.Equals(b)).IsTrue(); + } + + [Test] + public async Task I_can_equal_result_and_get_false_for_success_and_success_with_unequal_value() + { + var a = Prelude.Success(2); + var b = Prelude.Success(3); + + await Assert.That(a.Equals(b)).IsFalse(); + } + + [Test] + public async Task I_can_equals_result_and_get_false_for_success_and_failure() + { + var a = Prelude.Success(2); + var b = Prelude.Failure("error 1"); + + await Assert.That(a.Equals(b)).IsFalse(); + } + + [Test] + public async Task I_can_equals_result_and_get_false_for_failure_and_success() + { + var a = Prelude.Failure("error"); + var b = Prelude.Success(2); + + await Assert.That(a.Equals(b)).IsFalse(); + } + + [Test] + public async Task I_can_equals_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_t_and_get_true_for_success_with_equal_value_using_comparer() + { + var a = Prelude.Success(2); + var b = 2; + + await Assert.That(a.Equals(b, EqualityComparer.Default)).IsTrue(); + } + + [Test] + public async Task I_can_equal_t_and_get_false_for_success_with_unequal_value_using_comparer() + { + var a = Prelude.Success(2); + var b = 3; + + await Assert.That(a.Equals(b, EqualityComparer.Default)).IsFalse(); + } + + [Test] + public async Task I_can_equal_t_and_get_false_for_failure_using_comparer() + { + var a = Prelude.Failure("error"); + var b = 2; + + await Assert.That(a.Equals(b, EqualityComparer.Default)).IsFalse(); + } + + [Test] + public async Task I_can_equal_result_and_get_true_for_success_and_success_with_equal_value_using_comparer() + { + var a = Prelude.Success(2); + var b = Prelude.Success(2); + + await Assert.That(a.Equals(b, EqualityComparer.Default)).IsTrue(); + } + + [Test] + public async Task I_can_equal_result_and_get_false_for_success_and_success_with_unequal_value_using_comparer() + { + var a = Prelude.Success(2); + var b = Prelude.Success(3); + + await Assert.That(a.Equals(b, EqualityComparer.Default)).IsFalse(); + } + + [Test] + public async Task I_can_equals_result_and_get_false_for_success_and_failure_using_comparer() + { + var a = Prelude.Success(2); + var b = Prelude.Failure("error 1"); + + await Assert.That(a.Equals(b, EqualityComparer.Default)).IsFalse(); + } + + [Test] + public async Task I_can_equals_result_and_get_false_for_failure_and_success_using_comparer() + { + var a = Prelude.Failure("error"); + var b = Prelude.Success(2); + + await Assert.That(a.Equals(b, EqualityComparer.Default)).IsFalse(); + } + + [Test] + public async Task I_can_equals_result_and_get_true_for_failure_and_failure_using_comparer() + { + var a = Prelude.Failure("error 1"); + var b = Prelude.Failure("error 2"); + + await Assert.That(a.Equals(b, EqualityComparer.Default)).IsTrue(); + } + + [Test] + public async Task I_can_get_hashcode_and_get_hashcode_for_success() + { + var result = Prelude.Success(2); + + await Assert.That(result.GetHashCode()).IsEqualTo(2.GetHashCode()); + } + + [Test] + public async Task I_can_get_hashcode_and_get_zero_for_null() + { + var result = Prelude.Success(null); + + await Assert.That(result.GetHashCode()).IsZero(); + } + + [Test] + public async Task I_can_get_hashcode_and_get_zero_for_failure() + { + var result = Prelude.Failure("error"); + + await Assert.That(result.GetHashCode()).IsZero(); + } +} \ No newline at end of file diff --git a/src/Request.Result.Tests/ResultMatchingTests.cs b/src/Request.Result.Tests/ResultMatchingTests.cs new file mode 100644 index 0000000..662c3dc --- /dev/null +++ b/src/Request.Result.Tests/ResultMatchingTests.cs @@ -0,0 +1,234 @@ +// Copyright (c) The Geekeey Authors +// SPDX-License-Identifier: EUPL-1.2 + +using Geekeey.Request.Result; + +namespace Request.Result.Tests; + +internal sealed class ResultMatchingTests +{ + [Test] + public async Task I_can_match_and_it_calls_success_func_for_success() + { + var result = Prelude.Success(2); + var match = result.Match( + v => v, + _ => throw new InvalidOperationException()); + + await Assert.That(match).IsEqualTo(2); + } + + [Test] + public async Task I_can_match_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_and_it_calls_success_action_for_success() + { + var called = false; + var value = default(int); + + var result = Prelude.Success(2); + result.Switch(OnSuccess, OnFailure); + + await Assert.That(called).IsTrue(); + await Assert.That(value).IsEqualTo(2); + + return; + + void OnSuccess(int i) + { + value = i; + called = true; + } + + void OnFailure(Error e) + { + throw new InvalidOperationException(); + } + } + + [Test] + public async Task I_can_switch_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"); + + return; + + void OnSuccess(int i) + { + throw new InvalidOperationException(); + } + + void OnFailure(Error e) + { + value = e; + called = true; + } + } + + [Test] + public async Task I_can_match_async_and_it_calls_success_func_for_success() + { + var result = Prelude.Success(2); + var match = await result.MatchAsync( + Task.FromResult, + _ => throw new InvalidOperationException()); + + await Assert.That(match).IsEqualTo(2); + } + + [Test] + public async Task I_can_match_async_and_it_calls_failure_func_for_failure() + { + var result = Prelude.Failure("error"); + var match = await result.MatchAsync( + _ => throw new InvalidOperationException(), + Task.FromResult); + + await Assert.That(match.Message).IsEqualTo("error"); + } + + [Test] + public async Task I_can_switch_async_and_it_calls_success_action_for_success() + { + var called = false; + var value = default(int); + + var result = Prelude.Success(2); + await result.SwitchAsync(OnSuccess, OnFailure); + + await Assert.That(called).IsTrue(); + await Assert.That(value).IsEqualTo(2); + return; + + Task OnSuccess(int i) + { + value = i; + called = true; + return Task.CompletedTask; + } + + Task OnFailure(Error e) + { + throw new InvalidOperationException(); + } + } + + [Test] + public async Task I_can_switch_async_and_it_calls_failure_action_for_failure() + { + var called = false; + var value = default(Error); + + var result = Prelude.Failure("error"); + await result.SwitchAsync(OnSuccess, OnFailure); + + await Assert.That(called).IsTrue(); + await Assert.That(value?.Message).IsEqualTo("error"); + + return; + + Task OnSuccess(int i) + { + throw new InvalidOperationException(); + } + + Task OnFailure(Error e) + { + value = e; + called = true; + return Task.CompletedTask; + } + } + + [Test] + public async Task I_can_match_and_it_calls_success_func_for_success_ValueTask() + { + var result = Prelude.Success(2); + var match = await result.MatchAsync( + ValueTask.FromResult, + _ => throw new InvalidOperationException()); + + await Assert.That(match).IsEqualTo(2); + } + + [Test] + public async Task I_can_match_async_and_it_calls_failure_func_for_failure_ValueTask() + { + var result = Prelude.Failure("error"); + var match = await result.MatchAsync( + _ => throw new InvalidOperationException(), + ValueTask.FromResult); + + await Assert.That(match.Message).IsEqualTo("error"); + } + + [Test] + public async Task I_can_switch_async_and_it_calls_success_action_for_success_ValueTask() + { + var called = false; + var value = default(int); + + var result = Prelude.Success(2); + await result.SwitchAsync(OnSuccess, OnFailure); + + await Assert.That(called).IsTrue(); + await Assert.That(value).IsEqualTo(2); + + return; + + ValueTask OnSuccess(int i) + { + value = i; + called = true; + return ValueTask.CompletedTask; + } + + ValueTask OnFailure(Error e) + { + throw new InvalidOperationException(); + } + } + + [Test] + public async Task I_can_switch_async_and_it_calls_failure_action_for_failure_ValueTask() + { + var called = false; + var value = default(Error); + + var result = Prelude.Failure("error"); + await result.SwitchAsync(OnSuccess, OnFailure); + + await Assert.That(called).IsTrue(); + await Assert.That(value?.Message).IsEqualTo("error"); + + return; + + ValueTask OnSuccess(int i) + { + throw new InvalidOperationException(); + } + + ValueTask OnFailure(Error e) + { + value = e; + called = true; + return ValueTask.CompletedTask; + } + } +} \ No newline at end of file diff --git a/src/Request.Result.Tests/ResultTests.cs b/src/Request.Result.Tests/ResultTests.cs new file mode 100644 index 0000000..5bf2a6f --- /dev/null +++ b/src/Request.Result.Tests/ResultTests.cs @@ -0,0 +1,61 @@ +// Copyright (c) The Geekeey Authors +// SPDX-License-Identifier: EUPL-1.2 + +using Geekeey.Request.Result; + +namespace Request.Result.Tests; + +internal sealed class ResultTests +{ + [Test] + public async Task I_can_create_new_success_result_from_t() + { + var 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.Value).IsNotEqualTo(default); + await Assert.That(result.Error).IsNull(); + } + + [Test] + public async Task I_can_create_new_failure_result_from_error() + { + 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.Value).IsEqualTo(default(int)); + 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() + { + Result result = 2; + + await Assert.That(result.ToString()).IsEqualTo("Success { 2 }"); + } + + [Test] + public async Task I_can_to_string_failure_result_value() + { + Result result = new StringError("error"); + + await Assert.That(result.ToString()).IsEqualTo("Failure { error }"); + } +} \ No newline at end of file diff --git a/src/Request.Result.Tests/ResultTransformTests.cs b/src/Request.Result.Tests/ResultTransformTests.cs new file mode 100644 index 0000000..ca62f85 --- /dev/null +++ b/src/Request.Result.Tests/ResultTransformTests.cs @@ -0,0 +1,644 @@ +// Copyright (c) The Geekeey Authors +// SPDX-License-Identifier: EUPL-1.2 + +using Geekeey.Request.Result; + +namespace Request.Result.Tests; + +internal sealed class ResultTransformTests +{ + [Test] + public async Task I_can_map_and_it_returns_success_for_success() + { + var start = Prelude.Success(2); + var result = start.Map(value => value.ToString()); + + 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_and_it_returns_failure_for_failure() + { + var start = Prelude.Failure("error"); + var result = start.Map(value => value.ToString()); + + 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_transform_result_with_then_and_it_returns_success_for_success_and_mapping_returning_success() + { + var start = Prelude.Success(2); + var result = start.Then(value => Prelude.Success(value.ToString())); + + using var scope = Assert.Multiple(); + await Assert.That(result.IsSuccess).IsTrue(); + await Assert.That(result.Value).IsEqualTo("2"); + } + + [Test] + public async Task I_can_transform_result_with_then_and_it_returns_failure_for_success_and_mapping_returning_failure() + { + var start = Prelude.Success(2); + var result = start.Then(_ => 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_transform_result_with_then_and_it_returns_failure_for_failure_and_mapping_returning_success() + { + var start = Prelude.Failure("error"); + var result = start.Then(value => Prelude.Success(value.ToString())); + + 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_transform_result_with_then_and_it_returns_failure_for_failure_and_mapping_returning_failure() + { + var start = Prelude.Failure("error"); + var result = start.Then(_ => Prelude.Failure("error 2")); + + 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_and_it_returns_success_for_success_without_throwing() + { + var start = Prelude.Success(2); + var result = start.TryMap(value => value.ToString()); + + 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_and_it_returns_failure_for_failure_without_throwing() + { + var start = Prelude.Failure("error"); + var result = start.TryMap(value => value.ToString()); + + 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_and_it_returns_failure_for_success_with_throwing() + { + var start = Prelude.Success(2); + 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_try_map_and_it_returns_failure_for_failure_with_throwing() + { + var start = Prelude.Failure("error"); + var result = start.TryMap(_ => throw new CustomTestException()); + + using var scope = Assert.Multiple(); + await Assert.That(result.IsSuccess).IsFalse(); + await Assert.That(result.Error).IsTypeOf(); + await Assert.That(result.Error?.Message).IsEqualTo("error"); + } + + [Test] + public async Task I_can_try_transform_result_with_then_and_it_returns_success_for_success_and_mapping_returning_success() + { + var start = Prelude.Success(2); + var result = start.ThenTry(value => Prelude.Success(value.ToString())); + + 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_transform_result_with_then_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_with_then_and_it_returns_failure_for_failure_and_mapping_returning_failure() + { + var start = Prelude.Failure("error"); + var result = start.ThenTry(x => Prelude.Success(x.ToString())); + + 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_with_then_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_with_then_and_it_returns_failure_for_failure_and_mapping_throwing() + { + var start = Prelude.Failure("error"); + var result = start.ThenTry(_ => throw new CustomTestException()); + + using var scope = Assert.Multiple(); + await Assert.That(result.IsSuccess).IsFalse(); + await Assert.That(result.Error).IsTypeOf(); + await Assert.That(result.Error?.Message).IsEqualTo("error"); + } + + [Test] + public async Task I_can_map_async_and_it_returns_success_for_success() + { + var start = Prelude.Success(2); + var result = await start.MapAsync(value => Task.FromResult(value.ToString())); + + 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_async_and_it_returns_failure_for_failure() + { + var start = Prelude.Failure("error"); + var result = await start.MapAsync(value => Task.FromResult(value.ToString())); + + 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_transform_result_async_with_then_and_it_returns_success_for_success_and_mapping_returning_success() + { + var start = Prelude.Success(2); + var result = await start.ThenAsync(value => Task.FromResult(Prelude.Success(value.ToString()))); + + using var scope = Assert.Multiple(); + await Assert.That(result.IsSuccess).IsTrue(); + await Assert.That(result.Value).IsEqualTo("2"); + } + + [Test] + public async Task I_can_transform_result_async_with_then_and_it_returns_failure_for_success_and_mapping_returning_failure() + { + var start = Prelude.Success(2); + var result = await start.ThenAsync(_ => Task.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_transform_result_async_with_then_and_it_returns_failure_for_failure_and_mapping_returning_success() + { + var start = Prelude.Failure("error"); + var result = await start.ThenAsync(value => Task.FromResult(Prelude.Success(value.ToString()))); + + 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_transform_result_async_with_then_and_it_returns_failure_for_failure_and_mapping_returning_failure() + { + var start = Prelude.Failure("error"); + var result = await start.ThenAsync(_ => Task.FromResult(Prelude.Failure("error 2"))); + + 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_async_and_it_returns_success_for_success_without_throwing() + { + var start = Prelude.Success(2); + var result = await start.TryMapAsync(value => Task.FromResult(value.ToString())); + + 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_async_and_it_returns_failure_for_failure_without_throwing() + { + var start = Prelude.Failure("error"); + var result = await start.TryMapAsync(value => Task.FromResult(value.ToString())); + + 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_async_and_it_returns_failure_for_success_with_throwing() + { + var start = Prelude.Success(2); + var result = await start.TryMapAsync(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_map_async_and_it_returns_failure_for_success_with_await_throwing() + { + var start = Prelude.Success(2); + var result = await start.TryMapAsync(async Task (_) => + { + 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_try_map_async_and_it_returns_failure_for_failure_with_throwing() + { + var start = Prelude.Failure("error"); + var result = await start.TryMapAsync(Task (_) => throw new CustomTestException()); + + using var scope = Assert.Multiple(); + await Assert.That(result.IsSuccess).IsFalse(); + await Assert.That(result.Error).IsTypeOf(); + await Assert.That(result.Error?.Message).IsEqualTo("error"); + } + + [Test] + public async Task I_can_try_map_async_and_it_returns_failure_for_failure_with_await_throwing() + { + var start = Prelude.Failure("error"); + var result = await start.TryMapAsync(async Task (_) => + { + await Task.CompletedTask; + throw new CustomTestException(); + }); + + using var scope = Assert.Multiple(); + await Assert.That(result.IsSuccess).IsFalse(); + await Assert.That(result.Error).IsTypeOf(); + await Assert.That(result.Error?.Message).IsEqualTo("error"); + } + + [Test] + public async Task I_can_try_transform_result_async_with_then_and_it_returns_success_for_success_and_mapping_returning_success() + { + var start = Prelude.Success(2); + var result = await start.ThenTryAsync(value => Task.FromResult(Prelude.Success(value.ToString()))); + + 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_transform_result_async_with_then_and_it_returns_failure_for_success_and_mapping_returning_failure() + { + var start = Prelude.Success(2); + var result = await start.ThenTryAsync(_ => Task.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_with_then_and_it_returns_failure_for_failure_and_mapping_returning_failure() + { + var start = Prelude.Failure("error"); + var result = await start.ThenTryAsync(x => Task.FromResult(Prelude.Success(x.ToString()))); + + 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_with_then_and_it_returns_failure_for_success_and_mapping_throwing() + { + 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_with_then_and_it_returns_failure_for_success_and_mapping_await_throwing() + { + var start = Prelude.Success(2); + var result = await start.ThenTryAsync(async Task> (_) => + { + 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_try_transform_result_async_with_then_and_it_returns_failure_for_failure_and_mapping_throwing() + { + var start = Prelude.Failure("error"); + var result = await start.ThenTryAsync(Task> (_) => throw new CustomTestException()); + + using var scope = Assert.Multiple(); + await Assert.That(result.IsSuccess).IsFalse(); + await Assert.That(result.Error).IsTypeOf(); + await Assert.That(result.Error?.Message).IsEqualTo("error"); + } + + [Test] + public async Task I_can_try_transform_result_async_with_then_and_it_returns_failure_for_failure_and_mapping_await_throwing() + { + var start = Prelude.Failure("error"); + var result = await start.ThenTryAsync(async Task> (_) => + { + await Task.CompletedTask; + throw new CustomTestException(); + }); + + using var scope = Assert.Multiple(); + await Assert.That(result.IsSuccess).IsFalse(); + await Assert.That(result.Error).IsTypeOf(); + await Assert.That(result.Error?.Message).IsEqualTo("error"); + } + + [Test] + public async Task I_can_map_async_and_it_returns_success_for_success_ValueTask() + { + var start = Prelude.Success(2); + var result = await start.MapAsync(value => ValueTask.FromResult(value.ToString())); + + 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_async_and_it_returns_failure_for_failure_ValueTask() + { + var start = Prelude.Failure("error"); + var result = await start.MapAsync(value => ValueTask.FromResult(value.ToString())); + + 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_transform_result_async_with_then_and_it_returns_success_for_success_and_mapping_returning_success_ValueTask() + { + var start = Prelude.Success(2); + var result = await start.ThenAsync(value => ValueTask.FromResult(Prelude.Success(value.ToString()))); + + using var scope = Assert.Multiple(); + await Assert.That(result.IsSuccess).IsTrue(); + await Assert.That(result.Value).IsEqualTo("2"); + } + + [Test] + public async Task I_can_transform_result_async_with_then_and_it_returns_failure_for_success_and_mapping_returning_failure_ValueTask() + { + var start = Prelude.Success(2); + var result = await start.ThenAsync(_ => 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_transform_result_async_with_then_and_it_returns_failure_for_failure_and_mapping_returning_success_ValueTask() + { + var start = Prelude.Failure("error"); + var result = await start.ThenAsync(value => ValueTask.FromResult(Prelude.Success(value.ToString()))); + + 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_transform_result_async_with_then_and_it_returns_failure_for_failure_and_mapping_returning_failure_ValueTask() + { + var start = Prelude.Failure("error"); + var result = await start.ThenAsync(_ => ValueTask.FromResult(Prelude.Failure("error 2"))); + + 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_async_and_it_returns_success_for_success_without_throwing_ValueTask() + { + var start = Prelude.Success(2); + var result = await start.TryMapAsync(value => ValueTask.FromResult(value.ToString())); + + 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_async_and_it_returns_failure_for_failure_without_throwing_ValueTask() + { + var start = Prelude.Failure("error"); + var result = await start.TryMapAsync(value => ValueTask.FromResult(value.ToString())); + + 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_async_and_it_returns_failure_for_success_with_throwing_ValueTask() + { + var start = Prelude.Success(2); + var result = await start.TryMapAsync(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_map_async_and_it_returns_failure_for_success_with_await_throwing_ValueTask() + { + var start = Prelude.Success(2); + var result = await start.TryMapAsync(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_try_map_async_and_it_returns_failure_for_failure_with_throwing_ValueTask() + { + var start = Prelude.Failure("error"); + var result = await start.TryMapAsync(ValueTask (_) => throw new CustomTestException()); + + using var scope = Assert.Multiple(); + await Assert.That(result.IsSuccess).IsFalse(); + await Assert.That(result.Error).IsTypeOf(); + await Assert.That(result.Error?.Message).IsEqualTo("error"); + } + + [Test] + public async Task I_can_try_map_async_and_it_returns_failure_for_failure_with_await_throwing_ValueTask() + { + var start = Prelude.Failure("error"); + var result = await start.TryMapAsync(async ValueTask (_) => + { + await ValueTask.CompletedTask; + throw new CustomTestException(); + }); + + using var scope = Assert.Multiple(); + await Assert.That(result.IsSuccess).IsFalse(); + await Assert.That(result.Error).IsTypeOf(); + await Assert.That(result.Error?.Message).IsEqualTo("error"); + } + + [Test] + public async Task I_can_try_transform_result_async_with_then_and_it_returns_success_for_success_and_mapping_returning_success_ValueTask() + { + var start = Prelude.Success(2); + var result = await start.ThenTryAsync(value => ValueTask.FromResult(Prelude.Success(value.ToString()))); + + 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_transform_result_async_with_then_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_with_then_and_it_returns_failure_for_failure_and_mapping_returning_failure_ValueTask() + { + var start = Prelude.Failure("error"); + var result = await start.ThenTryAsync(x => ValueTask.FromResult(Prelude.Success(x.ToString()))); + + 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_with_then_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_result_async_with_then_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_try_transform_result_async_with_then_and_it_returns_failure_for_failure_and_mapping_throwing_ValueTask() + { + var start = Prelude.Failure("error"); + var result = await start.ThenTryAsync(ValueTask> (_) => throw new CustomTestException()); + + using var scope = Assert.Multiple(); + await Assert.That(result.IsSuccess).IsFalse(); + await Assert.That(result.Error).IsTypeOf(); + await Assert.That(result.Error?.Message).IsEqualTo("error"); + } + + [Test] + public async Task I_can_try_transform_result_async_with_then_and_it_returns_failure_for_failure_and_mapping_await_throwing_ValueTask() + { + var start = Prelude.Failure("error"); + var result = await start.ThenTryAsync(async ValueTask> (_) => + { + await ValueTask.CompletedTask; + throw new CustomTestException(); + }); + + using var scope = Assert.Multiple(); + await Assert.That(result.IsSuccess).IsFalse(); + await Assert.That(result.Error).IsTypeOf(); + await Assert.That(result.Error?.Message).IsEqualTo("error"); + } +} \ No newline at end of file diff --git a/src/Request.Result.Tests/ResultUnboxTests.cs b/src/Request.Result.Tests/ResultUnboxTests.cs new file mode 100644 index 0000000..6684041 --- /dev/null +++ b/src/Request.Result.Tests/ResultUnboxTests.cs @@ -0,0 +1,101 @@ +// Copyright (c) The Geekeey Authors +// SPDX-License-Identifier: EUPL-1.2 + +using Geekeey.Request.Result; + +namespace Request.Result.Tests; + +internal sealed class ResultUnboxTests +{ + [Test] + public async Task I_can_try_get_value_and_it_returns_true_and_sets_value_for_success_with_1_param() + { + var result = Prelude.Success(2); + var ok = result.TryGetValue(out int value); + + using var scope = Assert.Multiple(); + await Assert.That(ok).IsTrue(); + await Assert.That(value).IsEqualTo(2); + } + + [Test] + public async Task I_can_try_get_value_and_it_returns_false_for_failure_with_1_param() + { + var result = Prelude.Failure("error"); + var ok = result.TryGetValue(out int value); + + using var scope = Assert.Multiple(); + await Assert.That(ok).IsFalse(); + await Assert.That(value).IsEqualTo(default(int)); + } + + [Test] + public async Task I_can_try_get_value_and_it_returns_true_and_sets_value_for_success_with_2_param() + { + var result = Prelude.Success(2); + var ok = result.TryGetValue(out int value, out var error); + + using var scope = Assert.Multiple(); + await Assert.That(ok).IsTrue(); + await Assert.That(value).IsEqualTo(2); + await Assert.That(error).IsEqualTo(default(Error)); + } + + [Test] + public async Task I_can_try_get_value_and_it_returns_false_and_sets_error_for_failure_with_2_param() + { + var result = Prelude.Failure("error"); + var ok = result.TryGetValue(out int value, out var error); + + using var scope = Assert.Multiple(); + await Assert.That(ok).IsFalse(); + await Assert.That(value).IsEqualTo(default(int)); + await Assert.That(error?.Message).IsEqualTo("error"); + } + + [Test] + public async Task I_can_try_get_error_and_it_returns_true_and_sets_error_for_failure_with_1_param() + { + var result = Prelude.Failure("error"); + var ok = result.TryGetValue(out Error? error); + + using var scope = Assert.Multiple(); + await Assert.That(ok).IsTrue(); + await Assert.That(error?.Message).IsEqualTo("error"); + } + + [Test] + public async Task I_can_try_get_error_and_it_returns_false_for_success_with_1_param() + { + var result = Prelude.Success(2); + var ok = result.TryGetValue(out Error? error); + + using var scope = Assert.Multiple(); + await Assert.That(ok).IsFalse(); + await Assert.That(error).IsEqualTo(default(Error)); + } + + [Test] + public async Task I_can_try_get_error_and_it_returns_true_and_sets_error_for_failure_with_2_param() + { + var result = Prelude.Failure("error"); + var ok = result.TryGetValue(out Error? error, out var value); + + using var scope = Assert.Multiple(); + await Assert.That(ok).IsTrue(); + await Assert.That(error?.Message).IsEqualTo("error"); + await Assert.That(value).IsEqualTo(default(int)); + } + + [Test] + public async Task I_can_try_get_error_and_it_returns_false_and_sets_value_for_success_with_2_param() + { + var result = Prelude.Success(2); + var ok = result.TryGetValue(out Error? error, out var value); + + using var scope = Assert.Multiple(); + await Assert.That(ok).IsFalse(); + await Assert.That(error).IsEqualTo(default(Error)); + await Assert.That(value).IsEqualTo(2); + } +} \ No newline at end of file diff --git a/src/Request.Result.Tests/_fixtures/CustomTestError.cs b/src/Request.Result.Tests/_fixtures/CustomTestError.cs new file mode 100644 index 0000000..d9ce0dc --- /dev/null +++ b/src/Request.Result.Tests/_fixtures/CustomTestError.cs @@ -0,0 +1,13 @@ +// Copyright (c) The Geekeey Authors +// SPDX-License-Identifier: EUPL-1.2 + +using Geekeey.Request.Result; + +namespace Request.Result.Tests; + +internal sealed class CustomTestError : Error +{ + internal const string DefaultMessage = "This is a custom error for test"; + + public override string Message => DefaultMessage; +} \ No newline at end of file diff --git a/src/Request.Result.Tests/_fixtures/CustomTestException.cs b/src/Request.Result.Tests/_fixtures/CustomTestException.cs new file mode 100644 index 0000000..4274981 --- /dev/null +++ b/src/Request.Result.Tests/_fixtures/CustomTestException.cs @@ -0,0 +1,8 @@ +// Copyright (c) The Geekeey Authors +// SPDX-License-Identifier: EUPL-1.2 + +namespace Request.Result.Tests; + +internal sealed class CustomTestException : Exception +{ +} \ No newline at end of file diff --git a/src/Request.Result/Geekeey.Request.Result.csproj b/src/Request.Result/Geekeey.Request.Result.csproj new file mode 100644 index 0000000..b39f727 --- /dev/null +++ b/src/Request.Result/Geekeey.Request.Result.csproj @@ -0,0 +1,30 @@ + + + + Library + net10.0 + true + + + + true + + + + + + + + package-readme.md + package-icon.png + https://code.geekeey.de/geekeey/request/src/branch/main/src/request.result + EUPL-1.2 + + + + + + + + + diff --git a/src/Request.Result/Prelude.cs b/src/Request.Result/Prelude.cs new file mode 100644 index 0000000..cd23136 --- /dev/null +++ b/src/Request.Result/Prelude.cs @@ -0,0 +1,101 @@ +// Copyright (c) The Geekeey Authors +// SPDX-License-Identifier: EUPL-1.2 + +using System.Diagnostics.Contracts; + +namespace Geekeey.Request.Result; + +/// +/// A class containing various utility methods, a 'prelude' to the rest of the library. +/// +/// +/// This class is meant to be imported statically, e.g. using static Geekeey.Extensions.Result.Prelude;. +/// Recommended to be imported globally via a global using statement. +/// +public static class Prelude +{ + /// + /// Creates a result containing a success value. + /// + /// The type of the success value. + /// The success value to create the result from. + [Pure] + public static Result Success(T value) + { + return new Result(value); + } + + /// + /// Creates a result containing a failure value. + /// + /// The type of success value in the result. + /// The failure value to create the result from. + [Pure] + public static Result Failure(Error error) + { + return new Result(error); + } + + /// + /// Tries to execute a function and return the result. If the function throws an exception, the exception will be + /// returned wrapped in an . + /// + /// The type the function returns. + /// The function to try to execute. + /// A result containing the return value of the function or an containing the + /// exception thrown by the function. + [Pure] + public static Result Try(Func function) + { + try + { + return new Result(function()); + } + 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 type the function returns. + /// The function to try to execute. + /// A result containing the return value of the function or an containing the + /// exception thrown by the function. + [Pure] + public static async ValueTask> TryAsync(Func> function) + { + try + { + return new Result(await function()); + } + 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 type the function returns. + /// The function to try to execute. + /// A result containing the return value of the function or an containing the + /// exception thrown by the function. + [Pure] + public static async Task> TryAsync(Func> function) + { + try + { + return new Result(await function()); + } + catch (Exception exception) + { + return new Result(new ExceptionError(exception)); + } + } +} \ No newline at end of file diff --git a/src/Request.Result/Result.Conversion.cs b/src/Request.Result/Result.Conversion.cs new file mode 100644 index 0000000..6c87e3d --- /dev/null +++ b/src/Request.Result/Result.Conversion.cs @@ -0,0 +1,51 @@ +// Copyright (c) The Geekeey Authors +// SPDX-License-Identifier: EUPL-1.2 + +using System.Diagnostics.Contracts; + +namespace Geekeey.Request.Result; + +public readonly partial struct Result +{ + /// + /// Implicitly constructs a result from a success value. + /// + /// The value to construct the result from. + [Pure] + public static implicit operator Result(T value) + { + return new Result(value); + } + + /// + /// 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); + } + + /// + /// Unwraps the success value of the result. Throws an if the result is a failure. + /// + /// + /// This call is unsafe in the sense that it might intentionally throw an exception. Please only use this + /// call if the caller knows that this operation is safe, or that an exception is acceptable to be thrown. + /// + /// The success value of the result. + /// The result is not a success. + [Pure] + public T Unwrap() + { + return IsSuccess ? Value : throw new UnwrapException(); + } + + /// + [Pure] + public static explicit operator T(Result result) + { + return result.Unwrap(); + } +} \ No newline at end of file diff --git a/src/Request.Result/Result.Equality.cs b/src/Request.Result/Result.Equality.cs new file mode 100644 index 0000000..babaf81 --- /dev/null +++ b/src/Request.Result/Result.Equality.cs @@ -0,0 +1,163 @@ +// Copyright (c) The Geekeey Authors +// SPDX-License-Identifier: EUPL-1.2 + +using System.Diagnostics.CodeAnalysis; +using System.Diagnostics.Contracts; +using System.Numerics; + +namespace Geekeey.Request.Result; + +public readonly partial struct Result : IEquatable>, IEquatable +{ + /// + /// Checks whether the result is equal to another result. Results are equal if both results are success values and + /// the success values are equal, or if both results are failures. + /// + /// The result to check for equality with the current result. + [Pure] + public bool Equals(Result other) + { + return Equals(this, other, EqualityComparer.Default); + } + + /// + /// Checks whether the result is equal to another result. Results are equal if both results are success values and + /// the success values are equal, or if both results are failures. + /// + /// The result to check for equality with the current result. + /// The equality comparer to use for comparing values. + [Pure] + public bool Equals(Result other, IEqualityComparer comparer) + { + return Equals(this, other, comparer); + } + + /// + /// Checks whether the result is a success value and the success value is equal to another value. + /// + /// The value to check for equality with the success value of the result. + [Pure] + public bool Equals(T? other) + { + return Equals(this, other, EqualityComparer.Default); + } + + /// + /// Checks whether the result is a success value and the success value is equal to another value using a specified + /// equality comparer. + /// + /// The value to check for equality with the success value of the result. + /// The equality comparer to use for comparing values. + [Pure] + public bool Equals(T? other, IEqualityComparer comparer) + { + return Equals(this, other, comparer); + } + + /// + [Pure] + public override bool Equals(object? obj) + { + return (obj is T x && Equals(x)) || (obj is Result r && Equals(r)); + } + + /// + [Pure] + public override int GetHashCode() + { + return GetHashCode(this, EqualityComparer.Default); + } + + internal static bool Equals(Result a, Result b, IEqualityComparer comparer) + { + if (!a.IsSuccess || !b.IsSuccess) + { + return !a.IsSuccess && !b.IsSuccess; + } + + if (a.Value is null || b.Value is null) + { + return a.Value is null && b.Value is null; + } + + return comparer.Equals(a.Value, b.Value); + } + + internal static bool Equals(Result a, T? b, IEqualityComparer comparer) + { + if (!a.IsSuccess) + { + return false; + } + + if (a.Value is null || b is null) + { + return a.Value is null && b is null; + } + + return comparer.Equals(a.Value, b); + } + + internal static int GetHashCode(Result result, IEqualityComparer comparer) + { + if (result is { IsSuccess: true, Value: not null }) + { + return comparer.GetHashCode(result.Value); + } + + return 0; + } +} + +public readonly partial struct 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 + /// values are equal, or if both results are failures. + /// + /// The first result to compare. + /// The second result to compare. + [Pure] + [ExcludeFromCodeCoverage] + public static bool operator ==(Result a, Result b) + { + return Equals(a, b, EqualityComparer.Default); + } + + /// + /// Checks whether two results are not equal. Results are equal if both results are success values and the success + /// values are equal, or if both results are failures. + /// + /// The first result to compare. + /// The second result to compare. + [Pure] + [ExcludeFromCodeCoverage] + public static bool operator !=(Result a, Result b) + { + return !Equals(a, b, EqualityComparer.Default); + } + + /// + /// Checks whether a result is a success value and the success value is equal to another value. + /// + /// The result to compare. + /// The value to check for equality with the success value in the result. + [Pure] + [ExcludeFromCodeCoverage] + public static bool operator ==(Result a, T? b) + { + return Equals(a, b, EqualityComparer.Default); + } + + /// + /// Checks whether a result either does not have a value, or the value is not equal to another value. + /// + /// The result to compare. + /// The value to check for inequality with the success value in the result. + [Pure] + [ExcludeFromCodeCoverage] + public static bool operator !=(Result a, T? b) + { + return !Equals(a, b, EqualityComparer.Default); + } +} \ No newline at end of file diff --git a/src/Request.Result/Result.Matching.cs b/src/Request.Result/Result.Matching.cs new file mode 100644 index 0000000..9520111 --- /dev/null +++ b/src/Request.Result/Result.Matching.cs @@ -0,0 +1,104 @@ +// Copyright (c) The Geekeey Authors +// SPDX-License-Identifier: EUPL-1.2 + +using System.Diagnostics.Contracts; + +namespace Geekeey.Request.Result; + +public readonly partial struct Result +{ + /// + /// Matches over the success value or failure value of the result and returns another value. Can be conceptualized + /// as an exhaustive switch expression matching all possible values of the type. + /// + /// The type to return from the match. + /// The function to apply to the success value of the result 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 success + /// value or failure value of the result. + [Pure] + public TResult Match(Func success, Func failure) + { + return IsSuccess ? success(Value!) : failure(Error!); + } + + /// + /// Matches over the success value or failure value of the result and invokes an effectful action onto the success + /// value or failure value. Can be conceptualized as an exhaustive switch statement matching all possible + /// values of the type. + /// + /// The function to call with the success value of the result 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(Value!); + } + else + { + failure(Error!); + } + } +} + +public readonly partial struct Result +{ + /// + /// Asynchronously matches over the success value or failure value of the result and returns another value. Can be + /// conceptualized as an exhaustive switch expression matching all possible values of the type. + /// + /// The type to return from the match. + /// The function to apply to the success value of the result 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 success value or failure value of the result. + [Pure] + public async Task MatchAsync(Func> success, Func> failure) + { + return IsSuccess ? await success(Value!) : await failure(Error!); + } + + /// + /// Asynchronously matches over the success value or failure value of the result and invokes an effectful action + /// onto the success value or failure value. Can be conceptualized as an exhaustive switch statement matching + /// all possible values of + /// the type. + /// + /// The function to call with the success value of the result 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(Value!) : failure(Error!); + } +} + +public readonly partial struct Result +{ + /// + /// Asynchronously matches over the success value or failure value of the result and returns another value. Can be + /// conceptualized as an exhaustive switch expression matching all possible values of the type. + /// + /// The type to return from the match. + /// The function to apply to the success value of the result 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 success value or failure value of the result. + [Pure] + public ValueTask MatchAsync(Func> success, Func> failure) + { + return IsSuccess ? success(Value!) : failure(Error!); + } + + /// + /// Asynchronously matches over the success value or failure value of the result and invokes an effectful action + /// onto the success value or the failure value. Can be conceptualized as an exhaustive switch statement + /// matching all possible values of the type. + /// + /// The function to call with the success value of the result 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(Value!) : failure(Error!); + } +} \ No newline at end of file diff --git a/src/Request.Result/Result.Transform.cs b/src/Request.Result/Result.Transform.cs new file mode 100644 index 0000000..f9d1378 --- /dev/null +++ b/src/Request.Result/Result.Transform.cs @@ -0,0 +1,366 @@ +// Copyright (c) The Geekeey Authors +// SPDX-License-Identifier: EUPL-1.2 + +using System.Diagnostics.Contracts; + +namespace Geekeey.Request.Result; + +public readonly partial struct Result +{ + /// + /// Maps the success value of the result using a mapping function, or does nothing if the result is a failure. + /// + /// The function used to map the success 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(Value!)) : new Result(Error!); + } + + /// + /// Tries to map the success value of the result 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 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)); + } + } + + /// + /// Maps the success value of the result to a new result using a mapping function, or does nothing if the result is + /// a failure. + /// + /// The function used to map the success value to a new 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(Value!) : new Result(Error!); + } + + /// + /// Tries to map the success value of the result to a new result 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 value to a new 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 readonly partial struct Result +{ + /// + /// Maps the success value of the result using an asynchronous mapping function, or does nothing if the result is + /// a failure. + /// + /// The function used to map the success value. + /// The type of the new value. + /// A which either completes asynchronously by invoking the mapping function on + /// the success value of the result 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(Value!); + return CreateResult(task); + + static async Task> CreateResult(Task task) + { + var value = await task; + return new Result(value); + } + } + + /// + /// Maps the success value 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 value. + /// The type of the new value. + /// A which either completes asynchronously by invoking the mapping function on + /// the success value of the result 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(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 new Result(value); + } + catch (Exception exception) + { + return new Result(new ExceptionError(exception)); + } + } + } + + /// + /// Maps the success value of the result to a new result using an asynchronous mapping function, or does nothing if + /// the result is a failure. + /// + /// The function used to map the success value to a new result. + /// The type of the new value. + /// A which either completes asynchronously by invoking the mapping function on + /// the success value of the result, 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; + } + } + + /// + /// Maps the success value of the result to a new 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 value to a new result. + /// The type of the new value. + /// A which either completes asynchronously by invoking the mapping function on + /// the success value of the result, 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 readonly partial struct Result +{ + /// + /// Maps the success value of the result using an asynchronous mapping function, or does nothing if the result is + /// a failure. + /// + /// The function used to map the success value. + /// The type of the new value. + /// A which either completes asynchronously by invoking the mapping function on + /// the success value of the result 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(Value!); + return CreateResult(task); + + static async ValueTask> CreateResult(ValueTask task) + { + var value = await task; + return new Result(value); + } + } + + /// + /// Maps the success value 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 value. + /// The type of the new value. + /// A which either completes asynchronously by invoking the mapping function on + /// the success value of the result 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(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 new Result(value); + } + catch (Exception exception) + { + return new Result(new ExceptionError(exception)); + } + } + } + + /// + /// Maps the success value of the result to a new result using an asynchronous mapping function, or does nothing if + /// the result is a failure. + /// + /// The function used to map the success value to a new result. + /// The type of the new value. + /// A which either completes asynchronously by invoking the mapping function on + /// the success value of the result, 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; + } + } + + /// + /// Maps the success value of the result to a new 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 value to a new result. + /// The type of the new value. + /// A which either completes asynchronously by invoking the mapping function on + /// the success value of the result, 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)); + } + } + } +} \ No newline at end of file diff --git a/src/Request.Result/Result.Unbox.cs b/src/Request.Result/Result.Unbox.cs new file mode 100644 index 0000000..7080669 --- /dev/null +++ b/src/Request.Result/Result.Unbox.cs @@ -0,0 +1,66 @@ +// Copyright (c) The Geekeey Authors +// SPDX-License-Identifier: EUPL-1.2 + +using System.Diagnostics.CodeAnalysis; +using System.Diagnostics.Contracts; + +namespace Geekeey.Request.Result; + +public readonly partial struct Result +{ + /// + /// Tries to get the success value from the result. + /// + /// The success value of the result. + /// Whether the result has success value. + [Pure] + public bool TryGetValue([MaybeNullWhen(false)] out T value) + { + value = Value; + + return IsSuccess; + } + + /// + /// Tries to get the success value from the result. + /// + /// The success value of the result. + /// The failure value of the result. + /// Whether the result has a success value. + [Pure] + public bool TryGetValue([MaybeNullWhen(false)] out T value, [MaybeNullWhen(true)] out Error error) + { + value = Value; + error = !IsSuccess ? Error : null; + + return IsSuccess; + } + + /// + /// Tries to get the failure value from the result. + /// + /// The failure value of the result. + /// Whether the result has a failure value. + [Pure] + public bool TryGetValue([MaybeNullWhen(false)] out Error error) + { + error = !IsSuccess ? Error : null; + + return !IsSuccess; + } + + /// + /// Tries to get the failure value from the result. + /// + /// The failure value of the result. + /// The success value of the result. + /// Whether the result a failure value. + [Pure] + public bool TryGetValue([MaybeNullWhen(false)] out Error error, [MaybeNullWhen(true)] out T value) + { + error = !IsSuccess ? Error : null; + value = Value; + + return !IsSuccess; + } +} \ No newline at end of file diff --git a/src/Request.Result/Result.cs b/src/Request.Result/Result.cs new file mode 100644 index 0000000..711cb8b --- /dev/null +++ b/src/Request.Result/Result.cs @@ -0,0 +1,76 @@ +// Copyright (c) The Geekeey Authors +// SPDX-License-Identifier: EUPL-1.2 + +using System.Diagnostics; +using System.Diagnostics.CodeAnalysis; +using System.Diagnostics.Contracts; + +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 +{ + /// + /// Creates a new result with a success value. + /// + /// The success value. + public Result(T value) + { + IsSuccess = true; + Value = value; + Error = default; + } + + /// + /// Creates a new result with a failure value. + /// + /// 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); + + /// + /// Whether the result is a success. + /// + /// + /// This is always the inverse of but is more specific about intent. + /// + [MemberNotNullWhen(true, nameof(Value))] + public bool IsSuccess { get; } + + /// + /// Whether the result is a failure. + /// + /// + /// This is always the inverse of but is more specific about intent. + /// + [MemberNotNullWhen(true, nameof(Error))] + public bool IsFailure => !IsSuccess; + + /// + /// Gets a string representation of the result. + /// + [Pure] + public override string ToString() + { + return IsSuccess ? $"Success {{ {Value} }}" : $"Failure {{ {Error} }}"; + } + + private sealed class ResultDebugProxy(Result result) + { + public bool IsSuccess => result.IsSuccess; + + public object? Value => result.IsSuccess ? result.Value : result.Error; + } +} diff --git a/src/Request.Result/_errors/AggregateError.cs b/src/Request.Result/_errors/AggregateError.cs new file mode 100644 index 0000000..df7dd0c --- /dev/null +++ b/src/Request.Result/_errors/AggregateError.cs @@ -0,0 +1,27 @@ +// Copyright (c) The Geekeey Authors +// SPDX-License-Identifier: EUPL-1.2 + +namespace Geekeey.Request.Result; + +/// +/// An error which is a combination of other errors. +/// +public sealed class AggregateError : Error +{ + /// + /// An error which is a combination of other errors. + /// + /// The errors the error consists of. + public AggregateError(IEnumerable errors) + { + Errors = [.. errors]; + } + + /// + /// The errors the error consists of. + /// + public IReadOnlyCollection Errors { get; } + + /// + public override string Message => string.Join(Environment.NewLine, Errors.Select(error => error.Message)); +} \ No newline at end of file diff --git a/src/Request.Result/_errors/Error.cs b/src/Request.Result/_errors/Error.cs new file mode 100644 index 0000000..d57e8ce --- /dev/null +++ b/src/Request.Result/_errors/Error.cs @@ -0,0 +1,54 @@ +// Copyright (c) The Geekeey Authors +// SPDX-License-Identifier: EUPL-1.2 + +namespace Geekeey.Request.Result; + +/// +/// An error containing a simple message. Makes up the other half of a which might be an error. +/// +/// +/// An error is conceptually very similar to an exception but without the ability to be thrown, meant to be a more +/// lightweight type meant to be wrapped in a . +/// An error fundamentally only contains a single string message, however other more concrete types such as +/// or may define other properties. +/// Errors are meant to be small, specific, and descriptive, such that they are easy to match over and provide specific +/// handling for specific kinds of errors. +/// +public abstract class Error +{ + /// + /// A statically accessible default "Result has no value." error. + /// + internal static Error DefaultValueError { get; } = new StringError("The result has no value."); + + /// + /// The message used to display the error. + /// + public abstract string Message { get; } + + /// + /// Gets a string representation of the error. Returns by default. + /// + public override string ToString() + { + return Message; + } + + /// + /// Implicitly converts a string into a . + /// + /// The message of the error. + public static implicit operator Error(string message) + { + return new StringError(message); + } + + /// + /// Implicitly converts an exception into an . + /// + /// The exception to convert. + public static implicit operator Error(Exception exception) + { + return new ExceptionError(exception); + } +} \ No newline at end of file diff --git a/src/Request.Result/_errors/ExceptionError.cs b/src/Request.Result/_errors/ExceptionError.cs new file mode 100644 index 0000000..a15aa06 --- /dev/null +++ b/src/Request.Result/_errors/ExceptionError.cs @@ -0,0 +1,29 @@ +// Copyright (c) The Geekeey Authors +// SPDX-License-Identifier: EUPL-1.2 + +namespace Geekeey.Request.Result; + +/// +/// An error which is constructed from an exception. +/// +public sealed class ExceptionError : Error +{ + /// + /// An error which is constructed from an exception. + /// + /// The exception in the error. + public ExceptionError(Exception exception) + { + Exception = exception; + } + + /// + /// The exception in the error. + /// + public Exception Exception { get; } + + /// + /// The exception in the error. + /// + public override string Message => Exception.Message; +} \ No newline at end of file diff --git a/src/Request.Result/_errors/StringError.cs b/src/Request.Result/_errors/StringError.cs new file mode 100644 index 0000000..d544add --- /dev/null +++ b/src/Request.Result/_errors/StringError.cs @@ -0,0 +1,24 @@ +// Copyright (c) The Geekeey Authors +// SPDX-License-Identifier: EUPL-1.2 + +namespace Geekeey.Request.Result; + +/// +/// An error which displays a simple string. +/// +public sealed class StringError : Error +{ + private readonly string _message; + + /// + /// An error which displays a simple string. + /// + /// The message to display. + public StringError(string message) + { + _message = message; + } + + /// + public override string Message => _message; +} \ No newline at end of file diff --git a/src/Request.Result/_exceptions/UnwrapException.cs b/src/Request.Result/_exceptions/UnwrapException.cs new file mode 100644 index 0000000..ad246d6 --- /dev/null +++ b/src/Request.Result/_exceptions/UnwrapException.cs @@ -0,0 +1,26 @@ +// Copyright (c) The Geekeey Authors +// SPDX-License-Identifier: EUPL-1.2 + +namespace Geekeey.Request.Result; + +/// +/// The exception is thrown when an is attempted to be unwrapped contains only a failure value. +/// +public sealed class UnwrapException : Exception +{ + /// + /// Creates a new . + /// + public UnwrapException() + : base("Cannot unwrap result because it does not have a value.") + { + } + + /// + /// Creates a new . + /// + /// An error message. + public UnwrapException(string error) : base(error) + { + } +} \ No newline at end of file diff --git a/src/Request.Result/_extensions/Extensions.Enumerable.cs b/src/Request.Result/_extensions/Extensions.Enumerable.cs new file mode 100644 index 0000000..9551907 --- /dev/null +++ b/src/Request.Result/_extensions/Extensions.Enumerable.cs @@ -0,0 +1,90 @@ +// Copyright (c) The Geekeey Authors +// SPDX-License-Identifier: EUPL-1.2 + +namespace Geekeey.Request.Result; + +/// +/// Extensions for or relating to . +/// +public static partial class Extensions +{ + /// + /// Turns a sequence of results into a single result containing the success values in the results only if all the + /// results have success values. + /// + /// The results to turn into a single sequence. + /// The type of the success values in the results. + /// A single result containing a sequence of all the success values from the original sequence of results, + /// or the first failure value encountered within the sequence. + /// + /// This method completely enumerates the input sequence before returning and is not lazy. As a consequence of this, + /// the sequence within the returned result is an . + /// + public static Result> Join(this IEnumerable> results) + { + _ = results.TryGetNonEnumeratedCount(out var count); + var list = new List(count); + + foreach (var result in results) + { + if (!result.TryGetValue(out T? value, out var error)) + { + return new Result>(error); + } + + list.Add(value); + } + + return list; + } + + /// + /// + /// For parallel execution of the async tasks, one should await the Task.WhenAll() of the provided list + /// before calling this function + /// + /// + // ReSharper disable once InconsistentNaming + public static async ValueTask>> Join(this IEnumerable>> results) + { + _ = results.TryGetNonEnumeratedCount(out var count); + var list = new List(count); + + foreach (var result in results) + { + if (!(await result).TryGetValue(out T? value, out var error)) + { + return new Result>(error); + } + + list.Add(value); + } + + return list; + } + + /// + /// + /// For parallel execution of the async tasks, one should await the Task.WhenAll() of the provided list + /// before calling this function + /// + /// + // ReSharper disable once InconsistentNaming + public static async Task>> Join(this IEnumerable>> results) + { + _ = results.TryGetNonEnumeratedCount(out var count); + var list = new List(count); + + foreach (var result in results) + { + if (!(await result).TryGetValue(out T? value, out var error)) + { + return new Result>(error); + } + + list.Add(value); + } + + return list; + } +} \ No newline at end of file diff --git a/src/Request.Result/_extensions/Extensions.Task.cs b/src/Request.Result/_extensions/Extensions.Task.cs new file mode 100644 index 0000000..3ef19b2 --- /dev/null +++ b/src/Request.Result/_extensions/Extensions.Task.cs @@ -0,0 +1,100 @@ +// Copyright (c) The Geekeey Authors +// SPDX-License-Identifier: EUPL-1.2 + +using System.Diagnostics.CodeAnalysis; +using System.Runtime.CompilerServices; + +namespace Geekeey.Request.Result; + +/// +/// Extensions for or relating to . +/// +[ExcludeFromCodeCoverage] +public static partial class Extensions +{ + #region Task> + + /// + /// Maps the success value of the result object of the completed task 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 value. + /// The type of the object inside the result returned by the task. + /// 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)] + public static async Task> MapAsync(this Task> result, Func> func) + { + return await (await result).MapAsync(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. + /// + /// A task object returning a result object when completing. + /// The function used to map the success value. + /// The type of the object inside the result returned by the task. + /// 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> Then(this Task> result, Func> func) + { + return (await result).Then(func); + } + + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static async Task> ThenAsync(this Task> result, Func>> func) + { + return await (await result).ThenAsync(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)] + public static async ValueTask> MapAsync(this ValueTask> result, Func> func) + { + return await (await result).MapAsync(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)] + public static async ValueTask> ThenAsync(this ValueTask> result, Func>> func) + { + return await (await result).ThenAsync(func); + } + + #endregion +} \ No newline at end of file diff --git a/src/Request.Result/package-icon.png b/src/Request.Result/package-icon.png new file mode 100644 index 0000000000000000000000000000000000000000..35f4099b1fc44cc33936dcc321249cfab4de8c52 GIT binary patch literal 15556 zcmZ8|Wl$W>59l3o9PaM!aJai`arffx?p7%7P~4?hp%jNhin~K`FD}LH{r>O6`|xHb zn@M(db|=YXce6>XnyMTs5)l#r06G1TE0j#aIO=dAim{>$DNM7e zO(p1m2b+?^nc$G&&cwG$)oK7TjCL|*=qoc{3BF-qNhZ^kQSv<$SYLK8<$aES7|!#k zYnkp2ONN7FH3#?J2?aGTlX!4%Jr41Obvs7$if9Uk|xFq)_!m#i~1&t_Z*?m1^ zxN>c?w9CmP&?9(+H{=Vk(FgKWz2DxmbMQ^}F&h~++z|GdIGM()`Ut>@L ztJV45t+)S9lhAoSdfWW;@n)%LtR(cY;~^;cj{Y)n=WYvU!y^+w((L+_yogQs| zxhhw92bDZz!$h869~A&dC2K27JH)Tw+7$HOU*Sz8Q&|x{0pv9m(ob^jypEfE{D^bz zeQ5jCkD=C}+w*=+QupHLvGcT0%fr2z^f!zPlNSxxWj^kGb1gI6`&0g3;)m;Wz=l2L zPR4ZW{E^UgWyR)4+-LunPr`|Z0prg+|Jj}I==We&X|LYDl?Vkz<{Q_ZH`m1TIs8cvZ8;*NJbx|&u1fDO z4@gbx{@Yr}1NQV+s>;{a-+>?BeSY2lxjz(4B)e|g0XMe%P5E~f%xxn!u)a24@cy8? zwVT{%B#qbXK@wApC%hvXb|p*i@NuBR-V_ut{-S*kxOMiX07_PX_;L_jt1!UwGHBr^kOwddc@|_CC#h_fe8-c$iL%dmOAr&sHHchiUv0 zdi6jNq#7ZEo=IL5N^bl&cI zv2lp30B++l0*2z572L{Ubh#+Ql5rp?K3Ejh;*=D+12DjXAJg(ngt@#ad_s!sJoNY? z9|%iF0Sen$ZSQ}k-$VOVOtH+T|7>Ee76_62x#n+zUvETRZ$vC(HT#TcaJk-^OybKk z*Jt{+mo;s}cjt2aZ*A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FxtV*vFZoQ@CHP%$)x{ zKd)>OJT{Lul>4mgq6unTH;@$=V#lC#-%j9kNy9Hgks$a(A@GG#V!Zjopuok1gH~b} zG}}k7q<^!~=`I{Jd#_!)u%EG9$rwlLYLU!7KilK#uC!}YtA@qT?|Wl5J(FIP=^sSe zl2>LxpTqJEOagR3J;aK~r2q{Kz=NdIc-JgQHSx~chC;$|UGSHB^|{AOE5**obIWPg zhw633?lh%w!YdUr$ipPWnP{(xs})MeCK-#21~Y42dVe5Ic{uQI!?4JzX*62FR>>`? z<+M|CrVosnZx*q%tFpTp%QtUon`1>F9^wokO#?OUw`(M2> OKtV=Tx?a*O{Qm&xB7La< literal 0 HcmV?d00001 diff --git a/src/Request.Result/package-readme.md b/src/Request.Result/package-readme.md new file mode 100644 index 0000000..e9f3167 --- /dev/null +++ b/src/Request.Result/package-readme.md @@ -0,0 +1,62 @@ +Result is a simple yet powerful [result type](https://doc.rust-lang.org/std/result/) implementation for C#, containing a +variety of utilities and standard functions for working with result types and integrating them into the rest of C#. + +## Features + +- **Success and Failure States:** Represent successful outcomes with a value (`Prelude.Success()`) or failures with an + error (`Prelude.Failure()`). +- **Immutability:** `Result` objects are immutable, ensuring thread safety and preventing accidental modification. +- **Chaining Operations:** Methods like `Map` and `Then` allow for chaining operations on successful results, promoting + a functional programming style. + +## Getting Started + +### Install the NuGet package: + +``` +dotnet add package Geekeey.Extensions.Result +``` + +You may need to add our NuGet Feed to your `nuget.config` this can be done by adding the following lines + +```xml + + + +``` + +### Usage + +```csharp +public Result Divide(int dividend, int divisor) +{ + if (divisor == 0) + { + return Prelude.Failure("Division by zero"); + } + + return Prelude.Success(dividend / divisor); +} + +if (result.IsSuccess) +{ + Console.WriteLine("Result: " + result.Value); +} +else +{ + Console.WriteLine("Error: " + result.Error); +} +``` + +```csharp +_ = await Prelude.Try(() => File.ReadAllLines("i_do_not_exist.txt")) + ThenAsync(static async Task>> (list) => + { + using var client = new HttpClient(); + Task> DoSomeThing(string line) + => Prelude.TryAsync(() => client.GetAsync(line)) + .Map(static async response => int.Parse(await response.Content.ReadAsStringAsync())); + var results = await Task.WhenAll(list.Select(DoSomeThing).ToArray()); + return results.Join(); + }); +```