Skip to content

Commit

Permalink
Rust rdtf numeric - supports auto and always options (#285)
Browse files Browse the repository at this point in the history
* Basic lint applied

* Remove unneeded include

* ICU4X: Add Numeric options for RDTF
  • Loading branch information
sven-oly authored Aug 20, 2024
1 parent eb01607 commit 806f49e
Showing 1 changed file with 15 additions and 19 deletions.
34 changes: 15 additions & 19 deletions executors/rust/1.3/src/relativedatetime_fmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use fixed_decimal::FixedDecimal;
use icu::locid::Locale;
use icu_provider::DataLocale;

use icu::relativetime::options::Numeric;
use icu::relativetime::{RelativeTimeError, RelativeTimeFormatter, RelativeTimeFormatterOptions};

use serde::{Deserialize, Serialize};
Expand All @@ -14,6 +15,7 @@ use serde_json::{json, Value};
struct RelativeDateTimeFormatterOptions {
style: Option<String>,
numbering_system: Option<String>,
numeric: Option<String>,
}

fn get_formatter_from_unit_style(
Expand Down Expand Up @@ -158,27 +160,21 @@ pub fn run_relativedatetimeformat_test(json_obj: &Value) -> Result<Value, String
style = option_struct.style.as_ref().unwrap();
}

// Update when ICU4X supports non-Latn numbering systems
// if option_struct.numbering_system.is_some() {
// let numbering_system = option_struct.numbering_system.as_ref().unwrap();

// if numbering_system != "latn" {
// return Ok(json!({
// "error": "Number system not supported",
// "error_msg": numbering_system,
// "label": label,
// "unsupported": "non-Latn numbering system not implemented",
// }));
// }
// }
// Get numeric option
let options = if option_struct.numeric == Some(String::from("auto")) {
RelativeTimeFormatterOptions {
numeric: Numeric::Auto,
}
} else {
// The default
RelativeTimeFormatterOptions {
numeric: Numeric::Always,
}
};

// Use unit & style to select the correct constructor.
let relative_time_formatter = get_formatter_from_unit_style(
&data_locale,
unit.to_string(),
style.to_string(),
RelativeTimeFormatterOptions::default(),
);
let relative_time_formatter =
get_formatter_from_unit_style(&data_locale, unit.to_string(), style.to_string(), options);

let formatter = match relative_time_formatter {
Ok(formatter) => formatter,
Expand Down

0 comments on commit 806f49e

Please sign in to comment.