@@ -70,8 +70,8 @@ public Result<U, TError> Map<U>(
70
70
/// <summary>
71
71
/// Evaluates the mapError delegate if Result is Error otherwise return Ok.
72
72
/// </summary>
73
- /// <typeparam name="U "></typeparam>
74
- /// <param name="map "></param>
73
+ /// <typeparam name="UError "></typeparam>
74
+ /// <param name="mapError "></param>
75
75
/// <returns></returns>
76
76
public Result < T , UError > MapError < UError > (
77
77
Func < TError , UError > mapError ) =>
@@ -144,15 +144,37 @@ public static Task<Result<T, TError>> ErrorAsync(TError errors) =>
144
144
public static async Task < Result < T , TError > > ErrorAsync ( Task < TError > errors ) =>
145
145
Error ( await errors ) ;
146
146
147
+ /// <summary>
148
+ /// Returns true if the specified Result is equal to the current Result.
149
+ /// </summary>
150
+ /// <param name="left"></param>
151
+ /// <param name="right"></param>
152
+ /// <returns></returns>
147
153
public static bool operator == ( Result < T , TError > left , Result < T , TError > right ) =>
148
154
left . Equals ( right ) ;
149
155
156
+ /// <summary>
157
+ /// Returns true if the specified Result is not equal to the current Result.
158
+ /// </summary>
159
+ /// <param name="left"></param>
160
+ /// <param name="right"></param>
161
+ /// <returns></returns>
150
162
public static bool operator != ( Result < T , TError > left , Result < T , TError > right ) =>
151
163
! ( left == right ) ;
152
164
165
+ /// <summary>
166
+ /// Returns true if the specified Result is equal to the current Result.
167
+ /// </summary>
168
+ /// <param name="obj"></param>
169
+ /// <returns></returns>
153
170
public override bool Equals ( object ? obj ) =>
154
171
obj is Result < T , TError > o && Equals ( o ) ;
155
172
173
+ /// <summary>
174
+ /// Returns true if the specified Result is equal to the current Result.
175
+ /// </summary>
176
+ /// <param name="other"></param>
177
+ /// <returns></returns>
156
178
public readonly bool Equals ( Result < T , TError > other ) =>
157
179
Match (
158
180
ok : x1 =>
@@ -164,13 +186,96 @@ public readonly bool Equals(Result<T, TError> other) =>
164
186
ok : _ => false ,
165
187
error : e2 => e2 is not null && e2 . Equals ( e1 ) ) ) ;
166
188
189
+ /// <summary>
190
+ /// Returns the hash code for the Result.
191
+ /// </summary>
192
+ /// <returns></returns>
167
193
public override int GetHashCode ( ) =>
168
194
Match (
169
195
ok : x => x is null ? 0 : x . GetHashCode ( ) ,
170
196
error : e => e is null ? 0 : e . GetHashCode ( ) ) ;
171
197
198
+ /// <summary>
199
+ /// Returns a string representation of the Result.
200
+ /// </summary>
201
+ /// <returns></returns>
172
202
public override string ToString ( ) =>
173
203
Match (
174
204
ok : x => $ "Ok({ x } )",
175
205
error : e => $ "Error({ e } )") ;
176
206
}
207
+
208
+
209
+ /// <summary>
210
+ /// The <see cref="Result{T, TError}"/> with <see cref="ResultErrors"/>
211
+ /// as the predefined error type.
212
+ ///
213
+ /// Alias for <see cref="Result{T, ResultErrors}"/>.
214
+ /// </summary>
215
+ /// <typeparam name="T"></typeparam>
216
+ public static class Result < T >
217
+ {
218
+ /// <summary>
219
+ /// Creates a new Result with the specified value.
220
+ /// </summary>
221
+ /// <param name="value"></param>
222
+ /// <returns></returns>
223
+ public static Result < T , ResultErrors > Ok ( T value ) =>
224
+ Result < T , ResultErrors > . Ok ( value ) ;
225
+
226
+ /// <summary>
227
+ /// Creates Result with the specified value wrapped in a completed Task.
228
+ /// </summary>
229
+ /// <param name="value"></param>
230
+ /// <returns></returns>
231
+ public static Task < Result < T , ResultErrors > > OkAsync ( T value ) =>
232
+ Result < T , ResultErrors > . OkAsync ( value ) ;
233
+
234
+ /// <summary>
235
+ /// Creates Result with the value of the awaited Task.
236
+ /// </summary>
237
+ /// <param name="valueTask"></param>
238
+ /// <returns></returns>
239
+ public static Task < Result < T , ResultErrors > > OkAsync ( Task < T > valueTask ) =>
240
+ Result < T , ResultErrors > . OkAsync ( valueTask ) ;
241
+
242
+ /// <summary>
243
+ /// Creates a new Result with the specified error.
244
+ /// </summary>
245
+ /// <param name="errors"></param>
246
+ /// <returns></returns>
247
+ public static Result < T , ResultErrors > Error ( ResultErrors errors ) =>
248
+ Result < T , ResultErrors > . Error ( errors ) ;
249
+
250
+ /// <summary>
251
+ /// Creates Result with the specified error wrapped in a completed Task.
252
+ /// </summary>
253
+ /// <param name="errors"></param>
254
+ /// <returns></returns>
255
+ public static Task < Result < T , ResultErrors > > ErrorAsync ( ResultErrors errors ) =>
256
+ Task . FromResult ( Error ( errors ) ) ;
257
+
258
+ /// <summary>
259
+ /// Creates a new Result with the specified error.
260
+ /// </summary>
261
+ /// <param name="messages"></param>
262
+ /// <returns></returns>
263
+ public static Result < T , ResultErrors > Error ( IEnumerable < string > messages ) =>
264
+ Error ( new ResultErrors ( messages ) ) ;
265
+
266
+ /// <summary>
267
+ /// Creates Result with the specified error wrapped in a completed Task.
268
+ /// </summary>
269
+ /// <param name="message"></param>
270
+ /// <returns></returns>
271
+ public static Result < T , ResultErrors > Error ( string message ) =>
272
+ Error ( [ message ] ) ;
273
+
274
+ /// <summary>
275
+ /// Creates Result with the specified error wrapped in a completed Task.
276
+ /// </summary>
277
+ /// <param name="message"></param>
278
+ /// <returns></returns>
279
+ public static Task < Result < T , ResultErrors > > ErrorAsync ( string message ) =>
280
+ Task . FromResult ( Error ( message ) ) ;
281
+ }
0 commit comments