@@ -33,19 +33,6 @@ public static bool IsSuccess(this IOperationResult result)
33
33
public static bool IsError ( this IOperationResult result )
34
34
=> result . ResultType == OperationResultType . Error ;
35
35
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
-
49
36
/// <summary>
50
37
/// Determines if the operation has caused no changes
51
38
/// to the state of an object.
@@ -197,5 +184,130 @@ public static Task<TResult> MatchAsync<TResult>(this IOperationResult result,
197
184
198
185
throw new InvalidOperationException ( "The operation result is in an unknown state." ) ;
199
186
}
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
+ }
200
312
}
201
313
}
0 commit comments