Skip to content

Commit

Permalink
Simplify Rust executor (unicode-org#119)
Browse files Browse the repository at this point in the history
  • Loading branch information
robertbastian authored Nov 13, 2023
1 parent 44540ca commit ca621dd
Show file tree
Hide file tree
Showing 9 changed files with 325 additions and 178 deletions.
359 changes: 276 additions & 83 deletions executors/rust/Cargo.lock

Large diffs are not rendered by default.

32 changes: 10 additions & 22 deletions executors/rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,19 @@ name = "executor"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
log = "0.4"
env_logger = "0.9.1"
env_logger = "0.9.1"

substring = "1.0"
json = "0.12.4"
serde = {version = "1.0.171", features = ["derive", "alloc"]}
serde = "1.0.171"
serde_json = "1.0.100"
rustc_version_runtime = "0.1.*"

fixed_decimal = {version="0.5.3"}
writeable = {version="0.5.2"}
icu_compactdecimal = {version="0.2.0"}
icu_displaynames = {version="0.11.1"}

icu = {version="1.3.0", features = ["serde"] }
icu_testdata = {version="1.3.0", features = ["icu_compactdecimal", "icu_displaynames"]}
icu_provider = {version="1.3.0", features=["macros"]}
icu_locid = {version="1.3.0"}
icu_locid_transform = {version = "1.3.0", optional=true}
icu_plurals = { version = "1.3.0", optional=true}
icu_decimal = { version = "1.3.0", optional=true}
databake = { version = "0.1.3", features = ["derive"], optional = true}
zerovec = { version = "0.10.0", features = ["yoke"] }

[features]

icu = "1.3.0"
icu_compactdecimal = "0.2.0"
icu_displaynames = "0.11.1"
icu_datagen = { version = "1.3.0", default-features = false }

fixed_decimal ="0.5.3"
writeable ="0.5.2"
4 changes: 1 addition & 3 deletions executors/rust/src/collator.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
/*
* Executor provides tests for Collator.
*/
//! Executor provides tests for Collator.
use serde_json::{json, Value};

Expand Down
13 changes: 4 additions & 9 deletions executors/rust/src/decimalfmt.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
/*
* Executor provides tests for DecimalFormat and NumberFormat.
*/
//! Executor provides tests for DecimalFormat and NumberFormat.
use serde_json::{json, Value};
use writeable::Writeable;

use icu::decimal::options;
use icu::decimal::FixedDecimalFormatter;

use fixed_decimal::FixedDecimal;

use icu::locid::locale;
use icu_provider::DataLocale;

// Runs decimal and number formatting given patterns or skeletons.
pub fn run_numberformat_test(json_obj: &Value) {
Expand All @@ -25,7 +21,6 @@ pub fn run_numberformat_test(json_obj: &Value) {
let locale_name = &json_obj["locale"].as_str().unwrap();
langid = icu::locid:Locale::from_str(&locale_name);
}
let data_locale = DataLocale::from(langid);

let input = &json_obj["input"].as_str().unwrap();

Expand All @@ -35,17 +30,17 @@ pub fn run_numberformat_test(json_obj: &Value) {
options.grouping_strategy = options::GroupingStrategy::Min2;

let fdf = FixedDecimalFormatter::try_new_with_buffer_provider(
&provider, &data_locale, options)
&provider, &langid.into(), options)
.expect("Data should load successfully");

// Check if the conversion from the string input is OK.
let input_num: FixedDecimal = input.parse().expect("valid input format");
let result_string = fdf.format(&input_num);
let result_string = fdf.format_to_string(&input_num);

// Result to stdout.
let json_result = json!({
"label": label,
"result": result_string.write_to_string()});
"result": result_string});
print!("{}", json_result);
}

Expand Down
10 changes: 3 additions & 7 deletions executors/rust/src/displaynames.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
/*
* Executor provides tests for DisplayNames.
*/
//! Executor provides tests for DisplayNames.
use serde_json::{json, Value};
use std::io::{self, Write};

use core::cmp::Ordering;
use icu::displaynames::*;

use icu::displaynames::*;
use icu::locid::{locale, Locale};
use icu_provider::DataLocale;

// Function runs comparison using displaynames
pub fn run_coll_test(json_obj: &Value) {
Expand All @@ -24,14 +21,13 @@ pub fn run_coll_test(json_obj: &Value) {
} else {
locale!("und")
};
let data_locale = DataLocale::from(langid);

let data_provider = icu_testdata::unstable();

let mut options = DisplayNamesOptions::new();

let displaynames: DisplayNames =
DisplayNames::try_new_unstable(&data_provider, data_locale, options).unwrap();
DisplayNames::try_new_unstable(&data_provider, langid.into(), options).unwrap();

let result = displaynames.of(input);

Expand Down
17 changes: 4 additions & 13 deletions executors/rust/src/langnames.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
/*
* Executor provides tests for LanguageNames, a special case of DisplayNames.
*/
//! Executor provides tests for LanguageNames, a special case of DisplayNames.
use serde_json::{json, Value};

Expand All @@ -9,10 +7,6 @@ use icu_displaynames::{DisplayNamesOptions, LanguageDisplayNames};
use icu::locid::subtags::Language;
use icu::locid::Locale;

use icu_provider::DataLocale;

use std::str::FromStr;

// Function runs language names tests
pub fn run_language_name_test(json_obj: &Value) -> Result<Value, String> {
let label = &json_obj["label"].as_str().unwrap();
Expand All @@ -22,7 +16,7 @@ pub fn run_language_name_test(json_obj: &Value) -> Result<Value, String> {
.as_str()
.unwrap()
.replace('_', "-");
let input_lang_result = Language::from_str(&language_label);
let input_lang_result = language_label.parse::<Language>();
let input_lang = match input_lang_result {
Ok(l) => l,
Err(_e) => {
Expand Down Expand Up @@ -55,7 +49,7 @@ pub fn run_language_name_test(json_obj: &Value) -> Result<Value, String> {
}
};

let langid_result = Locale::from_str(locale_name);
let langid_result = locale_name.parse::<Locale>();

let langid = match langid_result {
Ok(lid) => lid,
Expand All @@ -73,10 +67,7 @@ pub fn run_language_name_test(json_obj: &Value) -> Result<Value, String> {
}
};

// The locale data may not yet be supported.
let data_locale = DataLocale::from(&langid);

let display_name_formatter = LanguageDisplayNames::try_new(&data_locale, options);
let display_name_formatter = LanguageDisplayNames::try_new(&langid.into(), options);

let json_result = match display_name_formatter {
Ok(formatter) => {
Expand Down
10 changes: 3 additions & 7 deletions executors/rust/src/likelysubtags.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
/*
* Provides tests for likely subtags to mimimize and maximize.
*/
//! Provides tests for likely subtags to mimimize and maximize.
use serde_json::{json, Value};

use icu::locid::Locale;
use icu::locid_transform::LocaleExpander;

use std::str::FromStr;

// https://docs.rs/icu_locid_transform/latest/icu_locid_transform/
// https://docs.rs/icu/latest/icu/locid_transform/

// Function runs language names tests
pub fn run_likelysubtags_test(json_obj: &Value) -> Result<Value, String> {
Expand All @@ -21,7 +17,7 @@ pub fn run_likelysubtags_test(json_obj: &Value) -> Result<Value, String> {

let locale_str: &str = json_obj["locale"].as_str().unwrap();

let mut locale = Locale::from_str(locale_str).unwrap();
let mut locale = locale_str.parse::<Locale>().unwrap();

if test_option == &"minimizeFavorRegion" {
// This option is not yet supported.
Expand Down
14 changes: 5 additions & 9 deletions executors/rust/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ use std::collections::HashMap;
use std::env;
use std::io::{self};

use substring::Substring;

// Test modules for each type
use collator::run_collation_test;
use langnames::run_language_name_test;
Expand Down Expand Up @@ -60,10 +58,10 @@ fn main() -> io::Result<()> {
if buffer_size == 0 {
break;
}
if buffer.substring(0, 5) == "#EXIT" {
if buffer.starts_with("#EXIT") {
break;
}
if buffer.substring(0, 6) == "#TESTS" {
if buffer.starts_with("#TESTS") {
// Returns JSON list of supported tests.
// TODO: let mut test_vec : Vec<&str> = supported_test_map.into_keys().collect();
let json_result = json!(
Expand All @@ -76,15 +74,13 @@ fn main() -> io::Result<()> {
println!("{}", json_result);
}

if buffer.substring(0, 8) == "#VERSION" {
if buffer.starts_with("#VERSION") {
// Get data version information from PackageMetadata
// https://crates.io/crates/rustc_version_runtime
// https://github.com/serde-rs/json

#[allow(deprecated)] // this function only exists in icu_testdata
let icu_version = &icu_testdata::versions::icu_tag();
#[allow(deprecated)]
let cldr_version = &icu_testdata::versions::cldr_tag();
let icu_version = icu_datagen::DatagenProvider::LATEST_TESTED_ICUEXPORT_TAG;
let cldr_version = icu_datagen::DatagenProvider::LATEST_TESTED_CLDR_TAG;

let json_result = json!(
{
Expand Down
44 changes: 19 additions & 25 deletions executors/rust/src/numberfmt.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
/*
* Executor provides tests for NumberFormat and DecimalFormat.
*/
//! Executor provides tests for NumberFormat and DecimalFormat.
use fixed_decimal::FixedDecimal;
use fixed_decimal::SignDisplay;
Expand All @@ -11,15 +9,11 @@ use icu::decimal::FixedDecimalFormatter;

use icu_compactdecimal::CompactDecimalFormatter;

use icu::locid::{locale, Locale};
use icu_provider::DataLocale;
use icu::locid::{extensions::unicode::key, locale, Locale};

use serde::{Deserialize, Serialize};
use serde_json::{json, Value};

use std::panic;
use std::str::FromStr;

use writeable::Writeable;

// Support options - update when ICU4X adds support
Expand Down Expand Up @@ -62,13 +56,12 @@ pub fn run_numberformat_test(json_obj: &Value) -> Result<Value, String> {
let label = &json_obj["label"].as_str().unwrap();

// Default locale if not specified.
let langid = if json_obj.get("locale").is_some() {
let mut langid = if json_obj.get("locale").is_some() {
let locale_name = &json_obj["locale"].as_str().unwrap();
Locale::from_str(locale_name).unwrap()
locale_name.parse::<Locale>().unwrap()
} else {
locale!("und")
};
let mut data_locale = DataLocale::from(langid);

let input = &json_obj["input"].as_str().unwrap();

Expand All @@ -90,19 +83,19 @@ pub fn run_numberformat_test(json_obj: &Value) -> Result<Value, String> {
is_compact = true;
}
if option_struct.compact_display.is_some() {
compact_type = &option_struct.compact_display.as_ref().unwrap();
compact_type = option_struct.compact_display.as_ref().unwrap();
}
if option_struct.notation == Some(String::from("scientific")) {
is_scientific = true;
}
if option_struct.style.is_some() {
style = &option_struct.style.as_ref().unwrap();
style = option_struct.style.as_ref().unwrap();
}
if option_struct.unit.is_some() {
unit = &option_struct.unit.as_ref().unwrap();
unit = option_struct.unit.as_ref().unwrap();
}
if option_struct.rounding_mode.is_some() {
_rounding_mode = &option_struct.rounding_mode.as_ref().unwrap();
_rounding_mode = option_struct.rounding_mode.as_ref().unwrap();
}
let mut options: options::FixedDecimalFormatterOptions = Default::default();
// TODO: Use options to call operations including pad and trunc with rounding.
Expand Down Expand Up @@ -146,7 +139,11 @@ pub fn run_numberformat_test(json_obj: &Value) -> Result<Value, String> {
// --------------------------------------------------------------------------------

if let Some(numsys) = option_struct.numbering_system.as_ref() {
data_locale.set_unicode_ext("nu".parse().unwrap(), numsys.parse().unwrap());
langid
.extensions
.unicode
.keywords
.set(key!("nu"), numsys.parse().unwrap());
}

// Returns error if parsing the number string fails.
Expand All @@ -155,27 +152,24 @@ pub fn run_numberformat_test(json_obj: &Value) -> Result<Value, String> {
let result_string = if is_compact {
// We saw compact!
let cdf = if compact_type == "short" {
CompactDecimalFormatter::try_new_short(&data_locale, Default::default()).unwrap()
CompactDecimalFormatter::try_new_short(&langid.into(), Default::default()).unwrap()
} else {
println!("#{:?}", " LONG");
CompactDecimalFormatter::try_new_long(&data_locale, Default::default()).unwrap()
CompactDecimalFormatter::try_new_long(&langid.into(), Default::default()).unwrap()
};
// input.parse().map_err(|e| e.to_string())?;

let input_num = FixedDecimal::from_str(input).map_err(|e| e.to_string())?;
let input_num = input.parse::<FixedDecimal>().map_err(|e| e.to_string())?;
let formatted_cdf = cdf.format_fixed_decimal(input_num);
formatted_cdf
//.map_err(|e| e.to_string())?
.write_to_string()
.into_owned()
formatted_cdf.write_to_string().into_owned()
// }
// else if is_scientific {
// let mut sci_decimal = input.parse::<ScientificDecimal>().map_err(|e| e.to_string());
// // TEMPORARY
} else {
// FixedDecimal
// Can this fail with invalid options?
let fdf = FixedDecimalFormatter::try_new(&data_locale, options.clone())
let fdf = FixedDecimalFormatter::try_new(&langid.into(), options.clone())
.expect("Data should load successfully");

// Apply relevant options for digits.
Expand Down Expand Up @@ -218,7 +212,7 @@ pub fn run_numberformat_test(json_obj: &Value) -> Result<Value, String> {
}

// Apply the options and get formatted string.
fdf.format(&input_num).write_to_string().into_owned()
fdf.format_to_string(&input_num)
};

// Result to stdout.
Expand Down

0 comments on commit ca621dd

Please sign in to comment.