| | | 1 | | using System.Collections.ObjectModel; |
| | | 2 | | using AsiBackbone.Core.Results; |
| | | 3 | | |
| | | 4 | | namespace AsiBackbone.Core.Constraints; |
| | | 5 | | |
| | | 6 | | /// <summary> |
| | | 7 | | /// Represents the framework-neutral result of evaluating a constraint. |
| | | 8 | | /// </summary> |
| | | 9 | | public sealed class ConstraintEvaluationResult |
| | | 10 | | { |
| | | 11 | | private const string DefaultDeniedCode = "constraint.denied"; |
| | | 12 | | private const string DefaultDeniedMessage = "Constraint denied the operation."; |
| | | 13 | | private const string DefaultWarningCode = "constraint.warning"; |
| | | 14 | | private const string DefaultWarningMessage = "Constraint produced a warning."; |
| | | 15 | | |
| | 4 | 16 | | private static readonly OperationReason[] EmptyReasons = []; |
| | | 17 | | |
| | 4 | 18 | | private static readonly ReadOnlyCollection<string> EmptyReasonCodes = |
| | 4 | 19 | | Array.AsReadOnly(Array.Empty<string>()); |
| | | 20 | | |
| | 4 | 21 | | private static readonly ConstraintEvaluationResult AllowedResult = |
| | 4 | 22 | | new(ConstraintEvaluationOutcome.Allowed, EmptyReasons); |
| | | 23 | | |
| | 4 | 24 | | private static readonly ConstraintEvaluationResult NotApplicableResult = |
| | 4 | 25 | | new(ConstraintEvaluationOutcome.NotApplicable, EmptyReasons); |
| | | 26 | | |
| | 215 | 27 | | private ConstraintEvaluationResult( |
| | 215 | 28 | | ConstraintEvaluationOutcome outcome, |
| | 215 | 29 | | IReadOnlyList<OperationReason> reasons) |
| | | 30 | | { |
| | 215 | 31 | | Outcome = outcome; |
| | 215 | 32 | | Reasons = reasons; |
| | 215 | 33 | | ReasonCodes = CreateReasonCodes(reasons); |
| | 215 | 34 | | } |
| | | 35 | | |
| | | 36 | | /// <summary> |
| | | 37 | | /// Gets the constraint evaluation outcome. |
| | | 38 | | /// </summary> |
| | 1044 | 39 | | public ConstraintEvaluationOutcome Outcome { get; } |
| | | 40 | | |
| | | 41 | | /// <summary> |
| | | 42 | | /// Gets reasons associated with denied or warning outcomes. |
| | | 43 | | /// </summary> |
| | 339 | 44 | | public IReadOnlyList<OperationReason> Reasons { get; } |
| | | 45 | | |
| | | 46 | | /// <summary> |
| | | 47 | | /// Gets machine-readable reason codes associated with denied or warning outcomes. |
| | | 48 | | /// </summary> |
| | 46 | 49 | | public IReadOnlyList<string> ReasonCodes { get; } |
| | | 50 | | |
| | | 51 | | /// <summary> |
| | | 52 | | /// Gets a value indicating whether the constraint allows the operation to proceed. |
| | | 53 | | /// </summary> |
| | 20 | 54 | | public bool CanProceed => Outcome is not ConstraintEvaluationOutcome.Denied; |
| | | 55 | | |
| | | 56 | | /// <summary> |
| | | 57 | | /// Gets a value indicating whether this result denies the operation. |
| | | 58 | | /// </summary> |
| | 532 | 59 | | public bool IsDenied => Outcome is ConstraintEvaluationOutcome.Denied; |
| | | 60 | | |
| | | 61 | | /// <summary> |
| | | 62 | | /// Gets a value indicating whether this result contains warnings. |
| | | 63 | | /// </summary> |
| | 446 | 64 | | public bool IsWarning => Outcome is ConstraintEvaluationOutcome.Warning; |
| | | 65 | | |
| | | 66 | | /// <summary> |
| | | 67 | | /// Gets a value indicating whether this result is not applicable to the supplied context. |
| | | 68 | | /// </summary> |
| | 12 | 69 | | public bool IsNotApplicable => Outcome is ConstraintEvaluationOutcome.NotApplicable; |
| | | 70 | | |
| | | 71 | | /// <summary> |
| | | 72 | | /// Gets a value indicating whether the result contains reason data. |
| | | 73 | | /// </summary> |
| | 8 | 74 | | public bool HasReasons => Reasons.Count > 0; |
| | | 75 | | |
| | | 76 | | /// <summary> |
| | | 77 | | /// Creates an allowed constraint result. |
| | | 78 | | /// </summary> |
| | | 79 | | /// <returns>An allowed constraint evaluation result.</returns> |
| | | 80 | | public static ConstraintEvaluationResult Allow() |
| | | 81 | | { |
| | 142 | 82 | | return AllowedResult; |
| | | 83 | | } |
| | | 84 | | |
| | | 85 | | /// <summary> |
| | | 86 | | /// Creates a not-applicable constraint result. |
| | | 87 | | /// </summary> |
| | | 88 | | /// <returns>A not-applicable constraint evaluation result.</returns> |
| | | 89 | | public static ConstraintEvaluationResult NotApplicable() |
| | | 90 | | { |
| | 34 | 91 | | return NotApplicableResult; |
| | | 92 | | } |
| | | 93 | | |
| | | 94 | | /// <summary> |
| | | 95 | | /// Creates a denied constraint result. |
| | | 96 | | /// </summary> |
| | | 97 | | /// <param name="code">The machine-readable reason code.</param> |
| | | 98 | | /// <param name="message">The human-readable reason message.</param> |
| | | 99 | | /// <returns>A denied constraint evaluation result.</returns> |
| | | 100 | | public static ConstraintEvaluationResult Deny(string code, string message) |
| | | 101 | | { |
| | 93 | 102 | | return Deny(OperationReason.Create(code, message)); |
| | | 103 | | } |
| | | 104 | | |
| | | 105 | | /// <summary> |
| | | 106 | | /// Creates a denied constraint result. |
| | | 107 | | /// </summary> |
| | | 108 | | /// <param name="reason">The reason associated with the denied result.</param> |
| | | 109 | | /// <returns>A denied constraint evaluation result.</returns> |
| | | 110 | | public static ConstraintEvaluationResult Deny(OperationReason reason) |
| | | 111 | | { |
| | 97 | 112 | | ArgumentNullException.ThrowIfNull(reason); |
| | | 113 | | |
| | 95 | 114 | | return new ConstraintEvaluationResult( |
| | 95 | 115 | | ConstraintEvaluationOutcome.Denied, |
| | 95 | 116 | | Array.AsReadOnly([reason])); |
| | | 117 | | } |
| | | 118 | | |
| | | 119 | | /// <summary> |
| | | 120 | | /// Creates a denied constraint result. |
| | | 121 | | /// </summary> |
| | | 122 | | /// <param name="reasons">The reasons associated with the denied result.</param> |
| | | 123 | | /// <returns>A denied constraint evaluation result.</returns> |
| | | 124 | | public static ConstraintEvaluationResult Deny(IEnumerable<OperationReason> reasons) |
| | | 125 | | { |
| | 16 | 126 | | return new ConstraintEvaluationResult( |
| | 16 | 127 | | ConstraintEvaluationOutcome.Denied, |
| | 16 | 128 | | NormalizeReasons(reasons, DefaultDeniedCode, DefaultDeniedMessage)); |
| | | 129 | | } |
| | | 130 | | |
| | | 131 | | /// <summary> |
| | | 132 | | /// Creates a warning constraint result. |
| | | 133 | | /// </summary> |
| | | 134 | | /// <param name="code">The machine-readable reason code.</param> |
| | | 135 | | /// <param name="message">The human-readable reason message.</param> |
| | | 136 | | /// <returns>A warning constraint evaluation result.</returns> |
| | | 137 | | public static ConstraintEvaluationResult Warning(string code, string message) |
| | | 138 | | { |
| | 74 | 139 | | return Warning(OperationReason.Create(code, message)); |
| | | 140 | | } |
| | | 141 | | |
| | | 142 | | /// <summary> |
| | | 143 | | /// Creates a warning constraint result. |
| | | 144 | | /// </summary> |
| | | 145 | | /// <param name="reason">The reason associated with the warning result.</param> |
| | | 146 | | /// <returns>A warning constraint evaluation result.</returns> |
| | | 147 | | public static ConstraintEvaluationResult Warning(OperationReason reason) |
| | | 148 | | { |
| | 78 | 149 | | ArgumentNullException.ThrowIfNull(reason); |
| | | 150 | | |
| | 76 | 151 | | return new ConstraintEvaluationResult( |
| | 76 | 152 | | ConstraintEvaluationOutcome.Warning, |
| | 76 | 153 | | Array.AsReadOnly([reason])); |
| | | 154 | | } |
| | | 155 | | |
| | | 156 | | /// <summary> |
| | | 157 | | /// Creates a warning constraint result. |
| | | 158 | | /// </summary> |
| | | 159 | | /// <param name="reasons">The reasons associated with the warning result.</param> |
| | | 160 | | /// <returns>A warning constraint evaluation result.</returns> |
| | | 161 | | public static ConstraintEvaluationResult Warning(IEnumerable<OperationReason> reasons) |
| | | 162 | | { |
| | 10 | 163 | | return new ConstraintEvaluationResult( |
| | 10 | 164 | | ConstraintEvaluationOutcome.Warning, |
| | 10 | 165 | | NormalizeReasons(reasons, DefaultWarningCode, DefaultWarningMessage)); |
| | | 166 | | } |
| | | 167 | | |
| | | 168 | | private static ReadOnlyCollection<OperationReason> NormalizeReasons( |
| | | 169 | | IEnumerable<OperationReason>? reasons, |
| | | 170 | | string fallbackCode, |
| | | 171 | | string fallbackMessage) |
| | | 172 | | { |
| | 26 | 173 | | if (reasons is null) |
| | | 174 | | { |
| | 4 | 175 | | return CreateFallbackReason(fallbackCode, fallbackMessage); |
| | | 176 | | } |
| | | 177 | | |
| | 22 | 178 | | if (reasons is ICollection<OperationReason> collection) |
| | | 179 | | { |
| | 18 | 180 | | if (collection.Count == 0) |
| | | 181 | | { |
| | 4 | 182 | | return CreateFallbackReason(fallbackCode, fallbackMessage); |
| | | 183 | | } |
| | | 184 | | |
| | 14 | 185 | | var normalizedReasons = new OperationReason[collection.Count]; |
| | 14 | 186 | | int normalizedCount = 0; |
| | | 187 | | |
| | 84 | 188 | | foreach (OperationReason? reason in collection) |
| | | 189 | | { |
| | 28 | 190 | | if (reason is not null) |
| | | 191 | | { |
| | 16 | 192 | | normalizedReasons[normalizedCount] = reason; |
| | 16 | 193 | | normalizedCount++; |
| | | 194 | | } |
| | | 195 | | } |
| | | 196 | | |
| | 14 | 197 | | if (normalizedCount == 0) |
| | | 198 | | { |
| | 4 | 199 | | return CreateFallbackReason(fallbackCode, fallbackMessage); |
| | | 200 | | } |
| | | 201 | | |
| | 10 | 202 | | if (normalizedCount == normalizedReasons.Length) |
| | | 203 | | { |
| | 2 | 204 | | return Array.AsReadOnly(normalizedReasons); |
| | | 205 | | } |
| | | 206 | | |
| | 8 | 207 | | var filteredReasons = new OperationReason[normalizedCount]; |
| | 8 | 208 | | Array.Copy(normalizedReasons, filteredReasons, normalizedCount); |
| | | 209 | | |
| | 8 | 210 | | return Array.AsReadOnly(filteredReasons); |
| | | 211 | | } |
| | | 212 | | |
| | 4 | 213 | | List<OperationReason>? normalizedList = null; |
| | | 214 | | |
| | 16 | 215 | | foreach (OperationReason? reason in reasons) |
| | | 216 | | { |
| | 4 | 217 | | if (reason is not null) |
| | | 218 | | { |
| | 4 | 219 | | normalizedList ??= []; |
| | 4 | 220 | | normalizedList.Add(reason); |
| | | 221 | | } |
| | | 222 | | } |
| | | 223 | | |
| | 4 | 224 | | return normalizedList is null || normalizedList.Count == 0 |
| | 4 | 225 | | ? CreateFallbackReason(fallbackCode, fallbackMessage) |
| | 4 | 226 | | : Array.AsReadOnly([.. normalizedList]); |
| | | 227 | | } |
| | | 228 | | |
| | | 229 | | private static ReadOnlyCollection<OperationReason> CreateFallbackReason( |
| | | 230 | | string fallbackCode, |
| | | 231 | | string fallbackMessage) |
| | | 232 | | { |
| | 14 | 233 | | return Array.AsReadOnly([OperationReason.Create(fallbackCode, fallbackMessage)]); |
| | | 234 | | } |
| | | 235 | | |
| | | 236 | | private static ReadOnlyCollection<string> CreateReasonCodes(IReadOnlyList<OperationReason> reasons) |
| | | 237 | | { |
| | 215 | 238 | | if (reasons.Count == 0) |
| | | 239 | | { |
| | 12 | 240 | | return EmptyReasonCodes; |
| | | 241 | | } |
| | | 242 | | |
| | 203 | 243 | | string[] reasonCodes = new string[reasons.Count]; |
| | | 244 | | |
| | 828 | 245 | | for (int index = 0; index < reasons.Count; index++) |
| | | 246 | | { |
| | 211 | 247 | | reasonCodes[index] = reasons[index].Code; |
| | | 248 | | } |
| | | 249 | | |
| | 203 | 250 | | return Array.AsReadOnly(reasonCodes); |
| | | 251 | | } |
| | | 252 | | } |