-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathResult.cs
More file actions
105 lines (84 loc) · 3.24 KB
/
Copy pathResult.cs
File metadata and controls
105 lines (84 loc) · 3.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
using System.Diagnostics.CodeAnalysis;
namespace SimpleModule.Core;
/// <summary>
/// Represents the outcome of an operation that may fail with a structured error.
/// </summary>
/// <typeparam name="T">The type of the success value.</typeparam>
[SuppressMessage(
"Design",
"CA1000:Do not declare static members on generic types",
Justification = "Standard Result<T> pattern requires static factory methods"
)]
public readonly struct Result<T> : IEquatable<Result<T>>
{
private readonly T? _value;
private readonly ResultError? _error;
private Result(T value)
{
_value = value;
_error = null;
IsSuccess = true;
}
private Result(ResultError error)
{
_value = default;
_error = error;
IsSuccess = false;
}
public bool IsSuccess { get; }
public bool IsFailure => !IsSuccess;
public T Value =>
IsSuccess
? _value!
: throw new InvalidOperationException("Cannot access Value on a failed Result.");
public ResultError Error =>
!IsSuccess
? _error!
: throw new InvalidOperationException("Cannot access Error on a successful Result.");
public static Result<T> Ok(T value) => new(value);
public static Result<T> Fail(string message) => new(new ResultError(message));
public static Result<T> Fail(string message, Dictionary<string, string[]> validationErrors) =>
new(new ResultError(message, validationErrors));
/// <summary>
/// Creates a <see cref="Result{T}"/> from a value (alternate for implicit operator).
/// </summary>
[SuppressMessage(
"Design",
"CA2225:Operator overloads have named alternates",
Justification = "ToResult provided"
)]
public static implicit operator Result<T>(T value) => Ok(value);
/// <summary>
/// Named alternate for the implicit conversion operator.
/// </summary>
public static Result<T> FromValue(T value) => Ok(value);
/// <summary>
/// Maps the success value to a new type using the provided function.
/// </summary>
public Result<TOut> Map<TOut>(Func<T, TOut> map) =>
IsSuccess
? Result<TOut>.Ok(map(_value!))
: Result<TOut>.Fail(
Error.Message,
Error.ValidationErrors ?? new Dictionary<string, string[]>()
);
/// <summary>
/// Returns the value if successful, or the provided fallback value.
/// </summary>
public T ValueOr(T fallback) => IsSuccess ? _value! : fallback;
public bool Equals(Result<T> other) =>
IsSuccess == other.IsSuccess
&& EqualityComparer<T?>.Default.Equals(_value, other._value)
&& Equals(_error, other._error);
public override bool Equals(object? obj) => obj is Result<T> other && Equals(other);
public override int GetHashCode() => HashCode.Combine(IsSuccess, _value, _error);
public static bool operator ==(Result<T> left, Result<T> right) => left.Equals(right);
public static bool operator !=(Result<T> left, Result<T> right) => !left.Equals(right);
}
/// <summary>
/// Describes why an operation failed.
/// </summary>
public sealed record ResultError(
string Message,
Dictionary<string, string[]>? ValidationErrors = null
);