ColorParseException.cs 4.2 KB

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