From efc3054bbf714e9f4858e01f697e028ecfdc28bb Mon Sep 17 00:00:00 2001 From: Marcin Jahn <10273406+marcinjahn@users.noreply.github.com> Date: Fri, 8 Nov 2024 19:26:56 +0100 Subject: [PATCH 1/3] feat: add lazy overload of ToResult --- .../Extensions/ToResultTests.Task.Left.cs | 128 ++++++++++------- .../Extensions/ToResultTests.Task.cs | 128 ++++++++++------- .../ToResultTests.ValueTask.Left.cs | 130 ++++++++++-------- .../Extensions/ToResultTests.ValueTask.cs | 130 ++++++++++-------- .../MaybeTests/Extensions/ToResultTests.cs | 127 ++++++++++------- .../Maybe/Extensions/ToResult.Task.cs | 45 +++--- .../Maybe/Extensions/ToResult.ValueTask.cs | 50 ++++--- .../Maybe/Extensions/ToResult.cs | 52 ++++--- 8 files changed, 461 insertions(+), 329 deletions(-) diff --git a/CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/ToResultTests.Task.Left.cs b/CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/ToResultTests.Task.Left.cs index 15e423af..a6a1cf7a 100644 --- a/CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/ToResultTests.Task.Left.cs +++ b/CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/ToResultTests.Task.Left.cs @@ -1,53 +1,75 @@ -using System.Threading.Tasks; -using FluentAssertions; -using Xunit; - -namespace CSharpFunctionalExtensions.Tests.MaybeTests.Extensions -{ - public class ToResultTests_Task_Left : MaybeTestBase - { - [Fact] - public async Task ToResult_Task_Left_returns_failure_if_has_no_value() - { - Maybe maybe = null; - - var result = await maybe.AsTask().ToResult("Error"); - - result.IsSuccess.Should().BeFalse(); - result.Error.Should().Be("Error"); - } - - [Fact] - public async Task ToResult_Task_Left_returns_success_if_has_value() - { - var maybe = Maybe.From(T.Value); - - var result = await maybe.AsTask().ToResult("Error"); - - result.IsSuccess.Should().BeTrue(); - result.Value.Should().Be(T.Value); - } - - [Fact] - public async Task ToResult_Task_Left_returns_custom_failure_if_has_no_value() - { - Maybe maybe = null; - - var result = await maybe.AsTask().ToResult(E.Value); - - result.IsSuccess.Should().BeFalse(); - result.Error.Should().Be(E.Value); - } - - [Fact] - public async Task ToResult_Task_Left_custom_failure_returns_success_if_has_value() - { - var maybe = Maybe.From(T.Value); - - var result = await maybe.AsTask().ToResult(E.Value); - - result.IsSuccess.Should().BeTrue(); - result.Value.Should().Be(T.Value); - } - } -} \ No newline at end of file +using System.Threading.Tasks; +using FluentAssertions; +using Xunit; + +namespace CSharpFunctionalExtensions.Tests.MaybeTests.Extensions +{ + public class ToResultTests_Task_Left : MaybeTestBase + { + [Fact] + public async Task ToResult_Task_Left_returns_failure_if_has_no_value() + { + Maybe maybe = null; + + var result = await maybe.AsTask().ToResult("Error"); + + result.IsSuccess.Should().BeFalse(); + result.Error.Should().Be("Error"); + } + + [Fact] + public async Task ToResult_Task_Left_returns_success_if_has_value() + { + var maybe = Maybe.From(T.Value); + + var result = await maybe.AsTask().ToResult("Error"); + + result.IsSuccess.Should().BeTrue(); + result.Value.Should().Be(T.Value); + } + + [Fact] + public async Task ToResult_Task_Left_returns_custom_failure_if_has_no_value() + { + Maybe maybe = null; + + var result = await maybe.AsTask().ToResult(E.Value); + + result.IsSuccess.Should().BeFalse(); + result.Error.Should().Be(E.Value); + } + + [Fact] + public async Task ToResult_Task_Left_custom_failure_returns_success_if_has_value() + { + var maybe = Maybe.From(T.Value); + + var result = await maybe.AsTask().ToResult(E.Value); + + result.IsSuccess.Should().BeTrue(); + result.Value.Should().Be(T.Value); + } + + [Fact] + public async Task ToResult_Task_Left_returns_custom_failure_via_error_function_if_has_no_value() + { + Maybe maybe = null; + + var result = await maybe.AsTask().ToResult(() => E.Value); + + result.IsSuccess.Should().BeFalse(); + result.Error.Should().Be(E.Value); + } + + [Fact] + public async Task ToResult_Task_Left_custom_failure_with_error_function_returns_success_if_has_value() + { + var maybe = Maybe.From(T.Value); + + var result = await maybe.AsTask().ToResult(() => E.Value); + + result.IsSuccess.Should().BeTrue(); + result.Value.Should().Be(T.Value); + } + } +} diff --git a/CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/ToResultTests.Task.cs b/CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/ToResultTests.Task.cs index ae9fc147..a2a410ae 100644 --- a/CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/ToResultTests.Task.cs +++ b/CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/ToResultTests.Task.cs @@ -1,53 +1,75 @@ -using System.Threading.Tasks; -using FluentAssertions; -using Xunit; - -namespace CSharpFunctionalExtensions.Tests.MaybeTests.Extensions -{ - public class ToResultTests_Task : MaybeTestBase - { - [Fact] - public async Task ToResult_Task_returns_failure_if_has_no_value() - { - Maybe maybe = null; - - var result = await maybe.AsTask().ToResult("Error"); - - result.IsSuccess.Should().BeFalse(); - result.Error.Should().Be("Error"); - } - - [Fact] - public async Task ToResult_Task_returns_success_if_has_value() - { - var maybe = Maybe.From(T.Value); - - var result = await maybe.AsTask().ToResult("Error".AsTask()); - - result.IsSuccess.Should().BeTrue(); - result.Value.Should().Be(T.Value); - } - - [Fact] - public async Task ToResult_Task_returns_custom_failure_if_has_no_value() - { - Maybe maybe = null; - - var result = await maybe.AsTask().ToResult(E.Value); - - result.IsSuccess.Should().BeFalse(); - result.Error.Should().Be(E.Value); - } - - [Fact] - public async Task ToResult_Task_custom_failure_returns_success_if_has_value() - { - var maybe = Maybe.From(T.Value); - - var result = await maybe.AsTask().ToResult(E.Value.AsTask()); - - result.IsSuccess.Should().BeTrue(); - result.Value.Should().Be(T.Value); - } - } -} \ No newline at end of file +using System.Threading.Tasks; +using FluentAssertions; +using Xunit; + +namespace CSharpFunctionalExtensions.Tests.MaybeTests.Extensions +{ + public class ToResultTests_Task : MaybeTestBase + { + [Fact] + public async Task ToResult_Task_returns_failure_if_has_no_value() + { + Maybe maybe = null; + + var result = await maybe.AsTask().ToResult("Error"); + + result.IsSuccess.Should().BeFalse(); + result.Error.Should().Be("Error"); + } + + [Fact] + public async Task ToResult_Task_returns_success_if_has_value() + { + var maybe = Maybe.From(T.Value); + + var result = await maybe.AsTask().ToResult("Error".AsTask()); + + result.IsSuccess.Should().BeTrue(); + result.Value.Should().Be(T.Value); + } + + [Fact] + public async Task ToResult_Task_returns_custom_failure_if_has_no_value() + { + Maybe maybe = null; + + var result = await maybe.AsTask().ToResult(E.Value); + + result.IsSuccess.Should().BeFalse(); + result.Error.Should().Be(E.Value); + } + + [Fact] + public async Task ToResult_Task_custom_failure_returns_success_if_has_value() + { + var maybe = Maybe.From(T.Value); + + var result = await maybe.AsTask().ToResult(E.Value); + + result.IsSuccess.Should().BeTrue(); + result.Value.Should().Be(T.Value); + } + + [Fact] + public async Task ToResult_Task_returns_custom_failure_via_error_function_if_has_no_value() + { + Maybe maybe = null; + + var result = await maybe.AsTask().ToResult(() => E.Value); + + result.IsSuccess.Should().BeFalse(); + result.Error.Should().Be(E.Value); + } + + [Fact] + public async Task ToResult_Task_custom_failure_with_error_function_returns_success_if_has_value() + { + var maybe = Maybe.From(T.Value); + + var result = await maybe.AsTask().ToResult(() => E.Value); + + result.IsSuccess.Should().BeTrue(); + result.Value.Should().Be(T.Value); + } + } +} diff --git a/CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/ToResultTests.ValueTask.Left.cs b/CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/ToResultTests.ValueTask.Left.cs index 1cfc8025..310b06c4 100644 --- a/CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/ToResultTests.ValueTask.Left.cs +++ b/CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/ToResultTests.ValueTask.Left.cs @@ -1,54 +1,76 @@ -using System.Threading.Tasks; -using CSharpFunctionalExtensions.ValueTasks; -using FluentAssertions; -using Xunit; - -namespace CSharpFunctionalExtensions.Tests.MaybeTests.Extensions -{ - public class ToResultTests_ValueTask_Left : MaybeTestBase - { - [Fact] - public async Task ToResult_ValueTask_Left_returns_failure_if_has_no_value() - { - Maybe maybe = null; - - var result = await maybe.AsValueTask().ToResult("Error"); - - result.IsSuccess.Should().BeFalse(); - result.Error.Should().Be("Error"); - } - - [Fact] - public async Task ToResult_ValueTask_Left_returns_success_if_has_value() - { - var maybe = Maybe.From(T.Value); - - var result = await maybe.AsValueTask().ToResult("Error"); - - result.IsSuccess.Should().BeTrue(); - result.Value.Should().Be(T.Value); - } - - [Fact] - public async Task ToResult_ValueTask_Left_returns_custom_failure_if_has_no_value() - { - Maybe maybe = null; - - var result = await maybe.AsValueTask().ToResult(E.Value); - - result.IsSuccess.Should().BeFalse(); - result.Error.Should().Be(E.Value); - } - - [Fact] - public async Task ToResult_ValueTask_Left_custom_failure_returns_success_if_has_value() - { - var maybe = Maybe.From(T.Value); - - var result = await maybe.AsValueTask().ToResult(E.Value); - - result.IsSuccess.Should().BeTrue(); - result.Value.Should().Be(T.Value); - } - } -} \ No newline at end of file +using System.Threading.Tasks; +using CSharpFunctionalExtensions.ValueTasks; +using FluentAssertions; +using Xunit; + +namespace CSharpFunctionalExtensions.Tests.MaybeTests.Extensions +{ + public class ToResultTests_ValueTask_Left : MaybeTestBase + { + [Fact] + public async Task ToResult_ValueTask_Left_returns_failure_if_has_no_value() + { + Maybe maybe = null; + + var result = await maybe.AsValueTask().ToResult("Error"); + + result.IsSuccess.Should().BeFalse(); + result.Error.Should().Be("Error"); + } + + [Fact] + public async Task ToResult_ValueTask_Left_returns_success_if_has_value() + { + var maybe = Maybe.From(T.Value); + + var result = await maybe.AsValueTask().ToResult("Error"); + + result.IsSuccess.Should().BeTrue(); + result.Value.Should().Be(T.Value); + } + + [Fact] + public async Task ToResult_ValueTask_Left_returns_custom_failure_if_has_no_value() + { + Maybe maybe = null; + + var result = await maybe.AsValueTask().ToResult(E.Value); + + result.IsSuccess.Should().BeFalse(); + result.Error.Should().Be(E.Value); + } + + [Fact] + public async Task ToResult_ValueTask_Left_custom_failure_returns_success_if_has_value() + { + var maybe = Maybe.From(T.Value); + + var result = await maybe.AsValueTask().ToResult(E.Value); + + result.IsSuccess.Should().BeTrue(); + result.Value.Should().Be(T.Value); + } + + [Fact] + public async Task ToResult_ValueTask_Left_returns_custom_failure_via_error_function_if_has_no_value() + { + Maybe maybe = null; + + var result = await maybe.AsValueTask().ToResult(() => E.Value); + + result.IsSuccess.Should().BeFalse(); + result.Error.Should().Be(E.Value); + } + + [Fact] + public async Task ToResult_ValueTask_Left_custom_failure_with_error_function_returns_success_if_has_value() + { + var maybe = Maybe.From(T.Value); + + var result = await maybe.AsValueTask().ToResult(() => E.Value); + + result.IsSuccess.Should().BeTrue(); + result.Value.Should().Be(T.Value); + } + } +} diff --git a/CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/ToResultTests.ValueTask.cs b/CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/ToResultTests.ValueTask.cs index 5ac3575f..6d37f1bb 100644 --- a/CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/ToResultTests.ValueTask.cs +++ b/CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/ToResultTests.ValueTask.cs @@ -1,54 +1,76 @@ -using System.Threading.Tasks; -using CSharpFunctionalExtensions.ValueTasks; -using FluentAssertions; -using Xunit; - -namespace CSharpFunctionalExtensions.Tests.MaybeTests.Extensions -{ - public class ToResultTests_ValueTask : MaybeTestBase - { - [Fact] - public async Task ToResult_ValueTask_returns_failure_if_has_no_value() - { - Maybe maybe = null; - - var result = await maybe.AsValueTask().ToResult("Error"); - - result.IsSuccess.Should().BeFalse(); - result.Error.Should().Be("Error"); - } - - [Fact] - public async Task ToResult_ValueTask_returns_success_if_has_value() - { - var maybe = Maybe.From(T.Value); - - var result = await maybe.AsValueTask().ToResult("Error".AsValueTask()); - - result.IsSuccess.Should().BeTrue(); - result.Value.Should().Be(T.Value); - } - - [Fact] - public async Task ToResult_ValueTask_returns_custom_failure_if_has_no_value() - { - Maybe maybe = null; - - var result = await maybe.AsValueTask().ToResult(E.Value); - - result.IsSuccess.Should().BeFalse(); - result.Error.Should().Be(E.Value); - } - - [Fact] - public async Task ToResult_ValueTask_custom_failure_returns_success_if_has_value() - { - var maybe = Maybe.From(T.Value); - - var result = await maybe.AsValueTask().ToResult(E.Value.AsValueTask()); - - result.IsSuccess.Should().BeTrue(); - result.Value.Should().Be(T.Value); - } - } -} \ No newline at end of file +using System.Threading.Tasks; +using CSharpFunctionalExtensions.ValueTasks; +using FluentAssertions; +using Xunit; + +namespace CSharpFunctionalExtensions.Tests.MaybeTests.Extensions +{ + public class ToResultTests_ValueTask : MaybeTestBase + { + [Fact] + public async Task ToResult_ValueTask_returns_failure_if_has_no_value() + { + Maybe maybe = null; + + var result = await maybe.AsValueTask().ToResult("Error"); + + result.IsSuccess.Should().BeFalse(); + result.Error.Should().Be("Error"); + } + + [Fact] + public async Task ToResult_ValueTask_returns_success_if_has_value() + { + var maybe = Maybe.From(T.Value); + + var result = await maybe.AsValueTask().ToResult("Error".AsValueTask()); + + result.IsSuccess.Should().BeTrue(); + result.Value.Should().Be(T.Value); + } + + [Fact] + public async Task ToResult_ValueTask_returns_custom_failure_if_has_no_value() + { + Maybe maybe = null; + + var result = await maybe.AsValueTask().ToResult(E.Value); + + result.IsSuccess.Should().BeFalse(); + result.Error.Should().Be(E.Value); + } + + [Fact] + public async Task ToResult_ValueTask_custom_failure_returns_success_if_has_value() + { + var maybe = Maybe.From(T.Value); + + var result = await maybe.AsValueTask().ToResult(E.Value); + + result.IsSuccess.Should().BeTrue(); + result.Value.Should().Be(T.Value); + } + + [Fact] + public async Task ToResult_ValueTask_returns_custom_failure_via_error_function_if_has_no_value() + { + Maybe maybe = null; + + var result = await maybe.AsValueTask().ToResult(() => E.Value); + + result.IsSuccess.Should().BeFalse(); + result.Error.Should().Be(E.Value); + } + + [Fact] + public async Task ToResult_ValueTask_custom_failure_with_error_function_returns_success_if_has_value() + { + var maybe = Maybe.From(T.Value); + + var result = await maybe.AsValueTask().ToResult(() => E.Value); + + result.IsSuccess.Should().BeTrue(); + result.Value.Should().Be(T.Value); + } + } +} diff --git a/CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/ToResultTests.cs b/CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/ToResultTests.cs index e2de0610..345e2d03 100644 --- a/CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/ToResultTests.cs +++ b/CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/ToResultTests.cs @@ -1,53 +1,74 @@ -using System; -using FluentAssertions; -using Xunit; - -namespace CSharpFunctionalExtensions.Tests.MaybeTests.Extensions -{ - public class ToResultTests : MaybeTestBase - { - [Fact] - public void ToResult_returns_failure_if_has_no_value() - { - Maybe maybe = null; - - var result = maybe.ToResult("Error"); - - result.IsSuccess.Should().BeFalse(); - result.Error.Should().Be("Error"); - } - - [Fact] - public void ToResult_returns_success_if_has_value() - { - var maybe = Maybe.From(T.Value); - - var result = maybe.ToResult("Error"); - - result.IsSuccess.Should().BeTrue(); - result.Value.Should().Be(T.Value); - } - - [Fact] - public void ToResult_returns_custom_failure_if_has_no_value() - { - Maybe maybe = null; - - var result = maybe.ToResult(E.Value); - - result.IsSuccess.Should().BeFalse(); - result.Error.Should().Be(E.Value); - } - - [Fact] - public void ToResult_custom_failure_returns_success_if_has_value() - { - var maybe = Maybe.From(T.Value); - - var result = maybe.ToResult(E.Value); - - result.IsSuccess.Should().BeTrue(); - result.Value.Should().Be(T.Value); - } - } -} +using FluentAssertions; +using Xunit; + +namespace CSharpFunctionalExtensions.Tests.MaybeTests.Extensions +{ + public class ToResultTests : MaybeTestBase + { + [Fact] + public void ToResult_returns_failure_if_has_no_value() + { + Maybe maybe = null; + + var result = maybe.ToResult("Error"); + + result.IsSuccess.Should().BeFalse(); + result.Error.Should().Be("Error"); + } + + [Fact] + public void ToResult_returns_success_if_has_value() + { + var maybe = Maybe.From(T.Value); + + var result = maybe.ToResult("Error"); + + result.IsSuccess.Should().BeTrue(); + result.Value.Should().Be(T.Value); + } + + [Fact] + public void ToResult_returns_custom_failure_if_has_no_value() + { + Maybe maybe = null; + + var result = maybe.ToResult(E.Value); + + result.IsSuccess.Should().BeFalse(); + result.Error.Should().Be(E.Value); + } + + [Fact] + public void ToResult_custom_failure_returns_success_if_has_value() + { + var maybe = Maybe.From(T.Value); + + var result = maybe.ToResult(E.Value); + + result.IsSuccess.Should().BeTrue(); + result.Value.Should().Be(T.Value); + } + + [Fact] + public void ToResult_returns_custom_failure_via_error_function_if_has_no_value() + { + Maybe maybe = null; + + var result = maybe.ToResult(() => E.Value); + + result.IsSuccess.Should().BeFalse(); + result.Error.Should().Be(E.Value); + } + + [Fact] + public void ToResult_custom_failure_with_error_function_returns_success_if_has_value() + { + var maybe = Maybe.From(T.Value); + + var result = maybe.ToResult(() => E.Value); + + result.IsSuccess.Should().BeTrue(); + result.Value.Should().Be(T.Value); + } + } +} diff --git a/CSharpFunctionalExtensions/Maybe/Extensions/ToResult.Task.cs b/CSharpFunctionalExtensions/Maybe/Extensions/ToResult.Task.cs index 05fab457..14da69e4 100644 --- a/CSharpFunctionalExtensions/Maybe/Extensions/ToResult.Task.cs +++ b/CSharpFunctionalExtensions/Maybe/Extensions/ToResult.Task.cs @@ -1,19 +1,26 @@ -using System.Threading.Tasks; - -namespace CSharpFunctionalExtensions -{ - public static partial class MaybeExtensions - { - public static async Task> ToResult(this Task> maybeTask, string errorMessage) - { - var maybe = await maybeTask.DefaultAwait(); - return maybe.ToResult(errorMessage); - } - - public static async Task> ToResult(this Task> maybeTask, E error) - { - var maybe = await maybeTask.DefaultAwait(); - return maybe.ToResult(error); - } - } -} \ No newline at end of file +using System; +using System.Threading.Tasks; + +namespace CSharpFunctionalExtensions +{ + public static partial class MaybeExtensions + { + public static async Task> ToResult(this Task> maybeTask, string errorMessage) + { + var maybe = await maybeTask.DefaultAwait(); + return maybe.ToResult(errorMessage); + } + + public static async Task> ToResult(this Task> maybeTask, E error) + { + var maybe = await maybeTask.DefaultAwait(); + return maybe.ToResult(error); + } + + public static async Task> ToResult(this Task> maybeTask, Func errorFunc) + { + var maybe = await maybeTask.DefaultAwait(); + return maybe.ToResult(errorFunc); + } + } +} diff --git a/CSharpFunctionalExtensions/Maybe/Extensions/ToResult.ValueTask.cs b/CSharpFunctionalExtensions/Maybe/Extensions/ToResult.ValueTask.cs index 1b57d941..14a00f18 100644 --- a/CSharpFunctionalExtensions/Maybe/Extensions/ToResult.ValueTask.cs +++ b/CSharpFunctionalExtensions/Maybe/Extensions/ToResult.ValueTask.cs @@ -1,22 +1,28 @@ -#if NET5_0_OR_GREATER -using System; -using System.Threading.Tasks; - -namespace CSharpFunctionalExtensions.ValueTasks -{ - public static partial class MaybeExtensions - { - public static async ValueTask> ToResult(this ValueTask> maybeTask, string errorMessage) - { - Maybe maybe = await maybeTask; - return maybe.ToResult(errorMessage); - } - - public static async ValueTask> ToResult(this ValueTask> maybeTask, E error) - { - Maybe maybe = await maybeTask; - return maybe.ToResult(error); - } - } -} -#endif \ No newline at end of file +#if NET5_0_OR_GREATER +using System; +using System.Threading.Tasks; + +namespace CSharpFunctionalExtensions.ValueTasks +{ + public static partial class MaybeExtensions + { + public static async ValueTask> ToResult(this ValueTask> maybeTask, string errorMessage) + { + Maybe maybe = await maybeTask; + return maybe.ToResult(errorMessage); + } + + public static async ValueTask> ToResult(this ValueTask> maybeTask, E error) + { + Maybe maybe = await maybeTask; + return maybe.ToResult(error); + } + + public static async ValueTask> ToResult(this ValueTask> maybeTask, Func errorFunc) + { + Maybe maybe = await maybeTask; + return maybe.ToResult(errorFunc); + } + } +} +#endif diff --git a/CSharpFunctionalExtensions/Maybe/Extensions/ToResult.cs b/CSharpFunctionalExtensions/Maybe/Extensions/ToResult.cs index 424ce90a..5729a95d 100644 --- a/CSharpFunctionalExtensions/Maybe/Extensions/ToResult.cs +++ b/CSharpFunctionalExtensions/Maybe/Extensions/ToResult.cs @@ -1,21 +1,31 @@ -namespace CSharpFunctionalExtensions -{ - public static partial class MaybeExtensions - { - public static Result ToResult(in this Maybe maybe, string errorMessage) - { - if (maybe.HasNoValue) - return Result.Failure(errorMessage); - - return Result.Success(maybe.GetValueOrThrow()); - } - - public static Result ToResult(in this Maybe maybe, E error) - { - if (maybe.HasNoValue) - return Result.Failure(error); - - return Result.Success(maybe.GetValueOrThrow()); - } - } -} \ No newline at end of file +using System; + +namespace CSharpFunctionalExtensions +{ + public static partial class MaybeExtensions + { + public static Result ToResult(in this Maybe maybe, string errorMessage) + { + if (maybe.HasNoValue) + return Result.Failure(errorMessage); + + return Result.Success(maybe.GetValueOrThrow()); + } + + public static Result ToResult(in this Maybe maybe, E error) + { + if (maybe.HasNoValue) + return Result.Failure(error); + + return Result.Success(maybe.GetValueOrThrow()); + } + + public static Result ToResult(in this Maybe maybe, Func errorFunc) + { + if (maybe.HasNoValue) + return Result.Failure(errorFunc()); + + return Result.Success(maybe.GetValueOrThrow()); + } + } +} From 8f1354e14eb4a7b23e1bebf4d7f4d289761ff5f0 Mon Sep 17 00:00:00 2001 From: Marcin Jahn <10273406+marcinjahn@users.noreply.github.com> Date: Sat, 23 Nov 2024 07:55:46 +0100 Subject: [PATCH 2/3] Remove repeated tests + fixes --- .../Extensions/ToResultTests.Task.Left.cs | 75 ------------------ .../Extensions/ToResultTests.Task.cs | 2 +- .../ToResultTests.ValueTask.Left.cs | 76 ------------------- .../Extensions/ToResultTests.ValueTask.cs | 2 +- 4 files changed, 2 insertions(+), 153 deletions(-) delete mode 100644 CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/ToResultTests.Task.Left.cs delete mode 100644 CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/ToResultTests.ValueTask.Left.cs diff --git a/CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/ToResultTests.Task.Left.cs b/CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/ToResultTests.Task.Left.cs deleted file mode 100644 index a6a1cf7a..00000000 --- a/CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/ToResultTests.Task.Left.cs +++ /dev/null @@ -1,75 +0,0 @@ -using System.Threading.Tasks; -using FluentAssertions; -using Xunit; - -namespace CSharpFunctionalExtensions.Tests.MaybeTests.Extensions -{ - public class ToResultTests_Task_Left : MaybeTestBase - { - [Fact] - public async Task ToResult_Task_Left_returns_failure_if_has_no_value() - { - Maybe maybe = null; - - var result = await maybe.AsTask().ToResult("Error"); - - result.IsSuccess.Should().BeFalse(); - result.Error.Should().Be("Error"); - } - - [Fact] - public async Task ToResult_Task_Left_returns_success_if_has_value() - { - var maybe = Maybe.From(T.Value); - - var result = await maybe.AsTask().ToResult("Error"); - - result.IsSuccess.Should().BeTrue(); - result.Value.Should().Be(T.Value); - } - - [Fact] - public async Task ToResult_Task_Left_returns_custom_failure_if_has_no_value() - { - Maybe maybe = null; - - var result = await maybe.AsTask().ToResult(E.Value); - - result.IsSuccess.Should().BeFalse(); - result.Error.Should().Be(E.Value); - } - - [Fact] - public async Task ToResult_Task_Left_custom_failure_returns_success_if_has_value() - { - var maybe = Maybe.From(T.Value); - - var result = await maybe.AsTask().ToResult(E.Value); - - result.IsSuccess.Should().BeTrue(); - result.Value.Should().Be(T.Value); - } - - [Fact] - public async Task ToResult_Task_Left_returns_custom_failure_via_error_function_if_has_no_value() - { - Maybe maybe = null; - - var result = await maybe.AsTask().ToResult(() => E.Value); - - result.IsSuccess.Should().BeFalse(); - result.Error.Should().Be(E.Value); - } - - [Fact] - public async Task ToResult_Task_Left_custom_failure_with_error_function_returns_success_if_has_value() - { - var maybe = Maybe.From(T.Value); - - var result = await maybe.AsTask().ToResult(() => E.Value); - - result.IsSuccess.Should().BeTrue(); - result.Value.Should().Be(T.Value); - } - } -} diff --git a/CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/ToResultTests.Task.cs b/CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/ToResultTests.Task.cs index a2a410ae..da7f3675 100644 --- a/CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/ToResultTests.Task.cs +++ b/CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/ToResultTests.Task.cs @@ -22,7 +22,7 @@ public async Task ToResult_Task_returns_success_if_has_value() { var maybe = Maybe.From(T.Value); - var result = await maybe.AsTask().ToResult("Error".AsTask()); + var result = await maybe.AsTask().ToResult("Error"); result.IsSuccess.Should().BeTrue(); result.Value.Should().Be(T.Value); diff --git a/CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/ToResultTests.ValueTask.Left.cs b/CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/ToResultTests.ValueTask.Left.cs deleted file mode 100644 index 310b06c4..00000000 --- a/CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/ToResultTests.ValueTask.Left.cs +++ /dev/null @@ -1,76 +0,0 @@ -using System.Threading.Tasks; -using CSharpFunctionalExtensions.ValueTasks; -using FluentAssertions; -using Xunit; - -namespace CSharpFunctionalExtensions.Tests.MaybeTests.Extensions -{ - public class ToResultTests_ValueTask_Left : MaybeTestBase - { - [Fact] - public async Task ToResult_ValueTask_Left_returns_failure_if_has_no_value() - { - Maybe maybe = null; - - var result = await maybe.AsValueTask().ToResult("Error"); - - result.IsSuccess.Should().BeFalse(); - result.Error.Should().Be("Error"); - } - - [Fact] - public async Task ToResult_ValueTask_Left_returns_success_if_has_value() - { - var maybe = Maybe.From(T.Value); - - var result = await maybe.AsValueTask().ToResult("Error"); - - result.IsSuccess.Should().BeTrue(); - result.Value.Should().Be(T.Value); - } - - [Fact] - public async Task ToResult_ValueTask_Left_returns_custom_failure_if_has_no_value() - { - Maybe maybe = null; - - var result = await maybe.AsValueTask().ToResult(E.Value); - - result.IsSuccess.Should().BeFalse(); - result.Error.Should().Be(E.Value); - } - - [Fact] - public async Task ToResult_ValueTask_Left_custom_failure_returns_success_if_has_value() - { - var maybe = Maybe.From(T.Value); - - var result = await maybe.AsValueTask().ToResult(E.Value); - - result.IsSuccess.Should().BeTrue(); - result.Value.Should().Be(T.Value); - } - - [Fact] - public async Task ToResult_ValueTask_Left_returns_custom_failure_via_error_function_if_has_no_value() - { - Maybe maybe = null; - - var result = await maybe.AsValueTask().ToResult(() => E.Value); - - result.IsSuccess.Should().BeFalse(); - result.Error.Should().Be(E.Value); - } - - [Fact] - public async Task ToResult_ValueTask_Left_custom_failure_with_error_function_returns_success_if_has_value() - { - var maybe = Maybe.From(T.Value); - - var result = await maybe.AsValueTask().ToResult(() => E.Value); - - result.IsSuccess.Should().BeTrue(); - result.Value.Should().Be(T.Value); - } - } -} diff --git a/CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/ToResultTests.ValueTask.cs b/CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/ToResultTests.ValueTask.cs index 6d37f1bb..2381de2c 100644 --- a/CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/ToResultTests.ValueTask.cs +++ b/CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/ToResultTests.ValueTask.cs @@ -23,7 +23,7 @@ public async Task ToResult_ValueTask_returns_success_if_has_value() { var maybe = Maybe.From(T.Value); - var result = await maybe.AsValueTask().ToResult("Error".AsValueTask()); + var result = await maybe.AsValueTask().ToResult("Error"); result.IsSuccess.Should().BeTrue(); result.Value.Should().Be(T.Value); From 6896bd3d78a12c5aa2e238bd13e4e198c2692f19 Mon Sep 17 00:00:00 2001 From: Marcin Jahn <10273406+marcinjahn@users.noreply.github.com> Date: Sat, 23 Nov 2024 08:16:47 +0100 Subject: [PATCH 3/3] Fix line endings --- .../Extensions/ToResultTests.Task.cs | 150 ++++++++--------- .../Extensions/ToResultTests.ValueTask.cs | 152 +++++++++--------- .../MaybeTests/Extensions/ToResultTests.cs | 148 ++++++++--------- .../Maybe/Extensions/ToResult.Task.cs | 52 +++--- .../Maybe/Extensions/ToResult.ValueTask.cs | 56 +++---- .../Maybe/Extensions/ToResult.cs | 62 +++---- 6 files changed, 310 insertions(+), 310 deletions(-) diff --git a/CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/ToResultTests.Task.cs b/CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/ToResultTests.Task.cs index da7f3675..b13b4bc5 100644 --- a/CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/ToResultTests.Task.cs +++ b/CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/ToResultTests.Task.cs @@ -1,75 +1,75 @@ -using System.Threading.Tasks; -using FluentAssertions; -using Xunit; - -namespace CSharpFunctionalExtensions.Tests.MaybeTests.Extensions -{ - public class ToResultTests_Task : MaybeTestBase - { - [Fact] - public async Task ToResult_Task_returns_failure_if_has_no_value() - { - Maybe maybe = null; - - var result = await maybe.AsTask().ToResult("Error"); - - result.IsSuccess.Should().BeFalse(); - result.Error.Should().Be("Error"); - } - - [Fact] - public async Task ToResult_Task_returns_success_if_has_value() - { - var maybe = Maybe.From(T.Value); - - var result = await maybe.AsTask().ToResult("Error"); - - result.IsSuccess.Should().BeTrue(); - result.Value.Should().Be(T.Value); - } - - [Fact] - public async Task ToResult_Task_returns_custom_failure_if_has_no_value() - { - Maybe maybe = null; - - var result = await maybe.AsTask().ToResult(E.Value); - - result.IsSuccess.Should().BeFalse(); - result.Error.Should().Be(E.Value); - } - - [Fact] - public async Task ToResult_Task_custom_failure_returns_success_if_has_value() - { - var maybe = Maybe.From(T.Value); - - var result = await maybe.AsTask().ToResult(E.Value); - - result.IsSuccess.Should().BeTrue(); - result.Value.Should().Be(T.Value); - } - - [Fact] - public async Task ToResult_Task_returns_custom_failure_via_error_function_if_has_no_value() - { - Maybe maybe = null; - - var result = await maybe.AsTask().ToResult(() => E.Value); - - result.IsSuccess.Should().BeFalse(); - result.Error.Should().Be(E.Value); - } - - [Fact] - public async Task ToResult_Task_custom_failure_with_error_function_returns_success_if_has_value() - { - var maybe = Maybe.From(T.Value); - - var result = await maybe.AsTask().ToResult(() => E.Value); - - result.IsSuccess.Should().BeTrue(); - result.Value.Should().Be(T.Value); - } - } -} +using System.Threading.Tasks; +using FluentAssertions; +using Xunit; + +namespace CSharpFunctionalExtensions.Tests.MaybeTests.Extensions +{ + public class ToResultTests_Task : MaybeTestBase + { + [Fact] + public async Task ToResult_Task_returns_failure_if_has_no_value() + { + Maybe maybe = null; + + var result = await maybe.AsTask().ToResult("Error"); + + result.IsSuccess.Should().BeFalse(); + result.Error.Should().Be("Error"); + } + + [Fact] + public async Task ToResult_Task_returns_success_if_has_value() + { + var maybe = Maybe.From(T.Value); + + var result = await maybe.AsTask().ToResult("Error"); + + result.IsSuccess.Should().BeTrue(); + result.Value.Should().Be(T.Value); + } + + [Fact] + public async Task ToResult_Task_returns_custom_failure_if_has_no_value() + { + Maybe maybe = null; + + var result = await maybe.AsTask().ToResult(E.Value); + + result.IsSuccess.Should().BeFalse(); + result.Error.Should().Be(E.Value); + } + + [Fact] + public async Task ToResult_Task_custom_failure_returns_success_if_has_value() + { + var maybe = Maybe.From(T.Value); + + var result = await maybe.AsTask().ToResult(E.Value); + + result.IsSuccess.Should().BeTrue(); + result.Value.Should().Be(T.Value); + } + + [Fact] + public async Task ToResult_Task_returns_custom_failure_via_error_function_if_has_no_value() + { + Maybe maybe = null; + + var result = await maybe.AsTask().ToResult(() => E.Value); + + result.IsSuccess.Should().BeFalse(); + result.Error.Should().Be(E.Value); + } + + [Fact] + public async Task ToResult_Task_custom_failure_with_error_function_returns_success_if_has_value() + { + var maybe = Maybe.From(T.Value); + + var result = await maybe.AsTask().ToResult(() => E.Value); + + result.IsSuccess.Should().BeTrue(); + result.Value.Should().Be(T.Value); + } + } +} diff --git a/CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/ToResultTests.ValueTask.cs b/CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/ToResultTests.ValueTask.cs index 2381de2c..f1e36ff5 100644 --- a/CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/ToResultTests.ValueTask.cs +++ b/CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/ToResultTests.ValueTask.cs @@ -1,76 +1,76 @@ -using System.Threading.Tasks; -using CSharpFunctionalExtensions.ValueTasks; -using FluentAssertions; -using Xunit; - -namespace CSharpFunctionalExtensions.Tests.MaybeTests.Extensions -{ - public class ToResultTests_ValueTask : MaybeTestBase - { - [Fact] - public async Task ToResult_ValueTask_returns_failure_if_has_no_value() - { - Maybe maybe = null; - - var result = await maybe.AsValueTask().ToResult("Error"); - - result.IsSuccess.Should().BeFalse(); - result.Error.Should().Be("Error"); - } - - [Fact] - public async Task ToResult_ValueTask_returns_success_if_has_value() - { - var maybe = Maybe.From(T.Value); - - var result = await maybe.AsValueTask().ToResult("Error"); - - result.IsSuccess.Should().BeTrue(); - result.Value.Should().Be(T.Value); - } - - [Fact] - public async Task ToResult_ValueTask_returns_custom_failure_if_has_no_value() - { - Maybe maybe = null; - - var result = await maybe.AsValueTask().ToResult(E.Value); - - result.IsSuccess.Should().BeFalse(); - result.Error.Should().Be(E.Value); - } - - [Fact] - public async Task ToResult_ValueTask_custom_failure_returns_success_if_has_value() - { - var maybe = Maybe.From(T.Value); - - var result = await maybe.AsValueTask().ToResult(E.Value); - - result.IsSuccess.Should().BeTrue(); - result.Value.Should().Be(T.Value); - } - - [Fact] - public async Task ToResult_ValueTask_returns_custom_failure_via_error_function_if_has_no_value() - { - Maybe maybe = null; - - var result = await maybe.AsValueTask().ToResult(() => E.Value); - - result.IsSuccess.Should().BeFalse(); - result.Error.Should().Be(E.Value); - } - - [Fact] - public async Task ToResult_ValueTask_custom_failure_with_error_function_returns_success_if_has_value() - { - var maybe = Maybe.From(T.Value); - - var result = await maybe.AsValueTask().ToResult(() => E.Value); - - result.IsSuccess.Should().BeTrue(); - result.Value.Should().Be(T.Value); - } - } -} +using System.Threading.Tasks; +using CSharpFunctionalExtensions.ValueTasks; +using FluentAssertions; +using Xunit; + +namespace CSharpFunctionalExtensions.Tests.MaybeTests.Extensions +{ + public class ToResultTests_ValueTask : MaybeTestBase + { + [Fact] + public async Task ToResult_ValueTask_returns_failure_if_has_no_value() + { + Maybe maybe = null; + + var result = await maybe.AsValueTask().ToResult("Error"); + + result.IsSuccess.Should().BeFalse(); + result.Error.Should().Be("Error"); + } + + [Fact] + public async Task ToResult_ValueTask_returns_success_if_has_value() + { + var maybe = Maybe.From(T.Value); + + var result = await maybe.AsValueTask().ToResult("Error"); + + result.IsSuccess.Should().BeTrue(); + result.Value.Should().Be(T.Value); + } + + [Fact] + public async Task ToResult_ValueTask_returns_custom_failure_if_has_no_value() + { + Maybe maybe = null; + + var result = await maybe.AsValueTask().ToResult(E.Value); + + result.IsSuccess.Should().BeFalse(); + result.Error.Should().Be(E.Value); + } + + [Fact] + public async Task ToResult_ValueTask_custom_failure_returns_success_if_has_value() + { + var maybe = Maybe.From(T.Value); + + var result = await maybe.AsValueTask().ToResult(E.Value); + + result.IsSuccess.Should().BeTrue(); + result.Value.Should().Be(T.Value); + } + + [Fact] + public async Task ToResult_ValueTask_returns_custom_failure_via_error_function_if_has_no_value() + { + Maybe maybe = null; + + var result = await maybe.AsValueTask().ToResult(() => E.Value); + + result.IsSuccess.Should().BeFalse(); + result.Error.Should().Be(E.Value); + } + + [Fact] + public async Task ToResult_ValueTask_custom_failure_with_error_function_returns_success_if_has_value() + { + var maybe = Maybe.From(T.Value); + + var result = await maybe.AsValueTask().ToResult(() => E.Value); + + result.IsSuccess.Should().BeTrue(); + result.Value.Should().Be(T.Value); + } + } +} diff --git a/CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/ToResultTests.cs b/CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/ToResultTests.cs index 345e2d03..94ed658c 100644 --- a/CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/ToResultTests.cs +++ b/CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/ToResultTests.cs @@ -1,74 +1,74 @@ -using FluentAssertions; -using Xunit; - -namespace CSharpFunctionalExtensions.Tests.MaybeTests.Extensions -{ - public class ToResultTests : MaybeTestBase - { - [Fact] - public void ToResult_returns_failure_if_has_no_value() - { - Maybe maybe = null; - - var result = maybe.ToResult("Error"); - - result.IsSuccess.Should().BeFalse(); - result.Error.Should().Be("Error"); - } - - [Fact] - public void ToResult_returns_success_if_has_value() - { - var maybe = Maybe.From(T.Value); - - var result = maybe.ToResult("Error"); - - result.IsSuccess.Should().BeTrue(); - result.Value.Should().Be(T.Value); - } - - [Fact] - public void ToResult_returns_custom_failure_if_has_no_value() - { - Maybe maybe = null; - - var result = maybe.ToResult(E.Value); - - result.IsSuccess.Should().BeFalse(); - result.Error.Should().Be(E.Value); - } - - [Fact] - public void ToResult_custom_failure_returns_success_if_has_value() - { - var maybe = Maybe.From(T.Value); - - var result = maybe.ToResult(E.Value); - - result.IsSuccess.Should().BeTrue(); - result.Value.Should().Be(T.Value); - } - - [Fact] - public void ToResult_returns_custom_failure_via_error_function_if_has_no_value() - { - Maybe maybe = null; - - var result = maybe.ToResult(() => E.Value); - - result.IsSuccess.Should().BeFalse(); - result.Error.Should().Be(E.Value); - } - - [Fact] - public void ToResult_custom_failure_with_error_function_returns_success_if_has_value() - { - var maybe = Maybe.From(T.Value); - - var result = maybe.ToResult(() => E.Value); - - result.IsSuccess.Should().BeTrue(); - result.Value.Should().Be(T.Value); - } - } -} +using FluentAssertions; +using Xunit; + +namespace CSharpFunctionalExtensions.Tests.MaybeTests.Extensions +{ + public class ToResultTests : MaybeTestBase + { + [Fact] + public void ToResult_returns_failure_if_has_no_value() + { + Maybe maybe = null; + + var result = maybe.ToResult("Error"); + + result.IsSuccess.Should().BeFalse(); + result.Error.Should().Be("Error"); + } + + [Fact] + public void ToResult_returns_success_if_has_value() + { + var maybe = Maybe.From(T.Value); + + var result = maybe.ToResult("Error"); + + result.IsSuccess.Should().BeTrue(); + result.Value.Should().Be(T.Value); + } + + [Fact] + public void ToResult_returns_custom_failure_if_has_no_value() + { + Maybe maybe = null; + + var result = maybe.ToResult(E.Value); + + result.IsSuccess.Should().BeFalse(); + result.Error.Should().Be(E.Value); + } + + [Fact] + public void ToResult_custom_failure_returns_success_if_has_value() + { + var maybe = Maybe.From(T.Value); + + var result = maybe.ToResult(E.Value); + + result.IsSuccess.Should().BeTrue(); + result.Value.Should().Be(T.Value); + } + + [Fact] + public void ToResult_returns_custom_failure_via_error_function_if_has_no_value() + { + Maybe maybe = null; + + var result = maybe.ToResult(() => E.Value); + + result.IsSuccess.Should().BeFalse(); + result.Error.Should().Be(E.Value); + } + + [Fact] + public void ToResult_custom_failure_with_error_function_returns_success_if_has_value() + { + var maybe = Maybe.From(T.Value); + + var result = maybe.ToResult(() => E.Value); + + result.IsSuccess.Should().BeTrue(); + result.Value.Should().Be(T.Value); + } + } +} diff --git a/CSharpFunctionalExtensions/Maybe/Extensions/ToResult.Task.cs b/CSharpFunctionalExtensions/Maybe/Extensions/ToResult.Task.cs index 14da69e4..cf26a895 100644 --- a/CSharpFunctionalExtensions/Maybe/Extensions/ToResult.Task.cs +++ b/CSharpFunctionalExtensions/Maybe/Extensions/ToResult.Task.cs @@ -1,26 +1,26 @@ -using System; -using System.Threading.Tasks; - -namespace CSharpFunctionalExtensions -{ - public static partial class MaybeExtensions - { - public static async Task> ToResult(this Task> maybeTask, string errorMessage) - { - var maybe = await maybeTask.DefaultAwait(); - return maybe.ToResult(errorMessage); - } - - public static async Task> ToResult(this Task> maybeTask, E error) - { - var maybe = await maybeTask.DefaultAwait(); - return maybe.ToResult(error); - } - - public static async Task> ToResult(this Task> maybeTask, Func errorFunc) - { - var maybe = await maybeTask.DefaultAwait(); - return maybe.ToResult(errorFunc); - } - } -} +using System; +using System.Threading.Tasks; + +namespace CSharpFunctionalExtensions +{ + public static partial class MaybeExtensions + { + public static async Task> ToResult(this Task> maybeTask, string errorMessage) + { + var maybe = await maybeTask.DefaultAwait(); + return maybe.ToResult(errorMessage); + } + + public static async Task> ToResult(this Task> maybeTask, E error) + { + var maybe = await maybeTask.DefaultAwait(); + return maybe.ToResult(error); + } + + public static async Task> ToResult(this Task> maybeTask, Func errorFunc) + { + var maybe = await maybeTask.DefaultAwait(); + return maybe.ToResult(errorFunc); + } + } +} diff --git a/CSharpFunctionalExtensions/Maybe/Extensions/ToResult.ValueTask.cs b/CSharpFunctionalExtensions/Maybe/Extensions/ToResult.ValueTask.cs index 14a00f18..1bb8b6b0 100644 --- a/CSharpFunctionalExtensions/Maybe/Extensions/ToResult.ValueTask.cs +++ b/CSharpFunctionalExtensions/Maybe/Extensions/ToResult.ValueTask.cs @@ -1,28 +1,28 @@ -#if NET5_0_OR_GREATER -using System; -using System.Threading.Tasks; - -namespace CSharpFunctionalExtensions.ValueTasks -{ - public static partial class MaybeExtensions - { - public static async ValueTask> ToResult(this ValueTask> maybeTask, string errorMessage) - { - Maybe maybe = await maybeTask; - return maybe.ToResult(errorMessage); - } - - public static async ValueTask> ToResult(this ValueTask> maybeTask, E error) - { - Maybe maybe = await maybeTask; - return maybe.ToResult(error); - } - - public static async ValueTask> ToResult(this ValueTask> maybeTask, Func errorFunc) - { - Maybe maybe = await maybeTask; - return maybe.ToResult(errorFunc); - } - } -} -#endif +#if NET5_0_OR_GREATER +using System; +using System.Threading.Tasks; + +namespace CSharpFunctionalExtensions.ValueTasks +{ + public static partial class MaybeExtensions + { + public static async ValueTask> ToResult(this ValueTask> maybeTask, string errorMessage) + { + Maybe maybe = await maybeTask; + return maybe.ToResult(errorMessage); + } + + public static async ValueTask> ToResult(this ValueTask> maybeTask, E error) + { + Maybe maybe = await maybeTask; + return maybe.ToResult(error); + } + + public static async ValueTask> ToResult(this ValueTask> maybeTask, Func errorFunc) + { + Maybe maybe = await maybeTask; + return maybe.ToResult(errorFunc); + } + } +} +#endif diff --git a/CSharpFunctionalExtensions/Maybe/Extensions/ToResult.cs b/CSharpFunctionalExtensions/Maybe/Extensions/ToResult.cs index 5729a95d..a793ce83 100644 --- a/CSharpFunctionalExtensions/Maybe/Extensions/ToResult.cs +++ b/CSharpFunctionalExtensions/Maybe/Extensions/ToResult.cs @@ -1,31 +1,31 @@ -using System; - -namespace CSharpFunctionalExtensions -{ - public static partial class MaybeExtensions - { - public static Result ToResult(in this Maybe maybe, string errorMessage) - { - if (maybe.HasNoValue) - return Result.Failure(errorMessage); - - return Result.Success(maybe.GetValueOrThrow()); - } - - public static Result ToResult(in this Maybe maybe, E error) - { - if (maybe.HasNoValue) - return Result.Failure(error); - - return Result.Success(maybe.GetValueOrThrow()); - } - - public static Result ToResult(in this Maybe maybe, Func errorFunc) - { - if (maybe.HasNoValue) - return Result.Failure(errorFunc()); - - return Result.Success(maybe.GetValueOrThrow()); - } - } -} +using System; + +namespace CSharpFunctionalExtensions +{ + public static partial class MaybeExtensions + { + public static Result ToResult(in this Maybe maybe, string errorMessage) + { + if (maybe.HasNoValue) + return Result.Failure(errorMessage); + + return Result.Success(maybe.GetValueOrThrow()); + } + + public static Result ToResult(in this Maybe maybe, E error) + { + if (maybe.HasNoValue) + return Result.Failure(error); + + return Result.Success(maybe.GetValueOrThrow()); + } + + public static Result ToResult(in this Maybe maybe, Func errorFunc) + { + if (maybe.HasNoValue) + return Result.Failure(errorFunc()); + + return Result.Success(maybe.GetValueOrThrow()); + } + } +}