Skip to content

Commit

Permalink
Slightly generalize FluentArgs to accept Into<FluentValue>
Browse files Browse the repository at this point in the history
  • Loading branch information
zbraniecki committed Feb 7, 2021
1 parent 3c00724 commit 541cf48
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 41 deletions.
2 changes: 1 addition & 1 deletion fluent-bundle/examples/custom_formatter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ key-var-with-arg = Here is a variable formatted with an argument { NUMBER($num,
..Default::default()
},
);
args.add("num", num.into());
args.add("num", num);
let value = bundle.format_pattern(&pattern, Some(&args), &mut errors);

// Notice, that since we specificed minimum and maximum fraction digits options
Expand Down
2 changes: 1 addition & 1 deletion fluent-bundle/examples/custom_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ key-date = Today is { DATETIME($epoch, dateStyle: "long", timeStyle: "short") }

let mut args = FluentArgs::new();
let epoch: u64 = 1580127760093;
args.add("epoch", epoch.into());
args.add("epoch", epoch);
let value = bundle.format_pattern(pattern, Some(&args), &mut errors);
println!("{}", value);
}
2 changes: 1 addition & 1 deletion fluent-bundle/examples/external_arguments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ unread-emails =
println!("{}", value);

let mut args = FluentArgs::new();
args.add("emailCount", 1.into());
args.add("emailCount", 1);

let msg = bundle
.get_message("unread-emails")
Expand Down
30 changes: 10 additions & 20 deletions fluent-bundle/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub struct FluentArgs<'args>(Vec<(Cow<'args, str>, FluentValue<'args>)>);

impl<'args> FluentArgs<'args> {
pub fn new() -> Self {
Self(vec![])
Self::default()
}

pub fn with_capacity(capacity: usize) -> Self {
Expand All @@ -22,37 +22,27 @@ impl<'args> FluentArgs<'args> {
self.0.iter().find(|(k, _)| key == *k).map(|(_, v)| v)
}

pub fn add<K>(&mut self, key: K, value: FluentValue<'args>)
pub fn add<K, V>(&mut self, key: K, value: V)
where
K: Into<Cow<'args, str>>,
V: Into<FluentValue<'args>>,
{
self.0.push((key.into(), value));
self.0.push((key.into(), value.into()));
}

pub fn iter(&self) -> impl Iterator<Item = (&str, &FluentValue)> {
self.0.iter().map(|(k, v)| (k.as_ref(), v))
}
}

impl<'args> FromIterator<(&'args str, FluentValue<'args>)> for FluentArgs<'args> {
impl<'args, K, V> FromIterator<(K, V)> for FluentArgs<'args>
where
K: Into<Cow<'args, str>>,
V: Into<FluentValue<'args>>,
{
fn from_iter<I>(iter: I) -> Self
where
I: IntoIterator<Item = (&'args str, FluentValue<'args>)>,
{
let mut c = FluentArgs::new();

for (k, v) in iter {
c.add(k, v);
}

c
}
}

impl<'args> FromIterator<(String, FluentValue<'args>)> for FluentArgs<'args> {
fn from_iter<I>(iter: I) -> Self
where
I: IntoIterator<Item = (String, FluentValue<'args>)>,
I: IntoIterator<Item = (K, V)>,
{
let mut c = FluentArgs::new();

Expand Down
24 changes: 18 additions & 6 deletions fluent-bundle/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
//! Fluent is a modern localization system designed to improve how software is translated.
//!
//! The Rust implementation provides the low level components for syntax operations, like parser
//! and AST, and the core localization struct - [`FluentBundle`].
//! `fluent-bundle` is the mid-level component of the [Fluent Localization
//! System](https://www.projectfluent.org).
//!
//! [`FluentBundle`] is the low level container for storing and formatting localization messages
//! It builds on top of the low level [`fluent-syntax`](../fluent-syntax) package, and provides
//! foundational types and structures required for executing localization at runtime.
//!
//! # Fluent Bundle
//! The core structure on that level is [`FluentBundle`].
//!
//! [`FluentBundle`] is a low level container for storing and formatting localization messages
//! in a single locale.
//!
//! This crate provides also a number of structures needed for a localization API such as [`FluentResource`],
Expand All @@ -22,10 +28,13 @@
//! // Used to provide a locale for the bundle.
//! use unic_langid::langid;
//!
//! let ftl_string = String::from("
//! let ftl_string = r#"
//!
//! hello-world = Hello, world!
//! intro = Welcome, { $name }.
//! ");
//!
//! "#.to_string();
//!
//! let res = FluentResource::try_new(ftl_string)
//! .expect("Failed to parse an FTL string.");
//!
Expand All @@ -38,15 +47,18 @@
//!
//! let msg = bundle.get_message("hello-world")
//! .expect("Message doesn't exist.");
//!
//! let mut errors = vec![];
//!
//! let pattern = msg.value
//! .expect("Message has no value.");
//!
//! let value = bundle.format_pattern(&pattern, None, &mut errors);
//!
//! assert_eq!(&value, "Hello, world!");
//!
//! let mut args = FluentArgs::new();
//! args.add("name", FluentValue::from("John"));
//! args.add("name", "John");
//!
//! let msg = bundle.get_message("intro")
//! .expect("Message doesn't exist.");
Expand Down
2 changes: 1 addition & 1 deletion fluent-bundle/tests/resolver_fixtures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ fn test_test(test: &Test, defaults: &Option<TestDefaults>, mut scope: Scope) {
let args: Option<FluentArgs> = assert.args.as_ref().map(|args| {
args.iter()
.map(|(k, v)| {
let val = match v {
let val: FluentValue = match v {
TestArgumentValue::String(s) => s.as_str().into(),
TestArgumentValue::Number(n) => n.into(),
};
Expand Down
20 changes: 10 additions & 10 deletions fluent-bundle/tests/types_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,16 +104,16 @@ fn fluent_number_style() {
let mut fno = FluentNumberOptions::default();

let mut args = FluentArgs::new();
args.add("style", "currency".into());
args.add("currency", "EUR".into());
args.add("currencyDisplay", "code".into());
args.add("useGrouping", "false".into());
args.add("minimumIntegerDigits", 3.into());
args.add("minimumFractionDigits", 3.into());
args.add("maximumFractionDigits", 8.into());
args.add("minimumSignificantDigits", 1.into());
args.add("maximumSignificantDigits", 10.into());
args.add("someRandomOption", 10.into());
args.add("style", "currency");
args.add("currency", "EUR");
args.add("currencyDisplay", "code");
args.add("useGrouping", "false");
args.add("minimumIntegerDigits", 3);
args.add("minimumFractionDigits", 3);
args.add("maximumFractionDigits", 8);
args.add("minimumSignificantDigits", 1);
args.add("maximumSignificantDigits", 10);
args.add("someRandomOption", 10);

fno.merge(&args);

Expand Down
2 changes: 1 addition & 1 deletion fluent-syntax/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
//! `fluent-syntax` is the lowest level component of the [Fluent Localization
//! System](https://www.projectfluent.org).
//!
//! It exposes components necessary for parsing and operating on Fluent Translation Lists ("FTL").
//! It exposes components necessary for parsing and tooling operations on Fluent Translation Lists ("FTL").
//!
//! The crate provides a [`parser`] module which allows for parsing of an
//! input string to an Abstract Syntax Tree defined in the [`ast`] module.
Expand Down

0 comments on commit 541cf48

Please sign in to comment.