| | | 1 | | using ArturRios.Output; |
| | | 2 | | |
| | | 3 | | namespace ArturRios.Util.FlowControl; |
| | | 4 | | |
| | | 5 | | /// <summary> |
| | | 6 | | /// A fluent helper to evaluate multiple boolean conditions and aggregate failures. |
| | | 7 | | /// </summary> |
| | | 8 | | /// <remarks> |
| | | 9 | | /// Use <see cref="True"/> or <see cref="False"/> to set the result of an expression and chain <see cref="FailsWith"/> t |
| | | 10 | | /// After building all conditions call <see cref="ThrowIfNotSatisfied"/> or inspect <see cref="IsSatisfied"/> / <see cre |
| | | 11 | | /// </remarks> |
| | | 12 | | public class Condition |
| | | 13 | | { |
| | 4 | 14 | | private readonly HashSet<string> _failedConditions = []; |
| | | 15 | | private bool _expression; |
| | | 16 | | |
| | | 17 | | /// <summary> |
| | | 18 | | /// Creates a new <see cref="Condition"/> instance. Syntactic sugar for <c>new Condition()</c>. |
| | | 19 | | /// </summary> |
| | 4 | 20 | | public static Condition Create => new(); |
| | | 21 | | |
| | | 22 | | /// <summary> |
| | | 23 | | /// Gets an array of all failure messages collected by <see cref="FailsWith"/>. |
| | | 24 | | /// </summary> |
| | 0 | 25 | | public string[] FailedConditions => _failedConditions.ToArray(); |
| | | 26 | | |
| | | 27 | | /// <summary> |
| | | 28 | | /// Indicates whether all evaluated conditions have succeeded (i.e. there are no failures). |
| | | 29 | | /// </summary> |
| | 4 | 30 | | public bool IsSatisfied => _failedConditions.Count == 0; |
| | | 31 | | |
| | | 32 | | /// <summary> |
| | | 33 | | /// Sets the current expression result to <paramref name="expression"/>. |
| | | 34 | | /// </summary> |
| | | 35 | | /// <param name="expression">The boolean value representing the success of the condition.</param> |
| | | 36 | | /// <returns>The current <see cref="Condition"/> for fluent chaining.</returns> |
| | | 37 | | public Condition True(bool expression) |
| | 5 | 38 | | { |
| | 5 | 39 | | _expression = expression; |
| | | 40 | | |
| | 5 | 41 | | return this; |
| | 5 | 42 | | } |
| | | 43 | | |
| | | 44 | | /// <summary> |
| | | 45 | | /// Sets the current expression result to the negated value of <paramref name="expression"/>. |
| | | 46 | | /// </summary> |
| | | 47 | | /// <param name="expression">The boolean to negate.</param> |
| | | 48 | | /// <returns>The current <see cref="Condition"/> for fluent chaining.</returns> |
| | | 49 | | public Condition False(bool expression) |
| | 2 | 50 | | { |
| | 2 | 51 | | _expression = !expression; |
| | | 52 | | |
| | 2 | 53 | | return this; |
| | 2 | 54 | | } |
| | | 55 | | |
| | | 56 | | /// <summary> |
| | | 57 | | /// Adds an error message if the most recently set expression evaluated to <c>false</c>. |
| | | 58 | | /// </summary> |
| | | 59 | | /// <param name="error">The error message describing the failure.</param> |
| | | 60 | | /// <returns>The current <see cref="Condition"/> for fluent chaining.</returns> |
| | | 61 | | public Condition FailsWith(string error) |
| | 7 | 62 | | { |
| | 7 | 63 | | if (!_expression) |
| | 2 | 64 | | { |
| | 2 | 65 | | _failedConditions.Add(error); |
| | 2 | 66 | | } |
| | | 67 | | |
| | 7 | 68 | | return this; |
| | 7 | 69 | | } |
| | | 70 | | |
| | | 71 | | /// <summary> |
| | | 72 | | /// Throws a <see cref="ConditionFailedException"/> if any condition failed. |
| | | 73 | | /// </summary> |
| | | 74 | | /// <exception cref="ConditionFailedException">Thrown when <see cref="IsSatisfied"/> is false.</exception> |
| | | 75 | | public void ThrowIfNotSatisfied() |
| | 0 | 76 | | { |
| | 0 | 77 | | if (IsSatisfied) |
| | 0 | 78 | | { |
| | 0 | 79 | | return; |
| | | 80 | | } |
| | | 81 | | |
| | 0 | 82 | | throw new ConditionFailedException(FailedConditions); |
| | 0 | 83 | | } |
| | | 84 | | |
| | | 85 | | /// <summary> |
| | | 86 | | /// Converts the condition failures into a <see cref="ProcessOutput"/> instance. |
| | | 87 | | /// </summary> |
| | | 88 | | /// <returns>A <see cref="ProcessOutput"/> with errors populated when the condition is not satisfied.</returns> |
| | | 89 | | public ProcessOutput ToProcessOutput() |
| | 4 | 90 | | { |
| | 4 | 91 | | var output = new ProcessOutput(); |
| | | 92 | | |
| | 4 | 93 | | if (!IsSatisfied) |
| | 2 | 94 | | { |
| | 2 | 95 | | output.AddErrors(_failedConditions.ToList()); |
| | 2 | 96 | | } |
| | | 97 | | |
| | 4 | 98 | | return output; |
| | 4 | 99 | | } |
| | | 100 | | } |
| | | 101 | | |
| | | 102 | | /// <summary> |
| | | 103 | | /// Exception raised when one or more conditions fail in a <see cref="Condition"/> chain. |
| | | 104 | | /// </summary> |
| | | 105 | | /// <param name="errors">The set of failure messages collected.</param> |
| | | 106 | | public class ConditionFailedException(string[] errors) : Exception($"A total of {errors.Length} conditions failed") |
| | | 107 | | { |
| | | 108 | | /// <summary> |
| | | 109 | | /// The failure messages that caused this exception. |
| | | 110 | | /// </summary> |
| | | 111 | | public readonly string[] Errors = errors; |
| | | 112 | | } |