Skip to content

Commit

Permalink
Rename to DecimalFormatter (#6075)
Browse files Browse the repository at this point in the history
Fixes #5953
  • Loading branch information
robertbastian authored Feb 6, 2025
1 parent dc8ffad commit 547c45c
Show file tree
Hide file tree
Showing 85 changed files with 1,019 additions and 1,051 deletions.
8 changes: 4 additions & 4 deletions components/datetime/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use crate::pattern::TypedDateTimeNames;
#[cfg(doc)]
use icu_calendar::types::YearInfo;
#[cfg(doc)]
use icu_decimal::FixedDecimalFormatter;
use icu_decimal::DecimalFormatter;

/// An error from constructing a formatter.
#[derive(Display, Debug, Copy, Clone, PartialEq)]
Expand Down Expand Up @@ -87,15 +87,15 @@ pub enum DateTimeWriteError {
max: usize,
},

/// The [`FixedDecimalFormatter`] has not been loaded.
/// The [`DecimalFormatter`] has not been loaded.
///
/// This *only* happens if the formatter has been created using
/// [`TypedDateTimeNames::with_pattern_unchecked`], the pattern requires decimal
/// formatting, and the decimal formatter was not loaded.
///
/// The output will contain fallback values using Latin numerals.
#[displaydoc("FixedDecimalFormatter not loaded")]
FixedDecimalFormatterNotLoaded,
#[displaydoc("DecimalFormatter not loaded")]
DecimalFormatterNotLoaded,
/// The localized names for a field have not been loaded.
///
/// This *only* happens if the formatter has been created using
Expand Down
54 changes: 27 additions & 27 deletions components/datetime/src/external_loaders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@
//! Internal traits and structs for loading data from other crates.
use icu_calendar::{AnyCalendar, AnyCalendarPreferences};
use icu_decimal::options::FixedDecimalFormatterOptions;
use icu_decimal::{FixedDecimalFormatter, FixedDecimalFormatterPreferences};
use icu_decimal::options::DecimalFormatterOptions;
use icu_decimal::{DecimalFormatter, DecimalFormatterPreferences};
use icu_provider::prelude::*;

/// Trait for loading a FixedDecimalFormatter.
/// Trait for loading a DecimalFormatter.
///
/// Implemented on the provider-specific loader types in this module.
pub(crate) trait FixedDecimalFormatterLoader {
pub(crate) trait DecimalFormatterLoader {
fn load(
&self,
prefs: FixedDecimalFormatterPreferences,
options: FixedDecimalFormatterOptions,
) -> Result<FixedDecimalFormatter, DataError>;
prefs: DecimalFormatterPreferences,
options: DecimalFormatterOptions,
) -> Result<DecimalFormatter, DataError>;
}

/// Trait for loading an AnyCalendar.
Expand All @@ -32,14 +32,14 @@ pub(crate) trait AnyCalendarLoader {
pub(crate) struct ExternalLoaderCompiledData;

#[cfg(feature = "compiled_data")]
impl FixedDecimalFormatterLoader for ExternalLoaderCompiledData {
impl DecimalFormatterLoader for ExternalLoaderCompiledData {
#[inline]
fn load(
&self,
prefs: FixedDecimalFormatterPreferences,
options: FixedDecimalFormatterOptions,
) -> Result<FixedDecimalFormatter, DataError> {
FixedDecimalFormatter::try_new(prefs, options)
prefs: DecimalFormatterPreferences,
options: DecimalFormatterOptions,
) -> Result<DecimalFormatter, DataError> {
DecimalFormatter::try_new(prefs, options)
}
}

Expand All @@ -54,17 +54,17 @@ impl AnyCalendarLoader for ExternalLoaderCompiledData {
/// Loader for types from other crates using [`AnyProvider`].
pub(crate) struct ExternalLoaderAny<'a, P: ?Sized>(pub &'a P);

impl<P> FixedDecimalFormatterLoader for ExternalLoaderAny<'_, P>
impl<P> DecimalFormatterLoader for ExternalLoaderAny<'_, P>
where
P: ?Sized + AnyProvider,
{
#[inline]
fn load(
&self,
prefs: FixedDecimalFormatterPreferences,
options: FixedDecimalFormatterOptions,
) -> Result<FixedDecimalFormatter, DataError> {
FixedDecimalFormatter::try_new_with_any_provider(self.0, prefs, options)
prefs: DecimalFormatterPreferences,
options: DecimalFormatterOptions,
) -> Result<DecimalFormatter, DataError> {
DecimalFormatter::try_new_with_any_provider(self.0, prefs, options)
}
}

Expand All @@ -83,17 +83,17 @@ where
pub(crate) struct ExternalLoaderBuffer<'a, P: ?Sized>(pub &'a P);

#[cfg(feature = "serde")]
impl<P> FixedDecimalFormatterLoader for ExternalLoaderBuffer<'_, P>
impl<P> DecimalFormatterLoader for ExternalLoaderBuffer<'_, P>
where
P: ?Sized + BufferProvider,
{
#[inline]
fn load(
&self,
prefs: FixedDecimalFormatterPreferences,
options: FixedDecimalFormatterOptions,
) -> Result<FixedDecimalFormatter, DataError> {
FixedDecimalFormatter::try_new_with_buffer_provider(self.0, prefs, options)
prefs: DecimalFormatterPreferences,
options: DecimalFormatterOptions,
) -> Result<DecimalFormatter, DataError> {
DecimalFormatter::try_new_with_buffer_provider(self.0, prefs, options)
}
}

Expand All @@ -111,7 +111,7 @@ where
/// Loader for types from other crates using [`DataProvider`].
pub(crate) struct ExternalLoaderUnstable<'a, P: ?Sized>(pub &'a P);

impl<P> FixedDecimalFormatterLoader for ExternalLoaderUnstable<'_, P>
impl<P> DecimalFormatterLoader for ExternalLoaderUnstable<'_, P>
where
P: ?Sized
+ DataProvider<icu_decimal::provider::DecimalSymbolsV2>
Expand All @@ -120,10 +120,10 @@ where
#[inline]
fn load(
&self,
prefs: FixedDecimalFormatterPreferences,
options: FixedDecimalFormatterOptions,
) -> Result<FixedDecimalFormatter, DataError> {
FixedDecimalFormatter::try_new_unstable(self.0, prefs, options)
prefs: DecimalFormatterPreferences,
options: DecimalFormatterOptions,
) -> Result<DecimalFormatter, DataError> {
DecimalFormatter::try_new_unstable(self.0, prefs, options)
}
}

Expand Down
44 changes: 22 additions & 22 deletions components/datetime/src/format/datetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ use crate::{parts, pattern::*};
use core::fmt::{self, Write};
use fixed_decimal::SignedFixedDecimal;
use icu_calendar::types::{DayOfWeekInMonth, IsoWeekday};
use icu_decimal::FixedDecimalFormatter;
use icu_decimal::DecimalFormatter;
use writeable::{Part, PartsWrite, Writeable};

/// Apply length to input number and write to result using fixed_decimal_format.
/// Apply length to input number and write to result using decimal_formatter.
fn try_write_number<W>(
part: Part,
w: &mut W,
fixed_decimal_format: Option<&FixedDecimalFormatter>,
decimal_formatter: Option<&DecimalFormatter>,
mut num: SignedFixedDecimal,
length: FieldLength,
) -> Result<Result<(), DateTimeWriteError>, fmt::Error>
Expand All @@ -29,22 +29,22 @@ where
{
num.pad_start(length.to_len() as i16);

if let Some(fdf) = fixed_decimal_format {
if let Some(fdf) = decimal_formatter {
w.with_part(part, |w| fdf.format(&num).write_to_parts(w))?;
Ok(Ok(()))
} else {
w.with_part(part, |w| {
w.with_part(writeable::Part::ERROR, |r| num.write_to_parts(r))
})?;
Ok(Err(DateTimeWriteError::FixedDecimalFormatterNotLoaded))
Ok(Err(DateTimeWriteError::DecimalFormatterNotLoaded))
}
}

/// Apply length to input number and write to result using fixed_decimal_format.
/// Apply length to input number and write to result using decimal_formatter.
/// Don't annotate it with a part.
fn try_write_number_without_part<W>(
w: &mut W,
fixed_decimal_format: Option<&FixedDecimalFormatter>,
decimal_formatter: Option<&DecimalFormatter>,
mut num: SignedFixedDecimal,
length: FieldLength,
) -> Result<Result<(), DateTimeWriteError>, fmt::Error>
Expand All @@ -53,12 +53,12 @@ where
{
num.pad_start(length.to_len() as i16);

if let Some(fdf) = fixed_decimal_format {
if let Some(fdf) = decimal_formatter {
fdf.format(&num).write_to(w)?;
Ok(Ok(()))
} else {
w.with_part(writeable::Part::ERROR, |r| num.write_to(r))?;
Ok(Err(DateTimeWriteError::FixedDecimalFormatterNotLoaded))
Ok(Err(DateTimeWriteError::DecimalFormatterNotLoaded))
}
}

Expand All @@ -68,7 +68,7 @@ pub(crate) fn try_write_pattern_items<W>(
pattern_items: impl Iterator<Item = PatternItem>,
input: &ExtractedInput,
datetime_names: &RawDateTimeNamesBorrowed,
fixed_decimal_format: Option<&FixedDecimalFormatter>,
decimal_formatter: Option<&DecimalFormatter>,
w: &mut W,
) -> Result<Result<(), DateTimeWriteError>, fmt::Error>
where
Expand All @@ -84,7 +84,7 @@ where
pattern_metadata,
input,
datetime_names,
fixed_decimal_format,
decimal_formatter,
w,
)?);
}
Expand All @@ -103,7 +103,7 @@ fn try_write_field<W>(
pattern_metadata: PatternMetadata,
input: &ExtractedInput,
datetime_names: &RawDateTimeNamesBorrowed,
fdf: Option<&FixedDecimalFormatter>,
fdf: Option<&DecimalFormatter>,
w: &mut W,
) -> Result<Result<(), DateTimeWriteError>, fmt::Error>
where
Expand Down Expand Up @@ -255,7 +255,7 @@ where
.write_to(w)
})
})?;
Err(DateTimeWriteError::FixedDecimalFormatterNotLoaded)
Err(DateTimeWriteError::DecimalFormatterNotLoaded)
}
}
Err(e) => {
Expand Down Expand Up @@ -534,7 +534,7 @@ fn perform_timezone_fallback(
w: &mut (impl writeable::PartsWrite + ?Sized),
input: &ExtractedInput,
datetime_names: &RawDateTimeNamesBorrowed,
fdf: Option<&FixedDecimalFormatter>,
fdf: Option<&DecimalFormatter>,
field: fields::Field,
units: &[TimeZoneFormatterUnit],
) -> Result<Result<(), DateTimeWriteError>, core::fmt::Error> {
Expand Down Expand Up @@ -576,8 +576,8 @@ fn perform_timezone_fallback(
w.with_part(PART, |w| write_value_missing(w, field))?;
}
match e {
FormatTimeZoneError::FixedDecimalFormatterNotLoaded => {
Err(DateTimeWriteError::FixedDecimalFormatterNotLoaded)
FormatTimeZoneError::DecimalFormatterNotLoaded => {
Err(DateTimeWriteError::DecimalFormatterNotLoaded)
}
FormatTimeZoneError::NamesNotLoaded => {
Err(DateTimeWriteError::NamesNotLoaded(ErrorField(field)))
Expand All @@ -599,7 +599,7 @@ fn perform_timezone_fallback(
#[cfg(feature = "compiled_data")]
mod tests {
use super::*;
use icu_decimal::options::{FixedDecimalFormatterOptions, GroupingStrategy};
use icu_decimal::options::{DecimalFormatterOptions, GroupingStrategy};

#[test]
fn test_format_number() {
Expand All @@ -611,11 +611,11 @@ mod tests {
(FieldLength::Four, ["0002", "0020", "0201", "2017", "20173"]),
];

let mut fixed_decimal_format_options = FixedDecimalFormatterOptions::default();
fixed_decimal_format_options.grouping_strategy = GroupingStrategy::Never;
let fixed_decimal_format = FixedDecimalFormatter::try_new(
let mut decimal_formatter_options = DecimalFormatterOptions::default();
decimal_formatter_options.grouping_strategy = GroupingStrategy::Never;
let decimal_formatter = DecimalFormatter::try_new(
icu_locale_core::locale!("en").into(),
fixed_decimal_format_options,
decimal_formatter_options,
)
.unwrap();

Expand All @@ -624,7 +624,7 @@ mod tests {
let mut s = String::new();
try_write_number_without_part(
&mut writeable::adapters::CoreWriteAsPartsWrite(&mut s),
Some(&fixed_decimal_format),
Some(&decimal_formatter),
SignedFixedDecimal::from(*value),
*length,
)
Expand Down
Loading

0 comments on commit 547c45c

Please sign in to comment.