1+ using System ;
2+ using System . Collections . Generic ;
3+ using System . Linq ;
4+ using System . Text ;
5+
6+ namespace FluentResults
7+ {
8+ /// <summary>
9+ /// Extensions methods for IResultBase
10+ /// </summary>
11+ /// <remarks>
12+ /// Adds methods, that are else defined in ResultBase, so that
13+ /// they are also available for IResultBase.
14+ /// </remarks>
15+ public static class ResultBaseExtensions
16+ {
17+ #region Methods from ResultBase
18+
19+ /// <inheritdoc cref="ResultBase.HasError{TError}()"/>
20+ public static bool HasError < TError > ( this IResultBase resultBase ) where TError : IError
21+ {
22+ return HasError < TError > ( resultBase , out _ ) ;
23+ }
24+
25+ /// <inheritdoc cref="ResultBase.HasError{TError}(out IEnumerable{TError})"/>
26+ public static bool HasError < TError > ( this IResultBase resultBase , out IEnumerable < TError > result ) where TError : IError
27+ {
28+ return HasError < TError > ( resultBase , e => true , out result ) ;
29+ }
30+
31+ /// <inheritdoc cref="ResultBase.HasError{TError}(Func{TError, bool})"/>
32+ public static bool HasError < TError > ( this IResultBase resultBase , Func < TError , bool > predicate ) where TError : IError
33+ {
34+ return HasError < TError > ( resultBase , predicate , out _ ) ;
35+ }
36+
37+ /// <inheritdoc cref="ResultBase.HasError{TError}(Func{TError, bool}, out IEnumerable{TError})"/>
38+ public static bool HasError < TError > ( this IResultBase resultBase , Func < TError , bool > predicate , out IEnumerable < TError > result ) where TError : IError
39+ {
40+ if ( predicate == null )
41+ throw new ArgumentNullException ( nameof ( predicate ) ) ;
42+
43+ return ResultHelper . HasError ( resultBase . Errors , predicate , out result ) ;
44+ }
45+
46+ /// <inheritdoc cref="ResultBase.HasError(Func{IError, bool})"/>
47+ public static bool HasError ( this IResultBase resultBase , Func < IError , bool > predicate )
48+ {
49+ return HasError ( resultBase , predicate , out _ ) ;
50+ }
51+
52+ /// <inheritdoc cref="ResultBase.HasError(Func{IError, bool}, out IEnumerable{IError})"/>
53+ public static bool HasError ( this IResultBase resultBase , Func < IError , bool > predicate , out IEnumerable < IError > result )
54+ {
55+ if ( predicate == null )
56+ throw new ArgumentNullException ( nameof ( predicate ) ) ;
57+
58+ return ResultHelper . HasError ( resultBase . Errors , predicate , out result ) ;
59+ }
60+
61+ /// <inheritdoc cref="ResultBase.HasException{TException}()"/>
62+ public static bool HasException < TException > ( this IResultBase resultBase ) where TException : Exception
63+ {
64+ return HasException < TException > ( resultBase , out _ ) ;
65+ }
66+
67+ /// <inheritdoc cref="ResultBase.HasException{TException}(out IEnumerable{IError})"/>
68+ public static bool HasException < TException > ( this IResultBase resultBase , out IEnumerable < IError > result ) where TException : Exception
69+ {
70+ return HasException < TException > ( resultBase , error => true , out result ) ;
71+ }
72+
73+ /// <inheritdoc cref="ResultBase.HasException{TException}(Func{TException, bool})"/>
74+ public static bool HasException < TException > ( this IResultBase resultBase , Func < TException , bool > predicate ) where TException : Exception
75+ {
76+ return HasException ( resultBase , predicate , out _ ) ;
77+ }
78+
79+ /// <inheritdoc cref="ResultBase.HasException{TException}(Func{TException, bool}, out IEnumerable{IError})"/>
80+ public static bool HasException < TException > ( this IResultBase resultBase , Func < TException , bool > predicate , out IEnumerable < IError > result ) where TException : Exception
81+ {
82+ if ( predicate == null )
83+ throw new ArgumentNullException ( nameof ( predicate ) ) ;
84+
85+ return ResultHelper . HasException ( resultBase . Errors , predicate , out result ) ;
86+ }
87+
88+ /// <inheritdoc cref="ResultBase.HasSuccess{TSuccess}()" />
89+ public static bool HasSuccess < TSuccess > ( this IResultBase resultBase ) where TSuccess : ISuccess
90+ {
91+ return HasSuccess < TSuccess > ( resultBase , success => true , out _ ) ;
92+ }
93+
94+ /// <inheritdoc cref="ResultBase.HasSuccess{TSuccess}(out IEnumerable{TSuccess})"/>
95+ public static bool HasSuccess < TSuccess > ( this IResultBase resultBase , out IEnumerable < TSuccess > result ) where TSuccess : ISuccess
96+ {
97+ return HasSuccess < TSuccess > ( resultBase , success => true , out result ) ;
98+ }
99+
100+ /// <inheritdoc cref="ResultBase.HasSuccess{TSuccess}(Func{TSuccess, bool})"/>
101+ public static bool HasSuccess < TSuccess > ( this IResultBase resultBase , Func < TSuccess , bool > predicate ) where TSuccess : ISuccess
102+ {
103+ return HasSuccess ( resultBase , predicate , out _ ) ;
104+ }
105+
106+ /// <inheritdoc cref="ResultBase.HasSuccess{TSuccess}(Func{TSuccess, bool}, out IEnumerable{TSuccess})"/>
107+ public static bool HasSuccess < TSuccess > ( this IResultBase resultBase , Func < TSuccess , bool > predicate , out IEnumerable < TSuccess > result ) where TSuccess : ISuccess
108+ {
109+ return ResultHelper . HasSuccess ( resultBase . Successes , predicate , out result ) ;
110+ }
111+
112+ /// <inheritdoc cref="ResultBase.HasSuccess(Func{ISuccess, bool}, out IEnumerable{ISuccess})"/>
113+ public static bool HasSuccess ( this IResultBase resultBase , Func < ISuccess , bool > predicate , out IEnumerable < ISuccess > result )
114+ {
115+ return ResultHelper . HasSuccess ( resultBase . Successes , predicate , out result ) ;
116+ }
117+
118+ /// <inheritdoc cref="ResultBase.HasSuccess{TSuccess}()"/>
119+ public static bool HasSuccess ( this IResultBase resultBase , Func < ISuccess , bool > predicate )
120+ {
121+ return ResultHelper . HasSuccess ( resultBase . Successes , predicate , out _ ) ;
122+ }
123+
124+ /// <inheritdoc cref="ResultBase.Deconstruct(out bool, out bool)"/>
125+ public static void Deconstruct ( this IResultBase resultBase , out bool isSuccess , out bool isFailed )
126+ {
127+ isSuccess = resultBase . IsSuccess ;
128+ isFailed = resultBase . IsFailed ;
129+ }
130+
131+ /// <inheritdoc cref="ResultBase.Deconstruct(out bool, out bool, out List{IError})"/>
132+ public static void Deconstruct ( this IResultBase resultBase , out bool isSuccess , out bool isFailed , out IReadOnlyList < IError > errors )
133+ {
134+ isSuccess = resultBase . IsSuccess ;
135+ isFailed = resultBase . IsFailed ;
136+ errors = isFailed ? resultBase . Errors : ( IReadOnlyList < IError > ) Array . Empty < IError > ( ) ;
137+ }
138+
139+ #endregion
140+
141+
142+ #region Methods from ResultBase<TResult> (return the same type, for fluent syntax)
143+
144+ /// <inheritdoc cref="ResultBase{TResult}.WithReason(IReason)"/>
145+ public static TResult WithReason < TResult > ( this TResult result , IReason reason )
146+ where TResult : IResultBase
147+ {
148+ result . Reasons . Add ( reason ) ;
149+ return result ;
150+ }
151+
152+ /// <inheritdoc cref="ResultBase{TResult}.WithReasons(IEnumerable{IReason})"/>
153+ public static TResult WithReasons < TResult > ( this TResult result , IEnumerable < IReason > reasons )
154+ where TResult : IResultBase
155+ {
156+ result . Reasons . AddRange ( reasons ) ;
157+ return result ;
158+ }
159+
160+ /// <inheritdoc cref="ResultBase{TResult}.WithError(string)"/>
161+ public static TResult WithError < TResult > ( this TResult result , string errorMessage )
162+ where TResult : IResultBase
163+ {
164+ return result . WithError ( Result . Settings . ErrorFactory ( errorMessage ) ) ;
165+ }
166+
167+ /// <inheritdoc cref="ResultBase{TResult}.WithError(IError)"/>
168+ public static TResult WithError < TResult > ( this TResult result , IError error )
169+ where TResult : IResultBase
170+ {
171+ return result . WithReason ( error ) ;
172+ }
173+
174+ /// <inheritdoc cref="ResultBase{TResult}.WithErrors(IEnumerable{IError})"/>
175+ public static TResult WithErrors < TResult > ( this TResult result , IEnumerable < IError > errors )
176+ where TResult : IResultBase
177+ {
178+ return result . WithReasons ( errors ) ;
179+ }
180+
181+ /// <inheritdoc cref="ResultBase{TResult}.WithErrors(IEnumerable{string})"/>
182+ public static TResult WithErrors < TResult > ( this TResult result , IEnumerable < string > errors )
183+ where TResult : IResultBase
184+ {
185+ return result . WithReasons ( errors . Select ( errorMessage => Result . Settings . ErrorFactory ( errorMessage ) ) ) ;
186+ }
187+
188+ /// <inheritdoc cref="ResultBase{TResult}.WithSuccess(string)"/>
189+ public static TResult WithSuccess < TResult > ( this TResult result , string successMessage )
190+ where TResult : IResultBase
191+ {
192+ return result . WithSuccess ( Result . Settings . SuccessFactory ( successMessage ) ) ;
193+ }
194+
195+ /// <inheritdoc cref="ResultBase{TResult}.WithSuccess(ISuccess)"/>
196+ public static TResult WithSuccess < TResult > ( this TResult result , ISuccess success )
197+ where TResult : IResultBase
198+ {
199+ return result . WithReason ( success ) ;
200+ }
201+
202+ /// <inheritdoc cref="ResultBase{TResult}.WithSuccesses(IEnumerable{ISuccess})"/>
203+ public static TResult WithSuccesses < TResult > ( this TResult result , IEnumerable < ISuccess > successes )
204+ where TResult : IResultBase
205+ {
206+ foreach ( var success in successes )
207+ {
208+ result . WithSuccess ( success ) ;
209+ }
210+
211+ return result ;
212+ }
213+
214+
215+ #endregion
216+
217+ }
218+
219+ }
0 commit comments