Skip to content

Commit 4a38d24

Browse files
committed
Refactor FluentArgs
1 parent 239f5a2 commit 4a38d24

File tree

9 files changed

+38
-51
lines changed

9 files changed

+38
-51
lines changed

fluent-bundle/benches/resolver.rs

+11-14
Original file line numberDiff line numberDiff line change
@@ -38,23 +38,20 @@ fn get_ids(res: &FluentResource) -> Vec<String> {
3838
.collect()
3939
}
4040

41-
fn get_args(name: &str) -> Option<HashMap<String, FluentValue>> {
41+
fn get_args(name: &str) -> Option<HashMap<&str, FluentValue>> {
4242
match name {
4343
"preferences" => {
4444
let mut prefs_args = HashMap::new();
45-
prefs_args.insert("name".to_string(), FluentValue::from("John"));
46-
prefs_args.insert("tabCount".to_string(), FluentValue::from(5));
47-
prefs_args.insert("count".to_string(), FluentValue::from(3));
48-
prefs_args.insert("version".to_string(), FluentValue::from("65.0"));
49-
prefs_args.insert("path".to_string(), FluentValue::from("/tmp"));
50-
prefs_args.insert("num".to_string(), FluentValue::from(4));
51-
prefs_args.insert("email".to_string(), FluentValue::from("[email protected]"));
52-
prefs_args.insert("value".to_string(), FluentValue::from(4.5));
53-
prefs_args.insert("unit".to_string(), FluentValue::from("mb"));
54-
prefs_args.insert(
55-
"service-name".to_string(),
56-
FluentValue::from("Mozilla Disk"),
57-
);
45+
prefs_args.insert("name", FluentValue::from("John"));
46+
prefs_args.insert("tabCount", FluentValue::from(5));
47+
prefs_args.insert("count", FluentValue::from(3));
48+
prefs_args.insert("version", FluentValue::from("65.0"));
49+
prefs_args.insert("path", FluentValue::from("/tmp"));
50+
prefs_args.insert("num", FluentValue::from(4));
51+
prefs_args.insert("email", FluentValue::from("[email protected]"));
52+
prefs_args.insert("value", FluentValue::from(4.5));
53+
prefs_args.insert("unit", FluentValue::from("mb"));
54+
prefs_args.insert("service-name", FluentValue::from("Mozilla Disk"));
5855
Some(prefs_args)
5956
}
6057
_ => None,

fluent-bundle/examples/external_arguments.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ unread-emails =
2222
.expect("Failed to add FTL resources to the bundle.");
2323

2424
let mut args = HashMap::new();
25-
args.insert("name".to_string(), FluentValue::from("John"));
25+
args.insert("name", FluentValue::from("John"));
2626

2727
let msg = bundle
2828
.get_message("hello-world")
@@ -39,7 +39,7 @@ unread-emails =
3939
println!("{}", value);
4040

4141
let mut args = HashMap::new();
42-
args.insert("emailCount".to_string(), FluentValue::into_number("1.0"));
42+
args.insert("emailCount", FluentValue::into_number("1.0"));
4343

4444
let msg = bundle
4545
.get_message("unread-emails")

fluent-bundle/examples/selector.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ hello-world2 = Hello { $name ->
3030
println!("{}", value);
3131

3232
let mut args = HashMap::new();
33-
args.insert("name".to_string(), FluentValue::from("moon"));
33+
args.insert("name", FluentValue::from("moon"));
3434

3535
let msg = bundle
3636
.get_message("hello-world2")

fluent-bundle/examples/simple-app.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,8 @@ fn main() {
131131
// 7.2. Construct a map of arguments
132132
// to format the message.
133133
let mut args = HashMap::new();
134-
args.insert("input".to_string(), FluentValue::from(i));
135-
args.insert("value".to_string(), FluentValue::from(collatz(i)));
134+
args.insert("input", FluentValue::from(i));
135+
args.insert("value", FluentValue::from(collatz(i)));
136136
// 7.3. Format the message.
137137
let mut errors = vec![];
138138
let msg = bundle
@@ -144,8 +144,8 @@ fn main() {
144144
}
145145
Err(err) => {
146146
let mut args = HashMap::new();
147-
args.insert("input".to_string(), FluentValue::from(input.as_str()));
148-
args.insert("reason".to_string(), FluentValue::from(err.to_string()));
147+
args.insert("input", FluentValue::from(input.as_str()));
148+
args.insert("reason", FluentValue::from(err.to_string()));
149149
let mut errors = vec![];
150150
let msg = bundle
151151
.get_message("input-parse-error")

fluent-bundle/src/bundle.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ pub struct FluentMessage<'m> {
2828
pub attributes: HashMap<&'m str, &'m ast::Pattern<'m>>,
2929
}
3030

31+
pub type FluentArgs<'args> = HashMap<&'args str, FluentValue<'args>>;
32+
3133
/// A collection of localization messages for a single locale, which are meant
3234
/// to be used together in a single view, widget or any other UI abstraction.
3335
///
@@ -48,7 +50,7 @@ pub struct FluentMessage<'m> {
4850
/// .expect("Failed to add FTL resources to the bundle.");
4951
///
5052
/// let mut args = HashMap::new();
51-
/// args.insert("name".to_string(), FluentValue::from("Rustacean"));
53+
/// args.insert("name", FluentValue::from("Rustacean"));
5254
///
5355
/// let msg = bundle.get_message("intro").expect("Message doesn't exist.");
5456
/// let mut errors = vec![];
@@ -282,7 +284,7 @@ impl<R> FluentBundle<R> {
282284
pub fn format_pattern<'bundle>(
283285
&'bundle self,
284286
pattern: &'bundle ast::Pattern,
285-
args: Option<&'bundle HashMap<String, FluentValue>>,
287+
args: Option<&'bundle FluentArgs>,
286288
errors: &mut Vec<FluentError>,
287289
) -> Cow<'bundle, str>
288290
where
@@ -335,9 +337,7 @@ impl<R> FluentBundle<R> {
335337
/// [FTL syntax guide]: https://projectfluent.org/fluent/guide/functions.html
336338
pub fn add_function<F: 'static>(&mut self, id: &str, func: F) -> Result<(), FluentError>
337339
where
338-
F: for<'a> Fn(&[FluentValue<'a>], &HashMap<String, FluentValue<'a>>) -> FluentValue<'a>
339-
+ Sync
340-
+ Send,
340+
F: for<'a> Fn(&[FluentValue<'a>], &FluentArgs) -> FluentValue<'a> + Sync + Send,
341341
{
342342
match self.entries.entry(id.to_owned()) {
343343
HashEntry::Vacant(entry) => {

fluent-bundle/src/entry.rs

+3-7
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,15 @@
11
//! `Entry` is used to store Messages, Terms and Functions in `FluentBundle` instances.
22
33
use std::borrow::Borrow;
4-
use std::collections::HashMap;
54

65
use fluent_syntax::ast;
76

8-
use crate::bundle::FluentBundle;
7+
use crate::bundle::{FluentArgs, FluentBundle};
98
use crate::resource::FluentResource;
109
use crate::types::FluentValue;
1110

12-
pub type FluentFunction = Box<
13-
dyn for<'a> Fn(&[FluentValue<'a>], &HashMap<String, FluentValue<'a>>) -> FluentValue<'a>
14-
+ Send
15-
+ Sync,
16-
>;
11+
pub type FluentFunction =
12+
Box<dyn for<'a> Fn(&[FluentValue<'a>], &FluentArgs) -> FluentValue<'a> + Send + Sync>;
1713

1814
pub enum Entry {
1915
Message([usize; 2]),

fluent-bundle/src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
//! assert_eq!(&value, "Hello, world!");
3838
//!
3939
//! let mut args = HashMap::new();
40-
//! args.insert("name".to_string(), FluentValue::from("John"));
40+
//! args.insert("name", FluentValue::from("John"));
4141
//!
4242
//! let msg = bundle.get_message("intro").expect("Message doesn't exist.");
4343
//! let mut errors = vec![];
@@ -59,6 +59,6 @@ pub mod resolve;
5959
pub mod resource;
6060
pub mod types;
6161

62-
pub use bundle::FluentBundle;
62+
pub use bundle::{FluentArgs, FluentBundle};
6363
pub use resource::FluentResource;
6464
pub use types::FluentValue;

fluent-bundle/src/resolve.rs

+7-14
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,12 @@
77
//! [`FluentBundle`]: ../bundle/struct.FluentBundle.html
88
99
use std::borrow::Borrow;
10-
use std::collections::HashMap;
1110
use std::fmt::Write;
1211

1312
use fluent_syntax::ast;
1413
use fluent_syntax::unicode::unescape_unicode;
1514

16-
use crate::bundle::FluentBundle;
15+
use crate::bundle::{FluentArgs, FluentBundle};
1716
use crate::entry::GetEntry;
1817
use crate::resource::FluentResource;
1918
use crate::types::DisplayableNode;
@@ -31,20 +30,17 @@ pub struct Scope<'bundle, R: Borrow<FluentResource>> {
3130
/// The current `FluentBundle` instance.
3231
pub bundle: &'bundle FluentBundle<R>,
3332
/// The current arguments passed by the developer.
34-
args: Option<&'bundle HashMap<String, FluentValue<'bundle>>>,
33+
args: Option<&'bundle FluentArgs<'bundle>>,
3534
/// Local args
36-
local_args: Option<HashMap<String, FluentValue<'bundle>>>,
35+
local_args: Option<FluentArgs<'bundle>>,
3736
/// Tracks hashes to prevent infinite recursion.
3837
travelled: smallvec::SmallVec<[&'bundle ast::Pattern<'bundle>; 2]>,
3938
/// Track errors accumulated during resolving.
4039
pub errors: Vec<ResolverError>,
4140
}
4241

4342
impl<'bundle, R: Borrow<FluentResource>> Scope<'bundle, R> {
44-
pub fn new(
45-
bundle: &'bundle FluentBundle<R>,
46-
args: Option<&'bundle HashMap<String, FluentValue>>,
47-
) -> Self {
43+
pub fn new(bundle: &'bundle FluentBundle<R>, args: Option<&'bundle FluentArgs>) -> Self {
4844
Scope {
4945
bundle,
5046
args,
@@ -281,23 +277,20 @@ impl<'source> ResolveValue<'source> for ast::InlineExpression<'source> {
281277
fn get_arguments<'bundle, R>(
282278
scope: &mut Scope<'bundle, R>,
283279
arguments: &'bundle Option<ast::CallArguments<'bundle>>,
284-
) -> (
285-
Vec<FluentValue<'bundle>>,
286-
HashMap<String, FluentValue<'bundle>>,
287-
)
280+
) -> (Vec<FluentValue<'bundle>>, FluentArgs<'bundle>)
288281
where
289282
R: Borrow<FluentResource>,
290283
{
291284
let mut resolved_positional_args = Vec::new();
292-
let mut resolved_named_args = HashMap::new();
285+
let mut resolved_named_args = FluentArgs::new();
293286

294287
if let Some(ast::CallArguments { named, positional }) = arguments {
295288
for expression in positional {
296289
resolved_positional_args.push(expression.resolve(scope));
297290
}
298291

299292
for arg in named {
300-
resolved_named_args.insert(arg.name.name.to_string(), arg.value.resolve(scope));
293+
resolved_named_args.insert(arg.name.name, arg.value.resolve(scope));
301294
}
302295
}
303296

fluent-bundle/tests/resolver_fixtures.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use std::iter;
66
use std::path::Path;
77
use std::str::FromStr;
88

9+
use fluent_bundle::bundle::FluentArgs;
910
use fluent_bundle::errors::FluentError;
1011
use fluent_bundle::resolve::ResolverError;
1112
use fluent_bundle::{FluentBundle as FluentBundleGeneric, FluentResource, FluentValue};
@@ -264,14 +265,14 @@ fn test_test(test: &Test, defaults: &Option<TestDefaults>, mut scope: Scope) {
264265
))
265266
};
266267

267-
let args: Option<HashMap<String, FluentValue>> = assert.args.as_ref().map(|args| {
268+
let args: Option<FluentArgs> = assert.args.as_ref().map(|args| {
268269
args.iter()
269270
.map(|(k, v)| {
270271
let val = match f64::from_str(v) {
271272
Ok(_) => FluentValue::Number(v.into()),
272273
Err(_) => FluentValue::String(v.into()),
273274
};
274-
(k.to_string(), val)
275+
(k.as_str(), val)
275276
})
276277
.collect()
277278
});

0 commit comments

Comments
 (0)