Skip to content

Commit e7ade71

Browse files
committed
Adicionado conversor implicito para Task<Result> e conversor de Problem para Result via método.
1 parent 3f38fce commit e7ade71

5 files changed

Lines changed: 68 additions & 1 deletion

File tree

src/Directory.Build.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
</PropertyGroup>
1111
<PropertyGroup>
1212
<SPVer>1.0.0</SPVer>
13-
<SPPreview>-preview-4.4</SPPreview>
13+
<SPPreview>-preview-4.5</SPPreview>
1414
</PropertyGroup>
1515
<PropertyGroup>
1616
<FluentValidationVer>12.0.0</FluentValidationVer>

src/RoyalCode.SmartProblems/Problem.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@ public sealed class Problem
1212
{
1313
#region implicit operators
1414

15+
/// <summary>
16+
/// Converts a <see cref="Problem"/> to a <see cref="Task"/> of <see cref="Result"/>.
17+
/// </summary>
18+
/// <param name="problem"></param>
19+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
20+
public static implicit operator Task<Result>(Problem problem) => Task.FromResult(new Result(problem));
21+
1522
/// <summary>
1623
/// Creates a collection of problems with both problems.
1724
/// </summary>
@@ -177,4 +184,23 @@ private static string DefaultToString(Problem problem)
177184

178185
return builder.ToString();
179186
}
187+
188+
/// <summary>
189+
/// Converts the problem to a <see cref="Result"/> object.
190+
/// </summary>
191+
/// <returns></returns>
192+
public Result AsResult()
193+
{
194+
return new Result(this);
195+
}
196+
197+
/// <summary>
198+
/// Converts the problem to a <see cref="Result{TValue}"/> object with a specified value type.
199+
/// </summary>
200+
/// <typeparam name="TValue"></typeparam>
201+
/// <returns></returns>
202+
public Result<TValue> AsResult<TValue>()
203+
{
204+
return new Result<TValue>(this);
205+
}
180206
}

src/RoyalCode.SmartProblems/Problems.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,13 @@ public sealed class Problems : ICollection<Problem>
4646
[MethodImpl(MethodImplOptions.AggressiveInlining)]
4747
public static implicit operator Problems(Problem problem) => [problem];
4848

49+
/// <summary>
50+
/// Convert a <see cref="Problem"/> to a <see cref="Task"/> of <see cref="Result"/>.
51+
/// </summary>
52+
/// <param name="problems"></param>
53+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
54+
public static implicit operator Task<Result>(Problems problems) => Task.FromResult(new Result(problems));
55+
4956
/// <summary>
5057
/// Add a problem to the collection of problems.
5158
/// </summary>
@@ -472,4 +479,24 @@ public InvalidOperationException ToException(string messagePattern, string separ
472479
var message = string.Format(messagePattern, string.Join(separator, this.Select(p => p.ToString())));
473480
return new InvalidOperationException(message);
474481
}
482+
483+
/// <summary>
484+
/// Convert the collection of problems to a <see cref="Result"/> instance.
485+
/// </summary>
486+
/// <returns></returns>
487+
public Result AsResult()
488+
{
489+
return new Result(this);
490+
}
491+
492+
/// <summary>
493+
/// Convert the collection of problems to a <see cref="Result{TValue}"/> instance with the specified value.
494+
/// </summary>
495+
/// <typeparam name="TValue"></typeparam>
496+
/// <param name="value"></param>
497+
/// <returns></returns>
498+
public Result<TValue> AsResult<TValue>(TValue value)
499+
{
500+
return new Result<TValue>(this);
501+
}
475502
}

src/RoyalCode.SmartProblems/Result'0.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,13 @@ public readonly struct Result
5252
[MethodImpl(MethodImplOptions.AggressiveInlining)]
5353
public static implicit operator Result(Exception ex) => new([Problems.InternalError(ex)]);
5454

55+
/// <summary>
56+
/// Converts a result to a <see cref="Task"/> of <see cref="Result"/>.
57+
/// </summary>
58+
/// <param name="result"></param>
59+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
60+
public static implicit operator Task<Result>(Result result) => Task.FromResult(result);
61+
5562
/// <summary>
5663
/// <para>
5764
/// Adds a problem to a result.

src/RoyalCode.SmartProblems/Result'1.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,13 @@ public readonly struct Result<TValue>
5858
[MethodImpl(MethodImplOptions.AggressiveInlining)]
5959
public static implicit operator Result<TValue>(Exception ex) => new([Problems.InternalError(ex)]);
6060

61+
/// <summary>
62+
/// Converts a result with a value to a task of result with a value.
63+
/// </summary>
64+
/// <param name="result"></param>
65+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
66+
public static implicit operator Task<Result<TValue>>(Result<TValue> result) => Task.FromResult(result);
67+
6168
/// <summary>
6269
/// <para>
6370
/// Converts a result with a value to a result without a value.

0 commit comments

Comments
 (0)