1- #if NET7_0_OR_GREATER
21using System . Diagnostics ;
3- #endif
4- #if NET6_0_OR_GREATER
52using System . Runtime . CompilerServices ;
6- #endif
73
84namespace LightResults . Extensions . ExceptionHandling ;
95
10-
11- #if NET6_0_OR_GREATER
126/// <summary>
137/// Provides extension methods for <see cref="IEnumerable{T}"/> and <see cref="IAsyncEnumerable{T}"/>
148/// that wrap enumeration in <see cref="Result{T}"/> to capture exceptions as failed results.
159/// </summary>
16- #else
17- /// <summary>
18- /// Provides extension methods for <see cref="IEnumerable{T}"/>
19- /// that wrap enumeration in <see cref="Result{T}"/> to capture exceptions as failed results.
20- /// </summary>
21- #endif
2210public static class EnumerableExtensions
2311{
24- #if NET7_0_OR_GREATER
2512 /// <summary>
2613 /// Enumerates the elements of the sequence, wrapping each element in a <see cref="Result{T}"/>.
2714 /// If an exception occurs during enumeration, yields a failed <see cref="Result{T}"/> containing the exception.
@@ -34,28 +21,9 @@ public static class EnumerableExtensions
3421 /// </returns>
3522 /// <exception cref="ArgumentNullException"><paramref name="source"/> is <c>null</c>.</exception>
3623 /// <exception cref="UnreachableException">The enumerator could not be obtained from <paramref name="source"/>.</exception>
37- #else
38- /// <summary>
39- /// Enumerates the elements of the sequence, wrapping each element in a <see cref="Result{T}"/>.
40- /// If an exception occurs during enumeration, yields a failed <see cref="Result{T}"/> containing the exception.
41- /// </summary>
42- /// <typeparam name="T">The type of the elements of <paramref name="source"/>.</typeparam>
43- /// <param name="source">The sequence to enumerate.</param>
44- /// <returns>
45- /// An <see cref="IEnumerable{T}"/> of <see cref="Result{T}"/> objects, where each successful result contains
46- /// an item from <paramref name="source"/>, or a failure result contains the exception thrown during enumeration.
47- /// </returns>
48- /// <exception cref="ArgumentNullException"><paramref name="source"/> is <c>null</c>.</exception>
49- /// <exception cref="InvalidOperationException">The enumerator could not be obtained from <paramref name="source"/>.</exception>
50- #endif
5124 public static IEnumerable < Result < T > > AsEnumerableResult < T > ( this IEnumerable < T > source )
5225 {
53- #if NET6_0_OR_GREATER
5426 ArgumentNullException . ThrowIfNull ( source ) ;
55- #else
56- if ( source is null )
57- throw new ArgumentNullException ( nameof ( source ) ) ;
58- #endif
5927
6028 Exception ? exception = null ;
6129
@@ -77,11 +45,7 @@ public static IEnumerable<Result<T>> AsEnumerableResult<T>(this IEnumerable<T> s
7745 }
7846
7947 if ( enumerator is null )
80- #if NET7_0_OR_GREATER
8148 throw new UnreachableException ( "The enumerator was unexpectedly null." ) ;
82- #else
83- throw new InvalidOperationException ( "The enumerator was unexpectedly null." ) ;
84- #endif
8549
8650 var hasMoreItems = true ;
8751
@@ -129,8 +93,6 @@ public static IEnumerable<Result<T>> AsEnumerableResult<T>(this IEnumerable<T> s
12993 }
13094 }
13195
132- #if NET6_0_OR_GREATER
133- #if NET7_0_OR_GREATER
13496 /// <summary>
13597 /// Asynchronously enumerates the elements of the sequence, wrapping each element in a <see cref="Result{T}"/>.
13698 /// If an exception occurs during asynchronous enumeration, yields a failed <see cref="Result{T}"/> containing the exception.
@@ -144,21 +106,6 @@ public static IEnumerable<Result<T>> AsEnumerableResult<T>(this IEnumerable<T> s
144106 /// </returns>
145107 /// <exception cref="ArgumentNullException"><paramref name="source"/> is <c>null</c>.</exception>
146108 /// <exception cref="UnreachableException">The asynchronous enumerator could not be obtained from <paramref name="source"/>.</exception>
147- #else
148- /// <summary>
149- /// Asynchronously enumerates the elements of the sequence, wrapping each element in a <see cref="Result{T}"/>.
150- /// If an exception occurs during asynchronous enumeration, yields a failed <see cref="Result{T}"/> containing the exception.
151- /// </summary>
152- /// <typeparam name="T">The type of the elements of <paramref name="source"/>.</typeparam>
153- /// <param name="source">The asynchronous sequence to enumerate.</param>
154- /// <param name="cancellationToken">A cancellation token that can be used to cancel the asynchronous enumeration.</param>
155- /// <returns>
156- /// An <see cref="IAsyncEnumerable{T}"/> of <see cref="Result{T}"/> objects, where each successful result contains
157- /// an item from <paramref name="source"/>, or a failure result contains the exception thrown during asynchronous enumeration.
158- /// </returns>
159- /// <exception cref="ArgumentNullException"><paramref name="source"/> is <c>null</c>.</exception>
160- /// <exception cref="InvalidOperationException">The asynchronous enumerator could not be obtained from <paramref name="source"/>.</exception>
161- #endif
162109 public static async IAsyncEnumerable < Result < T > > AsAsyncEnumerableResult < T > (
163110 this IAsyncEnumerable < T > source ,
164111 [ EnumeratorCancellation ] CancellationToken cancellationToken = default
@@ -188,11 +135,7 @@ await enumerator.DisposeAsync()
188135 }
189136
190137 if ( enumerator is null )
191- #if NET7_0_OR_GREATER
192138 throw new UnreachableException ( "The enumerator was unexpectedly null." ) ;
193- #else
194- throw new InvalidOperationException ( "The enumerator was unexpectedly null." ) ;
195- #endif
196139
197140 var hasMoreItems = true ;
198141
@@ -242,5 +185,4 @@ await enumerator.DisposeAsync()
242185 yield return Result . Success ( current ) ;
243186 }
244187 }
245- #endif
246188}
0 commit comments