11using System ;
2+ using System . Collections . Generic ;
23
34namespace ManagedCode . Communication ;
45
5- public class Result
6+ public class Result : BaseResult < ErrorCode >
67{
7- public bool IsSuccess { get ; }
8- public bool IsError => ! IsSuccess ;
9- public Error ? Error { get ; }
10-
11- protected Result ( Exception exception )
8+ internal Result ( bool isSuccess ) : base ( isSuccess )
129 {
13- IsSuccess = false ;
14- Error = new Error ( exception ) ;
1510 }
1611
17- protected Result ( string errorMessage )
12+ internal Result ( Error < ErrorCode > error ) : base ( error )
1813 {
19- IsSuccess = false ;
20- Error = new Error ( errorMessage ) ;
2114 }
2215
23- protected Result ( bool isSuccess )
16+ internal Result ( List < Error < ErrorCode > > errors ) : base ( errors )
2417 {
25- IsSuccess = isSuccess ;
26- Error = null ;
2718 }
2819
29- protected Result ( Error error )
30- {
31- IsSuccess = false ;
32- Error = error ;
33- }
3420
35- public static Result Succeed ( )
36- {
37- return new Result ( true ) ;
38- }
21+ public static implicit operator Result ( Error < ErrorCode > error ) => new ( error ) ;
22+ public static implicit operator Result ( List < Error < ErrorCode > > errors ) => new ( errors ) ;
3923
40- public static Result Fail ( )
41- {
42- return new Result ( false ) ;
43- }
4424
45- public static Result Fail ( Error error )
46- {
47- return new Result ( error ) ;
48- }
25+ public static Result Success ( ) => new ( true ) ;
26+ public static Result Fail ( ) => new ( false ) ;
27+ public static Result Fail ( Error < ErrorCode > error ) => new ( error ) ;
28+ public static Result Fail ( List < Error < ErrorCode > > errors ) => new ( errors ) ;
29+ }
4930
50- public static Result Fail ( Exception error )
31+ public class Result < T > : BaseResult < T , ErrorCode >
32+ {
33+ internal Result ( T value ) : base ( value )
5134 {
52- return new Result ( error ) ;
5335 }
5436
55- public static Result Fail ( string errorMessage )
37+ internal Result ( bool isSuccess ) : base ( isSuccess )
5638 {
57- return new Result ( errorMessage ) ;
5839 }
5940
60- public static Result < T > Succeed < T > ( T content )
41+ internal Result ( Error < ErrorCode > error ) : base ( error )
6142 {
62- return new Result < T > ( true , content ) ;
6343 }
6444
65- public static Result < T > Fail < T > ( )
45+ internal Result ( List < Error < ErrorCode > > errors ) : base ( errors )
6646 {
67- return new Result < T > ( false ) ;
6847 }
6948
70- public static Result < T > Fail < T > ( Error error )
71- {
72- return new Result < T > ( error ) ;
73- }
7449
75- public static Result < T > Fail < T > ( Exception exception )
76- {
77- return new Result < T > ( exception ) ;
78- }
50+ public static implicit operator Result < T > ( T value ) => new ( value ) ;
51+ public static implicit operator Result < T > ( Error < ErrorCode > error ) => new ( error ) ;
52+ public static implicit operator Result < T > ( List < Error < ErrorCode > > errors ) => new ( errors ) ;
53+
7954
80- public static Result < T > Fail < T > ( string errorMessage )
55+ public static Result < T > Success ( T value ) => new ( value ) ;
56+ public static Result < T > Fail ( ) => new ( false ) ;
57+ public static Result < T > Fail ( Error < ErrorCode > error ) => new ( error ) ;
58+ public static Result < T > Fail ( List < Error < ErrorCode > > errors ) => new ( errors ) ;
59+
60+
61+ public Result < T > WithError ( Error < ErrorCode > error )
8162 {
82- return new Result < T > ( errorMessage ) ;
63+ if ( IsSuccess )
64+ {
65+ throw new InvalidOperationException ( "Cannot add error to success result" ) ;
66+ }
67+
68+ Errors ! . Add ( error ) ;
69+ return this ;
8370 }
8471}
0 commit comments