| | | 1 | | namespace ArturRios.Output; |
| | | 2 | | |
| | | 3 | | /// <summary> |
| | | 4 | | /// Represents the outcome of an operation and provides a standardized way to |
| | | 5 | | /// collect messages, errors and a timestamp. |
| | | 6 | | /// </summary> |
| | | 7 | | public class ProcessOutput |
| | | 8 | | { |
| | 59 | 9 | | private List<string> _messages = []; |
| | 59 | 10 | | private List<string> _errors = []; |
| | | 11 | | |
| | | 12 | | /// <summary> |
| | | 13 | | /// Collection of informational messages produced during the process. Never <c>null</c>: |
| | | 14 | | /// assigning <c>null</c> — as a deserializer does for an explicit JSON <c>null</c> — |
| | | 15 | | /// resets it to an empty list. |
| | | 16 | | /// </summary> |
| | | 17 | | public List<string> Messages |
| | | 18 | | { |
| | 61 | 19 | | get => _messages; |
| | 7 | 20 | | set => _messages = value ?? []; |
| | | 21 | | } |
| | | 22 | | |
| | | 23 | | /// <summary> |
| | | 24 | | /// Collection of error messages produced during the process. If this list |
| | | 25 | | /// is empty the <see cref="Success"/> property evaluates to <c>true</c>. |
| | | 26 | | /// Never <c>null</c>, on the same terms as <see cref="Messages"/>. |
| | | 27 | | /// </summary> |
| | | 28 | | public List<string> Errors |
| | | 29 | | { |
| | 80 | 30 | | get => _errors; |
| | 8 | 31 | | set => _errors = value ?? []; |
| | | 32 | | } |
| | | 33 | | |
| | | 34 | | /// <summary> |
| | | 35 | | /// UTC timestamp representing when this output instance was created. |
| | | 36 | | /// </summary> |
| | 100 | 37 | | public DateTime Timestamp { get; set; } = DateTime.UtcNow; |
| | | 38 | | |
| | | 39 | | /// <summary> |
| | | 40 | | /// Indicates whether the process completed successfully (no errors were added). |
| | | 41 | | /// </summary> |
| | 30 | 42 | | public bool Success => Errors.Count == 0; |
| | | 43 | | |
| | | 44 | | /// <summary> |
| | | 45 | | /// Creates a new <see cref="ProcessOutput"/> instance. |
| | | 46 | | /// </summary> |
| | 12 | 47 | | public static ProcessOutput New => new(); |
| | | 48 | | |
| | | 49 | | /// <summary> |
| | | 50 | | /// Adds a non-empty error message to the output. |
| | | 51 | | /// </summary> |
| | | 52 | | /// <param name="error">The error message to add.</param> |
| | | 53 | | public void AddError(string error) |
| | 13 | 54 | | { |
| | 13 | 55 | | if (string.IsNullOrWhiteSpace(error)) |
| | 5 | 56 | | { |
| | 5 | 57 | | return; |
| | | 58 | | } |
| | | 59 | | |
| | 8 | 60 | | Errors.Add(error); |
| | 13 | 61 | | } |
| | | 62 | | |
| | | 63 | | /// <summary> |
| | | 64 | | /// Adds multiple error messages to the output, ignoring empty or whitespace entries. |
| | | 65 | | /// </summary> |
| | | 66 | | /// <param name="errors">The collection of errors to add.</param> |
| | | 67 | | public void AddErrors(IEnumerable<string> errors) => |
| | 20 | 68 | | Errors.AddRange(errors.Where(e => !string.IsNullOrWhiteSpace(e)).ToList()); |
| | | 69 | | |
| | | 70 | | |
| | | 71 | | /// <summary> |
| | | 72 | | /// Adds a non-empty informational message to the output. |
| | | 73 | | /// </summary> |
| | | 74 | | /// <param name="message">The message to add.</param> |
| | | 75 | | public void AddMessage(string message) |
| | 17 | 76 | | { |
| | 17 | 77 | | if (string.IsNullOrWhiteSpace(message)) |
| | 6 | 78 | | { |
| | 6 | 79 | | return; |
| | | 80 | | } |
| | | 81 | | |
| | 11 | 82 | | Messages.Add(message); |
| | 17 | 83 | | } |
| | | 84 | | |
| | | 85 | | /// <summary> |
| | | 86 | | /// Adds multiple informational messages to the output, ignoring empty or whitespace entries. |
| | | 87 | | /// </summary> |
| | | 88 | | /// <param name="messages">The collection of messages to add.</param> |
| | | 89 | | public void AddMessages(IEnumerable<string> messages) => |
| | 19 | 90 | | Messages.AddRange(messages.Where(e => !string.IsNullOrWhiteSpace(e)).ToList()); |
| | | 91 | | |
| | | 92 | | /// <summary> |
| | | 93 | | /// Fluent helper to add a single error and return the same instance. |
| | | 94 | | /// </summary> |
| | | 95 | | /// <param name="error">The error to add.</param> |
| | | 96 | | /// <returns>The same <see cref="ProcessOutput"/> instance for chaining.</returns> |
| | | 97 | | public ProcessOutput WithError(string error) |
| | 2 | 98 | | { |
| | 2 | 99 | | AddError(error); |
| | | 100 | | |
| | 2 | 101 | | return this; |
| | 2 | 102 | | } |
| | | 103 | | |
| | | 104 | | /// <summary> |
| | | 105 | | /// Fluent helper to add multiple errors and return the same instance. |
| | | 106 | | /// </summary> |
| | | 107 | | /// <param name="errors">The errors to add.</param> |
| | | 108 | | /// <returns>The same <see cref="ProcessOutput"/> instance for chaining.</returns> |
| | | 109 | | public ProcessOutput WithErrors(IEnumerable<string> errors) |
| | 1 | 110 | | { |
| | 1 | 111 | | AddErrors(errors); |
| | | 112 | | |
| | 1 | 113 | | return this; |
| | 1 | 114 | | } |
| | | 115 | | |
| | | 116 | | /// <summary> |
| | | 117 | | /// Fluent helper to add a single informational message and return the same instance. |
| | | 118 | | /// </summary> |
| | | 119 | | /// <param name="message">The message to add.</param> |
| | | 120 | | /// <returns>The same <see cref="ProcessOutput"/> instance for chaining.</returns> |
| | | 121 | | public ProcessOutput WithMessage(string message) |
| | 2 | 122 | | { |
| | 2 | 123 | | AddMessage(message); |
| | | 124 | | |
| | 2 | 125 | | return this; |
| | 2 | 126 | | } |
| | | 127 | | |
| | | 128 | | /// <summary> |
| | | 129 | | /// Fluent helper to add multiple informational messages and return the same instance. |
| | | 130 | | /// </summary> |
| | | 131 | | /// <param name="messages">The messages to add.</param> |
| | | 132 | | /// <returns>The same <see cref="ProcessOutput"/> instance for chaining.</returns> |
| | | 133 | | public ProcessOutput WithMessages(IEnumerable<string> messages) |
| | 1 | 134 | | { |
| | 1 | 135 | | AddMessages(messages); |
| | | 136 | | |
| | 1 | 137 | | return this; |
| | 1 | 138 | | } |
| | | 139 | | } |