Skip to content

Commit

Permalink
Implement display on all error types (#5969)
Browse files Browse the repository at this point in the history
Fixes #5930


<!--
Thank you for your pull request to ICU4X!

Reminder: try to use [Conventional
Comments](https://conventionalcomments.org/) to make comments clearer.

Please see
https://github.com/unicode-org/icu4x/blob/main/CONTRIBUTING.md for
general
information on contributing to ICU4X.
-->
  • Loading branch information
Manishearth authored Jan 9, 2025
1 parent 4c760e8 commit 2ea039e
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 11 deletions.
4 changes: 3 additions & 1 deletion components/calendar/src/ixdtf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ use ixdtf::parsers::IxdtfParser;
use ixdtf::ParseError as IxdtfError;

/// An error returned from parsing an IXDTF string to an `icu_calendar` type.
#[derive(Debug)]
#[derive(Debug, displaydoc::Display)]
#[non_exhaustive]
pub enum ParseError {
/// Syntax error in the IXDTF string.
#[displaydoc("Syntax error in the IXDTF string: {0}")]
Syntax(IxdtfError),
/// Value is out of range.
#[displaydoc("Value out of range: {0}")]
Range(RangeError),
/// The IXDTF is missing fields required for parsing into the chosen type.
MissingFields,
Expand Down
6 changes: 5 additions & 1 deletion components/experimental/src/duration/validated_options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,19 +61,23 @@ pub struct ValidatedDurationFormatterOptions {

/// Error type for [`DurationFormatterOptions`] validation.
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, displaydoc::Display)]
pub enum DurationFormatterOptionsError {
/// Returned when a unit field is set to [`FieldDisplay::Always`] and the style is set to [`FieldStyle::Fractional`].
#[displaydoc("Returned when a unit field is set to Always and the style is set to Fractional")]
DisplayAlwaysFractional,

/// Returned when a previous unit's style is [`FieldStyle::Fractional`], but the following unit's style is not [`FieldStyle::Fractional`].
#[displaydoc("Returned when a previous unit's style is Fractional, but the following unit's style is not Fractional")]
PreviousFractional,

/// Returned when a previous unit's style is set to [`FieldStyle::Numeric`] or [`FieldStyle::TwoDigit`] and the following unit's style is not
/// [`FieldStyle::Fractional`], [`FieldStyle::Numeric`], or [`FieldStyle::TwoDigit`].
#[displaydoc("Returned when a previous unit's style is set to Numeric or TwoDigit and the following unit's style is not Fractional, Numeric, or TwoDigit")]
PreviousNumeric,

/// Returned when the number of fractional digits is out of acceptable range. See [`FractionalDigits::Fixed`].
#[displaydoc("Returned when the number of fractional digits is out of acceptable range")]
FractionalDigitsOutOfRange,
}

Expand Down
20 changes: 14 additions & 6 deletions components/experimental/src/unicodeset_parse/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,20 @@ use icu_properties::{provider::*, PropertyParser};
use icu_provider::prelude::*;

/// The kind of error that occurred.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, displaydoc::Display)]
#[non_exhaustive]
pub enum ParseErrorKind {
/// An unexpected character was encountered. This variant implies the other variants
/// An unexpected character was encountered.
///
/// This variant implies the other variants
/// (notably `UnknownProperty` and `Unimplemented`) do not apply.
#[displaydoc("An unexpected character was encountered")]
UnexpectedChar(char),
/// The property name or value is unknown. For property names, make sure you use the spelling
/// The property name or value is unknown.
///
/// For property names, make sure you use the spelling
/// defined in [ECMA-262](https://tc39.es/ecma262/#table-nonbinary-unicode-properties).
#[displaydoc("The property name or value is unknown")]
UnknownProperty,
/// A reference to an unknown variable.
UnknownVariable,
Expand All @@ -47,11 +53,13 @@ pub enum ParseErrorKind {
Eof,
/// Something unexpected went wrong with our code. Please file a bug report on GitHub.
Internal,
/// The provided syntax is not supported by us. Note that unknown properties will return the
/// The provided syntax is not supported by us.
///
/// Note that unknown properties will return the
/// `UnknownProperty` variant, not this one.
#[displaydoc("The provided syntax is not supported by us.")]
Unimplemented,
/// The provided escape sequence is not a valid Unicode code point or represents too many
/// code points.
/// The provided escape sequence is not a valid Unicode code point or represents too many code points.
InvalidEscape,
}
use zerovec::VarZeroVec;
Expand Down
18 changes: 17 additions & 1 deletion components/experimental/src/units/ratio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,29 +22,45 @@ use crate::measure::provider::si_prefix::{Base, SiPrefix};
pub struct IcuRatio(Ratio<BigInt>);

/// Represents an error when the ratio string is invalid and cannot be parsed.
#[derive(Debug, PartialEq)]
#[derive(Debug, PartialEq, displaydoc::Display)]
pub enum RatioFromStrError {
/// Represents an error when the ratio string is divided by zero.
DivisionByZero,

/// Represents an error when the ratio string contains multiple slashes.
///
/// For example, "1/2/3".
#[displaydoc("Represents an error when the ratio string contains multiple slashes")]
MultipleSlashes,

/// Represents an error when the ratio string contains non-numeric characters in fractions.
///
/// For example, "1/2A".
#[displaydoc(
"Represents an error when the ratio string contains non-numeric characters in fractions"
)]
NonNumericCharactersInFractions,

/// Represents an error when the ratio string contains multiple scientific notations.
///
/// For example, "1.5E6E6".
#[displaydoc(
"Represents an error when the ratio string contains multiple scientific notations"
)]
MultipleScientificNotations,

/// Represents an error when the ratio string contains multiple decimal points.
///
/// For example, "1.5.6".
#[displaydoc("Represents an error when the ratio string contains multiple decimal points")]
MultipleDecimalPoints,

/// Represents an error when the exponent part of the ratio string is not an integer.
///
/// For example, "1.5E6.5".
#[displaydoc(
"Represents an error when the exponent part of the ratio string is not an integer"
)]
ExponentPartIsNotAnInteger,

/// Represents an error when the ratio string is dificient in some other way.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/// Error returned by parsers of unicode extensions as preferences.
#[non_exhaustive]
#[derive(Debug)]
#[derive(Debug, displaydoc::Display)]
pub enum PreferencesParseError {
/// The given keyword value is not a valid preference variant.
InvalidKeywordValue,
Expand Down
9 changes: 8 additions & 1 deletion components/timezone/src/ixdtf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,17 @@ use ixdtf::{
};

/// The error type for parsing IXDTF syntax strings in `icu_timezone`.
#[derive(Debug, PartialEq)]
#[derive(Debug, PartialEq, displaydoc::Display)]
#[non_exhaustive]
pub enum ParseError {
/// Syntax error for IXDTF string.
#[displaydoc("Syntax error in the IXDTF string: {0}")]
Syntax(IxdtfParseError),
/// Parsed record is out of valid date range.
#[displaydoc("Value out of range: {0}")]
Range(RangeError),
/// Parsed date and time records were not a valid ISO date.
#[displaydoc("Parsed date and time records were not a valid ISO date: {0}")]
Date(DateError),
/// There were missing fields required to parse component.
MissingFields,
Expand All @@ -40,6 +43,7 @@ pub enum ParseError {
/// The set of time zone fields was not expected for the given type.
/// For example, if a named time zone was present with offset-only parsing,
/// or an offset was present with named-time-zone-only parsing.
#[displaydoc("The set of time zone fields was not expected for the given type")]
MismatchedTimeZoneFields,
/// An unknown calendar was provided.
UnknownCalendar,
Expand All @@ -59,6 +63,9 @@ pub enum ParseError {
/// IxdtfParser::new().try_loose_iso_from_str("2024-08-12T14:32:00+02:00[Europe/Zurich]").unwrap();
/// IxdtfParser::new().try_loose_iso_from_str("2024-08-12T14:32:00[Europe/Zurich]").unwrap();
/// ```
#[displaydoc(
"A timezone calculation is required to interpret this string, which is not supported"
)]
RequiresCalculation,
}

Expand Down

0 comments on commit 2ea039e

Please sign in to comment.