Skip to content

Commit

Permalink
split_once
Browse files Browse the repository at this point in the history
  • Loading branch information
burrbull committed Mar 19, 2024
1 parent 4633d09 commit 8175d6e
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 27 deletions.
5 changes: 2 additions & 3 deletions macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
16 changes: 4 additions & 12 deletions src/loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,16 +225,8 @@ pub fn build_bundles(
bundles
}

fn map_to_fluent_args<'map, T: AsRef<str>>(
map: Option<&'map HashMap<T, FluentValue>>,
) -> Option<FluentArgs<'map>> {
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<str>>(map: &'map HashMap<T, FluentValue>) -> FluentArgs<'map> {
map.iter()
.map(|(key, value)| (key.as_ref(), value.clone()))
.collect()
}
2 changes: 1 addition & 1 deletion src/loader/arc_loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
15 changes: 5 additions & 10 deletions src/loader/shared.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,28 +14,23 @@ pub fn lookup_single_language<T: AsRef<str>, R: Borrow<FluentResource>>(
) -> Option<String> {
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:?}")
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/loader/static_loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 8175d6e

Please sign in to comment.