diff --git a/src/LightResults/LightResults.csproj b/src/LightResults/LightResults.csproj index 2212da3..1fde50f 100644 --- a/src/LightResults/LightResults.csproj +++ b/src/LightResults/LightResults.csproj @@ -6,7 +6,7 @@ enable LightResults latest - 8.0.10 + 8.0.11 LightResults Jean-Sebastien Carle An extremely light and modern Result Pattern library. @@ -19,8 +19,8 @@ https://github.com/jscarle/LightResults git result results pattern fluentresults error handling - 8.0.10.0 - 8.0.10.0 + 8.0.11.0 + 8.0.11.0 en-US true snupkg diff --git a/src/LightResults/Result`1.cs b/src/LightResults/Result`1.cs index 4c2e0b5..63bb0c0 100644 --- a/src/LightResults/Result`1.cs +++ b/src/LightResults/Result`1.cs @@ -1,4 +1,5 @@ using System.Collections.Immutable; +using System.Diagnostics.CodeAnalysis; using LightResults.Common; namespace LightResults; @@ -162,4 +163,13 @@ public override string ToString() var errorString = StringHelper.GetResultErrorString(_errors); return StringHelper.GetResultString(nameof(Result), "False", errorString); } + + /// Implicitly converts a value to a success . + /// The value to convert into a success result. + /// A new instance of representing a success result with the specified value. + [SuppressMessage("Usage", "CA2225: Operator overloads have named alternates", Justification = $"{nameof(Ok)} is the named alternate.")] + public static implicit operator Result(TValue value) + { + return Ok(value); + } } \ No newline at end of file diff --git a/tests/LightResults.Tests/ResultTValueTests.cs b/tests/LightResults.Tests/ResultTValueTests.cs index 6d52a7c..e306c52 100644 --- a/tests/LightResults.Tests/ResultTValueTests.cs +++ b/tests/LightResults.Tests/ResultTValueTests.cs @@ -1,4 +1,5 @@ using FluentAssertions; +using FluentAssertions.Execution; using Xunit; namespace LightResults.Tests; @@ -422,5 +423,32 @@ public void ToString_ShouldReturnProperRepresentationForUInt128(bool success, st } #endif + + [Fact] + public void ImplicitOperator_ShouldCreateSuccessResultWithValue() + { + // Arrange + const int value = 42; + + // Act + Result result = value; + + // Assert + using (new AssertionScope()) + { + result.IsSuccess + .Should() + .BeTrue(); + result.Value.Should() + .Be(value); + result.IsFailed + .Should() + .BeFalse(); + result.Errors + .Should() + .BeEmpty(); + } + } + private class ValidationError(string errorMessage) : Error(errorMessage); }