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