Skip to content

Commit fac5069

Browse files
authored
Merge pull request #2 from deveel/support-1
Support Package 1
2 parents 1ba8362 + 88a2cbb commit fac5069

File tree

7 files changed

+371
-55
lines changed

7 files changed

+371
-55
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
namespace Deveel
2+
{
3+
/// <summary>
4+
/// Extends the <see cref="IOperationError"/> contract to provide
5+
/// additional functionality.
6+
/// </summary>
7+
public static class OperationErrorExtensions
8+
{
9+
/// <summary>
10+
/// Converts the error to an operation exception.
11+
/// </summary>
12+
/// <param name="error">
13+
/// The error to convert to an exception.
14+
/// </param>
15+
/// <returns>
16+
/// Returns an instance of <see cref="OperationException"/> that
17+
/// represents the error.
18+
/// </returns>
19+
/// <exception cref="ArgumentNullException">
20+
/// Thrown when the <paramref name="error"/> is <see langword="null"/>.
21+
/// </exception>
22+
public static OperationException AsException(this IOperationError error)
23+
{
24+
ArgumentNullException.ThrowIfNull(error, nameof(error));
25+
return new OperationException(error.Code, error.Domain, error.Message, error.InnerError?.AsException());
26+
}
27+
}
28+
}

src/Deveel.Results/OperationResult.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,6 @@ private OperationResult(OperationResultType resultType, IOperationError? error)
3030
/// </summary>
3131
public static readonly OperationResult NotChanged = new(OperationResultType.Unchanged, null);
3232

33-
/// <summary>
34-
/// The result of an operation that has been cancelled.
35-
/// </summary>
36-
public static readonly OperationResult Cancelled = new(OperationResultType.Cancelled, null);
37-
3833
/// <summary>
3934
/// Creates a new instance of an operation result that has failed.
4035
/// </summary>

src/Deveel.Results/OperationResultExtensions.cs

Lines changed: 125 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -33,19 +33,6 @@ public static bool IsSuccess(this IOperationResult result)
3333
public static bool IsError(this IOperationResult result)
3434
=> result.ResultType == OperationResultType.Error;
3535

36-
/// <summary>
37-
/// Determines if the operation result is cancelled.
38-
/// </summary>
39-
/// <param name="result">
40-
/// The operation result to check.
41-
/// </param>
42-
/// <returns>
43-
/// Returns <see langword="true"/> if the operation result is cancelled,
44-
/// otherwise <see langword="false"/>.
45-
/// </returns>
46-
public static bool IsCancelled(this IOperationResult result)
47-
=> result.ResultType == OperationResultType.Cancelled;
48-
4936
/// <summary>
5037
/// Determines if the operation has caused no changes
5138
/// to the state of an object.
@@ -197,5 +184,130 @@ public static Task<TResult> MatchAsync<TResult>(this IOperationResult result,
197184

198185
throw new InvalidOperationException("The operation result is in an unknown state.");
199186
}
187+
188+
/// <summary>
189+
/// Attempts to match the operation result to a specific state
190+
/// that can be handled by the caller.
191+
/// </summary>
192+
/// <typeparam name="TResult">
193+
/// The type of the result that is returned by the match.
194+
/// </typeparam>
195+
/// <param name="result">
196+
/// The operation result to match.
197+
/// </param>
198+
/// <param name="ifSuccess">
199+
/// A function that is called when the operation result was a success.
200+
/// </param>
201+
/// <param name="ifError">
202+
/// A function that is called when the operation result was an error.
203+
/// </param>
204+
/// <param name="ifUnchanged">
205+
/// A function that is called when the operation result caused no changed
206+
/// to the object.
207+
/// </param>
208+
/// <returns>
209+
/// Returns the result of the function that was called based on the state
210+
/// of the operation result.
211+
/// </returns>
212+
/// <exception cref="InvalidOperationException">
213+
/// Thrown when the operation result is in an unknown state.
214+
/// </exception>
215+
public static TResult Match<T, TResult>(this IOperationResult<T> result,
216+
Func<T?, TResult>? ifSuccess = null,
217+
Func<IOperationError?, TResult>? ifError = null,
218+
Func<T?, TResult>? ifUnchanged = null)
219+
{
220+
ArgumentNullException.ThrowIfNull(result, nameof(result));
221+
222+
if (result.IsSuccess())
223+
{
224+
ArgumentNullException.ThrowIfNull(ifSuccess, nameof(ifSuccess));
225+
return ifSuccess(result.Value);
226+
}
227+
228+
if (result.IsError())
229+
{
230+
ArgumentNullException.ThrowIfNull(ifError, nameof(ifError));
231+
return ifError(result.Error);
232+
}
233+
234+
if (result.IsUnchanged())
235+
{
236+
ArgumentNullException.ThrowIfNull(ifUnchanged, nameof(ifUnchanged));
237+
return ifUnchanged(result.Value);
238+
}
239+
240+
throw new InvalidOperationException("The operation result is in an unknown state.");
241+
}
242+
243+
/// <summary>
244+
/// Attempts to match the operation result to a specific state
245+
/// that can be handled by the caller.
246+
/// </summary>
247+
/// <typeparam name="TResult">
248+
/// The type of the result that is returned by the match.
249+
/// </typeparam>
250+
/// <param name="result">
251+
/// The operation result to match.
252+
/// </param>
253+
/// <param name="ifSuccess">
254+
/// A function that is called when the operation result was a success.
255+
/// </param>
256+
/// <param name="ifError">
257+
/// A function that is called when the operation result was an error.
258+
/// </param>
259+
/// <param name="ifUnchanged">
260+
/// A function that is called when the operation result caused no changed
261+
/// to the object.
262+
/// </param>
263+
/// <returns>
264+
/// Returns the result of the function that was called based on the state
265+
/// of the operation result.
266+
/// </returns>
267+
/// <exception cref="InvalidOperationException">
268+
/// Thrown when the operation result is in an unknown state.
269+
/// </exception>
270+
public static Task<TResult> MatchAsync<T, TResult>(this IOperationResult<T> result,
271+
Func<T?, Task<TResult>>? ifSuccess = null,
272+
Func<IOperationError?, Task<TResult>>? ifError = null,
273+
Func<T?, Task<TResult>>? ifUnchanged = null)
274+
{
275+
ArgumentNullException.ThrowIfNull(result, nameof(result));
276+
277+
if (result.IsSuccess())
278+
{
279+
ArgumentNullException.ThrowIfNull(ifSuccess, nameof(ifSuccess));
280+
return ifSuccess(result.Value);
281+
}
282+
283+
if (result.IsError())
284+
{
285+
ArgumentNullException.ThrowIfNull(ifError, nameof(ifError));
286+
return ifError(result.Error);
287+
}
288+
289+
if (result.IsUnchanged())
290+
{
291+
ArgumentNullException.ThrowIfNull(ifUnchanged, nameof(ifUnchanged));
292+
return ifUnchanged(result.Value);
293+
}
294+
295+
throw new InvalidOperationException("The operation result is in an unknown state.");
296+
}
297+
298+
299+
300+
/// <summary>
301+
/// Converts an error result to an exception.
302+
/// </summary>
303+
/// <param name="result"></param>
304+
/// <returns></returns>
305+
public static OperationException? AsException(this IOperationResult result)
306+
{
307+
if (!result.IsError() || result.Error == null)
308+
return null;
309+
310+
return result.Error.AsException();
311+
}
200312
}
201313
}

src/Deveel.Results/OperationResultType.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,6 @@ public enum OperationResultType
2323
/// <summary>
2424
/// The operation caused no changes.
2525
/// </summary>
26-
Unchanged = 3,
27-
28-
/// <summary>
29-
/// The operation was cancelled.
30-
/// </summary>
31-
Cancelled = 4
26+
Unchanged = 3
3227
}
3328
}

src/Deveel.Results/OperationResult_T.cs

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,6 @@ private OperationResult(OperationResultType resultType, T? value, IOperationErro
2626
/// <inheritdoc/>
2727
public IOperationError? Error { get; }
2828

29-
/// <summary>
30-
/// A result of an operation that has not changed the state
31-
/// of an object.
32-
/// </summary>
33-
public static readonly OperationResult<T> NotChanged = new(OperationResultType.Unchanged, default, null);
34-
35-
/// <summary>
36-
/// A result of an operation that has been cancelled.
37-
/// </summary>
38-
public static readonly OperationResult<T> Cancelled = new(OperationResultType.Cancelled, default, null);
39-
4029
/// <summary>
4130
/// Creates a new instance of an operation result that has succeeded
4231
/// with the given value.
@@ -50,6 +39,21 @@ public static OperationResult<T> Success(T? value)
5039
return new OperationResult<T>(OperationResultType.Success, value, null);
5140
}
5241

42+
/// <summary>
43+
/// Creates a new instance of an operation result that has caused
44+
/// no change in the state of an object.
45+
/// </summary>
46+
/// <param name="value">
47+
/// The value that represents the object that was attempted
48+
/// to be changed.
49+
/// </param>
50+
/// <returns>
51+
/// Returns an instance of <see cref="OperationResult{T}"/> that
52+
/// represents an unchanged operation.
53+
/// </returns>
54+
public static OperationResult<T> NotChanged(T? value = default)
55+
=> new(OperationResultType.Unchanged, value, null);
56+
5357
/// <summary>
5458
/// Creates a new instance of an operation result that has failed
5559
/// because of an error.
@@ -153,5 +157,19 @@ public static implicit operator OperationResult<T>(OperationResult result)
153157
/// </param>
154158
/// <seealso cref="Fail(IOperationError)"/>
155159
public static implicit operator OperationResult<T>(OperationException error) => Fail(error);
160+
161+
/// <summary>
162+
/// Implicitly converts the result to is value
163+
/// </summary>
164+
/// <param name="result">
165+
/// The operation result that encapsulates the value to convert to.
166+
/// </param>
167+
public static implicit operator T?(OperationResult<T> result)
168+
{
169+
if (result.IsError())
170+
throw result.AsException()!;
171+
172+
return result.Value;
173+
}
156174
}
157175
}

0 commit comments

Comments
 (0)