diff --git a/executors/rust/2.0-beta1/src/main.rs b/executors/rust/2.0-beta1/src/main.rs index 8db11e76..bb2b08c4 100644 --- a/executors/rust/2.0-beta1/src/main.rs +++ b/executors/rust/2.0-beta1/src/main.rs @@ -16,6 +16,10 @@ mod numberfmt; mod pluralrules; mod relativedatetime_fmt; +#[path = "../../common/try_or_return_error.rs"] +#[macro_use] +mod try_or_return_error; + pub fn return_error(json_obj: &serde_json::Value) -> Result { let label = &json_obj["label"].as_str().unwrap(); return Ok(serde_json::json!({ diff --git a/executors/rust/2.0-beta1/src/numberfmt.rs b/executors/rust/2.0-beta1/src/numberfmt.rs index ab215a8e..f2ffc168 100644 --- a/executors/rust/2.0-beta1/src/numberfmt.rs +++ b/executors/rust/2.0-beta1/src/numberfmt.rs @@ -150,12 +150,14 @@ pub fn run_numberformat_test(json_obj: &Value) -> Result { let result_string = if is_compact { // We saw compact! - let cdf = if compact_type == "short" { - CompactDecimalFormatter::try_new_short(langid.into(), Default::default()).unwrap() - } else { - println!("#{:?}", " LONG"); - CompactDecimalFormatter::try_new_long(langid.into(), Default::default()).unwrap() - }; + let cdf = crate::try_or_return_error!(label, langid, { + if compact_type == "short" { + CompactDecimalFormatter::try_new_short((&langid).into(), Default::default()) + } else { + println!("#{:?}", " LONG"); + CompactDecimalFormatter::try_new_long((&langid).into(), Default::default()) + } + }); // input.parse().map_err(|e| e.to_string())?; let input_num = input.parse::().map_err(|e| e.to_string())?; @@ -168,18 +170,9 @@ pub fn run_numberformat_test(json_obj: &Value) -> Result { } else { // FixedDecimal // Can this fail with invalid options? - let fdf = match FixedDecimalFormatter::try_new((&langid).into(), options.clone()) { - Ok(fdf) => fdf, - Err(e) => { - let locale_str = langid.to_string(); - return Ok(json!({ - "label": label, - "locale_label": locale_str, - "error": e.to_string(), - "error_type": "panic", - })); - } - }; + let fdf = crate::try_or_return_error!(label, langid, { + FixedDecimalFormatter::try_new((&langid).into(), options.clone()) + }); // Apply relevant options for digits. if let Some(x) = option_struct.maximum_fraction_digits { diff --git a/executors/rust/common/try_or_return_error.rs b/executors/rust/common/try_or_return_error.rs new file mode 100644 index 00000000..cde47f7f --- /dev/null +++ b/executors/rust/common/try_or_return_error.rs @@ -0,0 +1,16 @@ +#[macro_export] +macro_rules! try_or_return_error { + ($label:expr, $locale:expr, $expr:expr) => {{ + match ($expr) { + Ok(x) => x, + Err(e) => { + return Ok(json!({ + "label": ($label), + "locale_label": ($locale).to_string(), + "error": e.to_string(), + "error_type": "panic", + })); + } + } + }}; +}