#nullable enable using System.Diagnostics.CodeAnalysis; namespace Terminal.Gui; /// /// An exception thrown when something goes wrong when trying to parse a . /// /// /// Contains additional information to help locate the problem. ///
/// Not intended to be thrown by consumers. ///
public sealed class ColorParseException : FormatException { internal const string DefaultMessage = "Failed to parse text as Color."; internal ColorParseException (string colorString, string? message, Exception? innerException = null) : base (message ?? DefaultMessage, innerException) { ColorString = colorString; } internal ColorParseException (string colorString, string? message = DefaultMessage) : base (message) { ColorString = colorString; } /// /// Creates a new instance of a populated with the provided values. /// /// The text that caused this exception, as a . /// /// The specific value in that caused this exception. /// /// /// The name of the value (red, green, blue, alpha) that represents. /// /// The reason that failed to parse. internal ColorParseException (in ReadOnlySpan colorString, string reason, in ReadOnlySpan badValue = default, in ReadOnlySpan badValueName = default) : base (DefaultMessage) { ColorString = colorString.ToString (); BadValue = badValue.ToString (); BadValueName = badValueName.ToString (); Reason = reason; } /// /// Creates a new instance of a populated with the provided values. /// /// The text that caused this exception, as a . /// /// The specific value in that caused this exception. /// /// /// The name of the value (red, green, blue, alpha) that represents. /// /// The reason that failed to parse. internal ColorParseException (in ReadOnlySpan colorString, string? badValue = null, string? badValueName = null, string? reason = null) : base (DefaultMessage) { ColorString = colorString.ToString (); BadValue = badValue; BadValueName = badValueName; Reason = reason; } /// Gets the text that failed to parse, as a /// /// Is marked , so must be set by a constructor or initializer. /// public string ColorString { get; } /// /// Gets the substring of caused this exception, as a /// /// May be null or empty - only set if known. public string? BadValue { get; } /// /// Gets the name of the color component corresponding to , if known. /// /// May be null or empty - only set if known. public string? BadValueName { get; } /// Gets the reason that failed to parse, if known. /// May be null or empty - only set if known. public string? Reason { get; } [DoesNotReturn] internal static void Throw (in ReadOnlySpan colorString, string reason, in ReadOnlySpan badValue = default, in ReadOnlySpan badValueName = default) => throw new ColorParseException (in colorString, reason, in badValue, in badValueName); [DoesNotReturn] internal static void ThrowIfNotAsciiDigits (in ReadOnlySpan valueText, string reason, in ReadOnlySpan badValue = default, in ReadOnlySpan badValueName = default) => throw new ColorParseException (in valueText, reason, in badValue, in badValueName); }