From 8175d6ed8fb475bcf4895efb08693a8e38d15f80 Mon Sep 17 00:00:00 2001 From: Andrey Zgarbul Date: Tue, 19 Mar 2024 07:13:35 +0300 Subject: [PATCH] split_once --- macros/src/lib.rs | 5 ++--- src/loader.rs | 16 ++++------------ src/loader/arc_loader.rs | 2 +- src/loader/shared.rs | 15 +++++---------- src/loader/static_loader.rs | 2 +- 5 files changed, 13 insertions(+), 27 deletions(-) diff --git a/macros/src/lib.rs b/macros/src/lib.rs index 7844a18..6aa598d 100644 --- a/macros/src/lib.rs +++ b/macros/src/lib.rs @@ -213,10 +213,9 @@ pub fn static_loader(input: proc_macro::TokenStream) -> proc_macro::TokenStream let mut insert_resources: Vec<_> = build_resources(locales_directory).into_iter().collect(); - if insert_resources + if !insert_resources .iter() - .find(|(lang, _)| *lang == fallback_language.value()) - .is_none() + .any(|(lang, _)| *lang == fallback_language.value()) { return syn::Error::new( fallback_language.span(), diff --git a/src/loader.rs b/src/loader.rs index f9ec1a3..b9c20f5 100644 --- a/src/loader.rs +++ b/src/loader.rs @@ -225,16 +225,8 @@ pub fn build_bundles( bundles } -fn map_to_fluent_args<'map, T: AsRef>( - map: Option<&'map HashMap>, -) -> Option> { - let mut new = FluentArgs::new(); - - if let Some(map) = map { - for (key, value) in map { - new.set(key.as_ref(), value.clone()); - } - } - - Some(new) +fn map_to_fluent_args<'map, T: AsRef>(map: &'map HashMap) -> FluentArgs<'map> { + map.iter() + .map(|(key, value)| (key.as_ref(), value.clone())) + .collect() } diff --git a/src/loader/arc_loader.rs b/src/loader/arc_loader.rs index 5811b6b..b9811e1 100644 --- a/src/loader/arc_loader.rs +++ b/src/loader/arc_loader.rs @@ -123,7 +123,7 @@ impl super::Loader for ArcLoader { return val; } } - format!("Unknown localization {}", text_id) + format!("Unknown localization {text_id}") } // Traverse the fallback chain, diff --git a/src/loader/shared.rs b/src/loader/shared.rs index 44671a8..6e69b4d 100644 --- a/src/loader/shared.rs +++ b/src/loader/shared.rs @@ -14,28 +14,23 @@ pub fn lookup_single_language, R: Borrow>( ) -> Option { let bundle = bundles.get(lang)?; let mut errors = Vec::new(); - let pattern = if text_id.contains('.') { - // TODO: #![feature(str_split_once)] - let ids: Vec<_> = text_id.splitn(2, '.').collect(); + let pattern = if let Some((msg, attr)) = text_id.split_once('.') { bundle - .get_message(ids[0])? + .get_message(msg)? .attributes() - .find(|attribute| attribute.id() == ids[1])? + .find(|attribute| attribute.id() == attr)? .value() } else { bundle.get_message(text_id)?.value()? }; - let args = super::map_to_fluent_args(args); + let args = args.map(super::map_to_fluent_args); let value = bundle.format_pattern(pattern, args.as_ref(), &mut errors); if errors.is_empty() { Some(value.into()) } else { - panic!( - "Failed to format a message for locale {} and id {}.\nErrors\n{:?}", - lang, text_id, errors - ) + panic!("Failed to format a message for locale {lang} and id {text_id}.\nErrors\n{errors:?}") } } diff --git a/src/loader/static_loader.rs b/src/loader/static_loader.rs index 9eb4162..fec3547 100644 --- a/src/loader/static_loader.rs +++ b/src/loader/static_loader.rs @@ -74,7 +74,7 @@ impl super::Loader for StaticLoader { return val; } } - format!("Unknown localization {}", text_id) + format!("Unknown localization {text_id}") } // Traverse the fallback chain,