ColorParseException.cs 4.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. #nullable enable
  2. using System.Diagnostics.CodeAnalysis;
  3. namespace Terminal.Gui;
  4. /// <summary>
  5. /// An exception thrown when something goes wrong when trying to parse a <see cref="Color" />.
  6. /// </summary>
  7. /// <remarks>
  8. /// Contains additional information to help locate the problem.
  9. /// <br />
  10. /// Not intended to be thrown by consumers.
  11. /// </remarks>
  12. public sealed class ColorParseException : FormatException {
  13. internal const string DefaultMessage = "Failed to parse text as Color.";
  14. internal ColorParseException (string colorString, string? message, Exception? innerException = null) : base (message ?? DefaultMessage, innerException)
  15. {
  16. ColorString = colorString;
  17. }
  18. internal ColorParseException (string colorString, string? message = DefaultMessage) : base (message)
  19. {
  20. ColorString = colorString;
  21. }
  22. /// <summary>
  23. /// Creates a new instance of a <see cref="ColorParseException" /> populated with the provided values.
  24. /// </summary>
  25. /// <param name="colorString">The text that caused this exception, as a <see langword="string" />.</param>
  26. /// <param name="badValue">
  27. /// The specific value in <paramref name="colorString" /> that caused this exception.
  28. /// </param>
  29. /// <param name="badValueName">
  30. /// The name of the value (red, green, blue, alpha) that <paramref name="badValue" /> represents.
  31. /// </param>
  32. /// <param name="reason">The reason that <paramref name="badValue" /> failed to parse.</param>
  33. internal ColorParseException (in ReadOnlySpan<char> colorString, string reason, in ReadOnlySpan<char> badValue = default, in ReadOnlySpan<char> badValueName = default) : base (DefaultMessage)
  34. {
  35. ColorString = colorString.ToString ();
  36. BadValue = badValue.ToString ();
  37. BadValueName = badValueName.ToString ();
  38. Reason = reason;
  39. }
  40. /// <summary>
  41. /// Creates a new instance of a <see cref="ColorParseException" /> populated with the provided values.
  42. /// </summary>
  43. /// <param name="colorString">The text that caused this exception, as a <see langword="string" />.</param>
  44. /// <param name="badValue">
  45. /// The specific value in <paramref name="colorString" /> that caused this exception.
  46. /// </param>
  47. /// <param name="badValueName">
  48. /// The name of the value (red, green, blue, alpha) that <paramref name="badValue" /> represents.
  49. /// </param>
  50. /// <param name="reason">The reason that <paramref name="badValue" /> failed to parse.</param>
  51. internal ColorParseException (in ReadOnlySpan<char> colorString, string? badValue = null, string? badValueName = null, string? reason = null) : base (DefaultMessage)
  52. {
  53. ColorString = colorString.ToString ();
  54. BadValue = badValue;
  55. BadValueName = badValueName;
  56. Reason = reason;
  57. }
  58. /// <summary>Gets the text that failed to parse, as a <see langword="string" /></summary>
  59. /// <remarks>
  60. /// Is marked <see langword="required" />, so must be set by a constructor or initializer.
  61. /// </remarks>
  62. public string ColorString { get; }
  63. /// <summary>
  64. /// Gets the substring of <see cref="ColorString" /> caused this exception, as a <see langword="string" />
  65. /// </summary>
  66. /// <remarks>May be null or empty - only set if known.</remarks>
  67. public string? BadValue { get; }
  68. /// <summary>
  69. /// Gets the name of the color component corresponding to <see cref="BadValue" />, if known.
  70. /// </summary>
  71. /// <remarks>May be null or empty - only set if known.</remarks>
  72. public string? BadValueName { get; }
  73. /// <summary>Gets the reason that <see cref="BadValue" /> failed to parse, if known.</summary>
  74. /// <remarks>May be null or empty - only set if known.</remarks>
  75. public string? Reason { get; }
  76. [DoesNotReturn]
  77. internal static void Throw (in ReadOnlySpan<char> colorString, string reason, in ReadOnlySpan<char> badValue = default, in ReadOnlySpan<char> badValueName = default) => throw new ColorParseException (in colorString, reason, in badValue, in badValueName);
  78. [DoesNotReturn]
  79. internal static void ThrowIfNotAsciiDigits (in ReadOnlySpan<char> valueText, string reason, in ReadOnlySpan<char> badValue = default, in ReadOnlySpan<char> badValueName = default) => throw new ColorParseException (in valueText, reason, in badValue, in badValueName);
  80. }