Skip to content

Commit

Permalink
Refactor Rust code and add 1.5 (unicode-org#360)
Browse files Browse the repository at this point in the history
* Refactor Rust code and add 1.5

* Add 1.5 to run_config.json

* cargo fmt

* Add 1.5 to rust lint job

* Change cargo-platform to a version supporting Rust 1.73
  • Loading branch information
sffc authored Dec 17, 2024
1 parent aeba024 commit 7a5226b
Show file tree
Hide file tree
Showing 16 changed files with 1,510 additions and 155 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/run-rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
strategy:
fail-fast: false
matrix:
icu4x-version: [ '1.3', '1.4' ]
icu4x-version: [ '1.3', '1.4', '1.5' ]
name: Lint ICU4X Rust executor
runs-on: ubuntu-latest
steps:
Expand Down
26 changes: 4 additions & 22 deletions executors/rust/1.3/build.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,8 @@
// Getting the version of ICU4X

fn main() {
let metadata = cargo_metadata::MetadataCommand::new().exec().unwrap();
for package in metadata.packages.iter() {
if package.name == "icu" {
println!(
"cargo:rustc-env=CONFORMANCE_ICU4X_VERSION={}",
package.version
);
}
}

// Get data version information from PackageMetadata
// https://crates.io/crates/rustc_version_runtime
// https://github.com/serde-rs/json
#[path = "../common/print_icu4x_versions_1_3.rs"]
mod print_icu4x_versions_1_3;

println!(
"cargo:rustc-env=CONFORMANCE_ICU_VERSION={}",
icu_datagen::DatagenProvider::LATEST_TESTED_ICUEXPORT_TAG
);
println!(
"cargo:rustc-env=CONFORMANCE_CLDR_VERSION={}",
icu_datagen::DatagenProvider::LATEST_TESTED_CLDR_TAG
);
fn main() {
print_icu4x_versions_1_3::print();
}
2 changes: 1 addition & 1 deletion executors/rust/1.3/src/displaynames.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
use serde_json::{json, Value};

use icu::displaynames::*;
use crate::icu::displaynames::*;
use icu::locid::Locale;

// Function runs comparison using displaynames
Expand Down
4 changes: 2 additions & 2 deletions executors/rust/1.3/src/localenames.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
use serde_json::{json, Value};

use icu::displaynames::{DisplayNamesOptions, LocaleDisplayNamesFormatter};
use crate::icu::displaynames::{DisplayNamesOptions, LocaleDisplayNamesFormatter};

use icu::locid::Locale;

use icu::displaynames::LanguageDisplay;
use crate::icu::displaynames::LanguageDisplay;

// Function runs locale names tests
pub fn run_locale_name_test(json_obj: &Value) -> Result<Value, String> {
Expand Down
144 changes: 19 additions & 125 deletions executors/rust/1.3/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,13 @@
// Run test
// Return results

// TODO:
// 3. Check compatibility of versions with data
// DONE 5. Move parameter extraction into function.
// 6. Fix NumberFormat with options
// 7. Clean up code
// 8. DONE Decide on a repository structure
// 9. DONE Modularize into separate files for each type of test
// 10. Fix test_type and switch statement
// 11. DONE Add language names --> locale names
use std::io;

// References for ICU4X:
// https://unicode-org.github.io/icu4x-docs/doc/icu_collator/index.html
mod icu {
pub use ::icu::compactdecimal;
pub use ::icu::displaynames;
pub use ::icu::relativetime;
}

mod collator;
mod datetimefmt;
Expand All @@ -27,120 +22,19 @@ mod numberfmt;
mod pluralrules;
mod relativedatetime_fmt;

use collator::run_collation_test;
use datetimefmt::run_datetimeformat_test;
use likelysubtags::run_likelysubtags_test;
use listfmt::run_list_fmt_test;
use localenames::run_locale_name_test;
use numberfmt::run_numberformat_test;
use pluralrules::run_plural_rules_test;
use relativedatetime_fmt::run_relativedatetimeformat_test;

use serde_json::{json, Value};
use std::collections::HashMap;
use std::env;
use std::io;
#[path = "../../common/run_all_tests.rs"]
mod run_all_tests;

// Read from stdin, call functions to get json, output the result.
fn main() -> io::Result<()> {
env::set_var("RUST_BACKTRACE", "1");
env_logger::init();
log::info!("Welcome to the ICU4X Conformance Executor");

// Supported tests names mapping to functions.
// Use these strings to respond to test requests.
let _supported_test_map = HashMap::from([
("collation_short".to_string(), run_collation_test), // TODO: ,("number_fmt".to_string(), run_numberformat_test)
]);

// TODO: supported_test_map to call the functions.

// TODO: Handle problem with
// Error: Custom { kind: INvalidData, error: Error{"unexpected end of hex escape"
// As in collation 000144, 0998, 0142
let mut buffer = String::new();

loop {
let buffer_size = io::stdin().read_line(&mut buffer)?;
if buffer_size == 0 {
break;
}
if buffer.starts_with("#EXIT") {
break;
}
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!(
{ "supported_tests": [
"collation_short",
"number_fmt",
"decimal_fmt",
"likelysubtags",
"list_fmt",
"plural_rules"
] }
);
println!("{}", json_result);
}

if buffer.starts_with("#VERSION") {
let json_result = json!(
{
"platform": "ICU4X",
"platformVersion": std::env!("CONFORMANCE_ICU4X_VERSION"),
"icuVersion": std::env!("CONFORMANCE_ICU_VERSION"),
"cldrVersion": std::env!("CONFORMANCE_CLDR_VERSION"),
});
println!("{}", json_result);
} else {
// Expecting test information as JSON data in a single line.

// https://stackoverflow.com/questions/30292752/how-do-i-parse-a-json-file
let json_info: Value = serde_json::from_str(&buffer)?;

let test_type: &str = json_info["test_type"].as_str().unwrap();
let label: &str = json_info["label"].as_str().unwrap();

// TODO!!! : supported_test_map to call the functions.
let json_result = if test_type == "collation_short" {
run_collation_test(&json_info)
} else if (test_type == "decimal_fmt") || (test_type == "number_fmt") {
run_numberformat_test(&json_info)
} else if (test_type == "display_names")
|| (test_type == "language_display_name")
|| (test_type == "lang_names")
{
run_locale_name_test(&json_info)
} else if test_type == "likely_subtags" {
run_likelysubtags_test(&json_info)
} else if test_type == "list_fmt" {
run_list_fmt_test(&json_info)
} else if test_type == "datetime_fmt" {
run_datetimeformat_test(&json_info)
} else if test_type == "plural_rules" {
run_plural_rules_test(&json_info)
} else if test_type == "rdt_fmt" {
run_relativedatetimeformat_test(&json_info)
} else {
Err(test_type.to_string())
};

// Sends the result to stdout.
match json_result {
Ok(value) => println!("{}", value),
Err(error_string) => println!(
"{}",
json!({"error": error_string,
"label": label,
"error_type": "unknown test type",
"error_detail": json_info})
),
}
}
// Empty the input buffer
buffer.clear();
}

Ok(())
let executor_fns = run_all_tests::ExecutorFns {
run_collation_test: collator::run_collation_test,
run_datetimeformat_test: datetimefmt::run_datetimeformat_test,
run_likelysubtags_test: likelysubtags::run_likelysubtags_test,
run_list_fmt_test: listfmt::run_list_fmt_test,
run_locale_name_test: localenames::run_locale_name_test,
run_numberformat_test: numberfmt::run_numberformat_test,
run_plural_rules_test: pluralrules::run_plural_rules_test,
run_relativedatetimeformat_test: relativedatetime_fmt::run_relativedatetimeformat_test,
};
run_all_tests::main(executor_fns)
}
2 changes: 1 addition & 1 deletion executors/rust/1.3/src/numberfmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use fixed_decimal::SignDisplay;
use icu::decimal::options;
use icu::decimal::FixedDecimalFormatter;

use icu::compactdecimal::CompactDecimalFormatter;
use crate::icu::compactdecimal::CompactDecimalFormatter;

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

Expand Down
6 changes: 4 additions & 2 deletions executors/rust/1.3/src/relativedatetime_fmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ use std::str::FromStr;

use icu_provider::DataLocale;

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

use serde::{Deserialize, Serialize};
use serde_json::{json, Value};
Expand Down
9 changes: 8 additions & 1 deletion executors/rust/1.4/build.rs
Original file line number Diff line number Diff line change
@@ -1 +1,8 @@
include!("../1.3/build.rs");
// Getting the version of ICU4X

#[path = "../common/print_icu4x_versions_1_3.rs"]
mod print_icu4x_versions_1_3;

fn main() {
print_icu4x_versions_1_3::print();
}
Loading

0 comments on commit 7a5226b

Please sign in to comment.