Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update locale_core::preferences to stakeholder alignment #5729

Merged
merged 7 commits into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions components/experimental/src/duration/formatter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ impl From<BaseStyle> for icu_list::ListFormatterOptions {
BaseStyle::Short | BaseStyle::Digital => ListLength::Short,
BaseStyle::Narrow => ListLength::Narrow,
};
Self::new().with_length(length)
Self::default().with_length(length)
}
}

Expand Down Expand Up @@ -195,11 +195,12 @@ impl DurationFormatter {
})?
.payload;

let temp_loc = locale.clone().into_locale();
Ok(Self {
digital,
options,
unit: DurationUnitFormatter::try_new(locale, options)?,
list: ListFormatter::try_new_unit_with_length(locale.into(), options.base.into())?,
list: ListFormatter::try_new_unit(temp_loc.into(), options.base.into())?,
fdf: FixedDecimalFormatter::try_new(locale, Default::default())?,
})
}
Expand All @@ -224,13 +225,14 @@ impl DurationFormatter {
})?
.payload;

let temp_loc = locale.clone().into_locale();
Ok(Self {
digital,
options,
unit: DurationUnitFormatter::try_new_unstable(provider, locale, options)?,
list: ListFormatter::try_new_unit_with_length_unstable(
list: ListFormatter::try_new_unit_unstable(
provider,
locale.into(),
temp_loc.into(),
options.base.into(),
)?,
fdf: FixedDecimalFormatter::try_new_unstable(provider, locale, Default::default())?,
Expand Down
12 changes: 6 additions & 6 deletions components/list/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions components/list/examples/and_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ use icu::list::{ListFormatter, ListFormatterOptions, ListLength};
use icu::locale::locale;

fn main() {
let list_formatter = ListFormatter::try_new_and_with_length(
let list_formatter = ListFormatter::try_new_and(
locale!("es").into(),
ListFormatterOptions::new().with_length(ListLength::Wide),
ListFormatterOptions::default().with_length(ListLength::Wide),
)
.unwrap();

Expand Down
12 changes: 6 additions & 6 deletions components/list/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
//! # use icu::locale::locale;
//! # use writeable::*;
//! #
//! let list_formatter = ListFormatter::try_new_and_with_length(
//! let list_formatter = ListFormatter::try_new_and(
//! locale!("es").into(),
//! ListFormatterOptions::new()
//! ListFormatterOptions::default()
//! .with_length(ListLength::Wide)
//! )
//! .expect("locale should be present");
Expand All @@ -42,9 +42,9 @@
//! # use icu::locale::locale;
//! # use writeable::*;
//! #
//! let list_formatter = ListFormatter::try_new_or_with_length(
//! let list_formatter = ListFormatter::try_new_or(
//! locale!("th").into(),
//! ListFormatterOptions::new()
//! ListFormatterOptions::default()
//! .with_length(ListLength::Short)
//! )
//! .expect("locale should be present");
Expand All @@ -60,9 +60,9 @@
//! # use icu::locale::locale;
//! # use writeable::*;
//! #
//! let list_formatter = ListFormatter::try_new_unit_with_length(
//! let list_formatter = ListFormatter::try_new_unit(
//! locale!("en").into(),
//! ListFormatterOptions::new()
//! ListFormatterOptions::default()
//! .with_length(ListLength::Wide)
//! )
//! .expect("locale should be present");
Expand Down
54 changes: 25 additions & 29 deletions components/list/src/list_formatter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,38 +74,38 @@ macro_rules! constructor {
}

fn get_data_locale_from_prefs(prefs: ListFormatterPreferences) -> DataLocale {
// XXX: This should utilize region source priority.
// TODO(#5764): This should utilize region source priority.
DataLocale::from_subtags(
prefs.language,
prefs.script,
prefs.region,
prefs.variant,
prefs.subdivision,
prefs.locale_prefs.language,
prefs.locale_prefs.script,
prefs.locale_prefs.region,
prefs.locale_prefs.variant,
prefs.locale_prefs.subdivision,
)
}

impl ListFormatter {
constructor!(
try_new_and_with_length,
try_new_and_with_length_with_any_provider,
try_new_and_with_length_with_buffer_provider,
try_new_and_with_length_unstable,
try_new_and,
try_new_and_with_any_provider,
try_new_and_with_buffer_provider,
try_new_and_unstable,
AndListV2Marker,
"and"
);
constructor!(
try_new_or_with_length,
try_new_or_with_length_with_any_provider,
try_new_or_with_length_with_buffer_provider,
try_new_or_with_length_unstable,
try_new_or,
try_new_or_with_any_provider,
try_new_or_with_buffer_provider,
try_new_or_unstable,
OrListV2Marker,
"or"
);
constructor!(
try_new_unit_with_length,
try_new_unit_with_length_with_any_provider,
try_new_unit_with_length_with_buffer_provider,
try_new_unit_with_length_unstable,
try_new_unit,
try_new_unit_with_any_provider,
try_new_unit_with_buffer_provider,
try_new_unit_unstable,
UnitListV2Marker,
"unit"
);
Expand All @@ -122,9 +122,9 @@ impl ListFormatter {
/// use icu::list::*;
/// # use icu::locale::locale;
/// # use writeable::*;
/// let formatteur = ListFormatter::try_new_and_with_length(
/// let formatteur = ListFormatter::try_new_and(
/// locale!("fr").into(),
/// ListFormatterOptions::new()
/// ListFormatterOptions::default()
/// .with_length(ListLength::Wide)
/// )
/// .unwrap();
Expand Down Expand Up @@ -388,14 +388,14 @@ mod tests {

#[test]
fn test_basic() {
test!("fr", try_new_or_with_length, (["A", "B"], "A ou B"),);
test!("fr", try_new_or, (["A", "B"], "A ou B"),);
}

#[test]
fn test_spanish() {
test!(
"es",
try_new_and_with_length,
try_new_and,
(["x", "Mallorca"], "x y Mallorca"),
(["x", "Ibiza"], "x e Ibiza"),
(["x", "Hidalgo"], "x e Hidalgo"),
Expand All @@ -404,7 +404,7 @@ mod tests {

test!(
"es",
try_new_or_with_length,
try_new_or,
(["x", "Ibiza"], "x o Ibiza"),
(["x", "Okinawa"], "x u Okinawa"),
(["x", "8 más"], "x u 8 más"),
Expand All @@ -421,18 +421,14 @@ mod tests {
(["x", "11.000,92"], "x u 11.000,92"),
);

test!(
"es-AR",
try_new_and_with_length,
(["x", "Ibiza"], "x e Ibiza"),
);
test!("es-AR", try_new_and, (["x", "Ibiza"], "x e Ibiza"),);
}

#[test]
fn test_hebrew() {
test!(
"he",
try_new_and_with_length,
try_new_and,
(["x", "יפו"], "x ויפו"),
(["x", "Ibiza"], "x ו‑Ibiza"),
);
Expand Down
16 changes: 11 additions & 5 deletions components/list/src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,30 @@
/// ```
/// use icu::list::{ListFormatterOptions, ListLength};
///
/// let options = ListFormatterOptions::new()
/// let options = ListFormatterOptions::default()
/// .with_length(ListLength::Wide);
/// ```
#[derive(Default, Debug, Clone)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub struct ListFormatterOptions {
/// The length variant should reflect available space for the list.
pub length: Option<ListLength>,
}

impl Default for ListFormatterOptions {
fn default() -> Self {
Self::default()
}
}

impl ListFormatterOptions {
/// Constructs a new [`ListFormatterOptions`] struct.
pub fn new() -> Self {
Self::default()
pub const fn default() -> Self {
Self { length: None }
}

/// Auguments the struct with the set [`ListLength`].
pub fn with_length(mut self, length: ListLength) -> Self {
pub const fn with_length(mut self, length: ListLength) -> Self {
self.length = Some(length);
self
}
Expand Down
13 changes: 2 additions & 11 deletions components/locale_core/src/extensions/unicode/subdivision.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

use core::str::FromStr;

use tinystr::TinyAsciiStr;

use crate::parser::ParseError;
use crate::subtags::{Region, Subtag};

Expand Down Expand Up @@ -136,15 +134,8 @@ impl SubdivisionId {

/// Convert to [`Subtag`]
pub fn into_subtag(self) -> Subtag {
use writeable::Writeable;

// XXX: This can be optimized to concatenate two TinyAsciiStr.
let mut result = alloc::string::String::with_capacity(8);
let _ = self.write_to(&mut result);
#[allow(clippy::expect_used)]
let tinystr = TinyAsciiStr::try_from_str(&result)
.expect("Constructing 8 chars TinyAsciiStr from two 4 char ones");
Subtag::from_tinystr_unvalidated(tinystr)
let result = self.region.to_tinystr().concat(self.suffix.to_tinystr());
Subtag::from_tinystr_unvalidated(result)
}
}

Expand Down
2 changes: 1 addition & 1 deletion components/locale_core/src/parser/langid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ pub const fn parse_locale_with_single_variant_single_keyword_unicode_extension_f
} else {
break;
}
iter = iter.next_const().0
iter = iter.next_const().0;
}
if let Some(k) = key {
keyword = Some((k, current_type));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,22 @@ enum_keyword!(
///
/// The valid values are listed in [LDML](https://unicode.org/reports/tr35/#UnicodeCollationIdentifier).
CollationType {
/// Pinyin ordering for Latin, big5 charset ordering for CJK characters (used in Chinese)
("big5han" => Big5han),
/// A previous version of the ordering, for compatibility
("compat" => Compat),
/// Dictionary style ordering (such as in Sinhala)
("dict" => Dict),
/// Binary code point order (used in Hindi)
("direct" => Direct),
/// The default Unicode collation element table order
("ducet" => Ducet),
/// Recommended ordering for emoji characters
("emoji" => Emoji),
/// European ordering rules
("eor" => Eor),
/// Pinyin ordering for Latin, gb2312han charset ordering for CJK characters (used in Chinese)
("gb2312" => Gb2312),
/// Phonebook style ordering (such as in German)
("phonebk" => Phonebk),
/// Phonetic ordering (sorting based on pronunciation)
("phonetic" => Phonetic),
/// Pinyin ordering for Latin and for CJK characters (used in Chinese)
("pinyin" => Pinyin),
/// Reformed ordering (such as in Swedish)
("reformed" => Reformed),
/// Special collation type for string search
("search" => Search),
/// Special collation type for Korean initial consonant search
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@ struct_keyword!(
.map(Self)
},
|input: DictionaryBreakScriptExclusions| {
crate::extensions::unicode::Value::from_iter(input.0.into_iter().map(Into::into))
input.0.into_iter().map(Into::into).collect()
}
);
Loading