Skip to content

Commit

Permalink
Switch FluentMessage to be lighter wrapper around ast::Message
Browse files Browse the repository at this point in the history
  • Loading branch information
zbraniecki committed Feb 8, 2021
1 parent b707922 commit cce506e
Show file tree
Hide file tree
Showing 21 changed files with 104 additions and 89 deletions.
12 changes: 6 additions & 6 deletions fluent-bundle/benches/resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,13 +118,13 @@ fn resolver_bench(c: &mut Criterion) {
for id in &ids {
let msg = bundle.get_message(id).expect("Message found");
let mut errors = vec![];
if let Some(value) = msg.value {
if let Some(value) = msg.value() {
let _ = bundle.write_pattern(&mut s, value, args.as_ref(), &mut errors);
s.clear();
}
for attr in msg.attributes {
for attr in msg.attributes() {
let _ =
bundle.write_pattern(&mut s, attr.value, args.as_ref(), &mut errors);
bundle.write_pattern(&mut s, attr.value(), args.as_ref(), &mut errors);
s.clear();
}
assert!(errors.len() == 0, "Resolver errors: {:#?}", errors);
Expand All @@ -144,11 +144,11 @@ fn resolver_bench(c: &mut Criterion) {
for id in &ids {
let msg = bundle.get_message(id).expect("Message found");
let mut errors = vec![];
if let Some(value) = msg.value {
if let Some(value) = msg.value() {
let _ = bundle.format_pattern(value, args.as_ref(), &mut errors);
}
for attr in msg.attributes {
let _ = bundle.format_pattern(attr.value, args.as_ref(), &mut errors);
for attr in msg.attributes() {
let _ = bundle.format_pattern(attr.value(), args.as_ref(), &mut errors);
}
assert!(errors.len() == 0, "Resolver errors: {:#?}", errors);
}
Expand Down
6 changes: 3 additions & 3 deletions fluent-bundle/benches/resolver_iai.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,15 @@ fn iai_resolve() {
for id in &ids {
let msg = bundle.get_message(id).expect("Message found");
let mut errors = vec![];
if let Some(value) = msg.value {
if let Some(value) = msg.value() {
bundle
.write_pattern(&mut s, value, args.as_ref(), &mut errors)
.expect("Failed to write a pattern.");
s.clear();
}
for attr in msg.attributes {
for attr in msg.attributes() {
bundle
.write_pattern(&mut s, attr.value, args.as_ref(), &mut errors)
.write_pattern(&mut s, attr.value(), args.as_ref(), &mut errors)
.expect("Failed to write a pattern.");
s.clear();
}
Expand Down
12 changes: 6 additions & 6 deletions fluent-bundle/examples/custom_formatter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ key-var-with-arg = Here is a variable formatted with an argument { NUMBER($num,
let msg = bundle
.get_message("key-implicit")
.expect("Message doesn't exist.");
let pattern = msg.value.expect("Message has no value.");
let pattern = msg.value().expect("Message has no value.");
let value = bundle.format_pattern(&pattern, None, &mut errors);
assert_eq!(value, "Here is an implicitly encoded number: 5.");
println!("{}", value);
Expand All @@ -71,7 +71,7 @@ key-var-with-arg = Here is a variable formatted with an argument { NUMBER($num,
let msg = bundle
.get_message("key-implicit")
.expect("Message doesn't exist.");
let pattern = msg.value.expect("Message has no value.");
let pattern = msg.value().expect("Message has no value.");
let value = bundle.format_pattern(&pattern, None, &mut errors);
assert_eq!(value, "Here is an implicitly encoded number: CUSTOM(5).");
println!("{}", value);
Expand All @@ -81,15 +81,15 @@ key-var-with-arg = Here is a variable formatted with an argument { NUMBER($num,
let msg = bundle
.get_message("key-explicit")
.expect("Message doesn't exist.");
let pattern = msg.value.expect("Message has no value.");
let pattern = msg.value().expect("Message has no value.");
let value = bundle.format_pattern(&pattern, None, &mut errors);
assert_eq!(value, "Here is an explicitly encoded number: CUSTOM(5).");
println!("{}", value);

let msg = bundle
.get_message("key-var-implicit")
.expect("Message doesn't exist.");
let pattern = msg.value.expect("Message has no value.");
let pattern = msg.value().expect("Message has no value.");
let mut args = FluentArgs::new();
args.set("num", FluentValue::from(-15));
let value = bundle.format_pattern(&pattern, Some(&args), &mut errors);
Expand All @@ -102,7 +102,7 @@ key-var-with-arg = Here is a variable formatted with an argument { NUMBER($num,
let msg = bundle
.get_message("key-var-explicit")
.expect("Message doesn't exist.");
let pattern = msg.value.expect("Message has no value.");
let pattern = msg.value().expect("Message has no value.");
let mut args = FluentArgs::new();
args.set("num", FluentValue::from(-15));
let value = bundle.format_pattern(&pattern, Some(&args), &mut errors);
Expand All @@ -118,7 +118,7 @@ key-var-with-arg = Here is a variable formatted with an argument { NUMBER($num,
let msg = bundle
.get_message("key-var-explicit")
.expect("Message doesn't exist.");
let pattern = msg.value.expect("Message has no value.");
let pattern = msg.value().expect("Message has no value.");
let mut args = FluentArgs::new();
let num = FluentNumber::new(
25.2,
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 @@ -179,7 +179,7 @@ key-date = Today is { DATETIME($epoch, dateStyle: "long", timeStyle: "short") }
let msg = bundle
.get_message("key-date")
.expect("Failed to retrieve the message.");
let pattern = msg.value.expect("Message has no value.");
let pattern = msg.value().expect("Message has no value.");
let mut errors = vec![];

let mut args = FluentArgs::new();
Expand Down
6 changes: 3 additions & 3 deletions fluent-bundle/examples/external_arguments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ unread-emails =
.get_message("hello-world")
.expect("Message doesn't exist.");
let mut errors = vec![];
let pattern = msg.value.expect("Message has no value.");
let pattern = msg.value().expect("Message has no value.");
let value = bundle.format_pattern(&pattern, Some(&args), &mut errors);
println!("{}", value);

let msg = bundle.get_message("ref").expect("Message doesn't exist.");
let mut errors = vec![];
let pattern = msg.value.expect("Message has no value.");
let pattern = msg.value().expect("Message has no value.");
let value = bundle.format_pattern(&pattern, Some(&args), &mut errors);
println!("{}", value);

Expand All @@ -44,7 +44,7 @@ unread-emails =
.get_message("unread-emails")
.expect("Message doesn't exist.");
let mut errors = vec![];
let pattern = msg.value.expect("Message has no value.");
let pattern = msg.value().expect("Message has no value.");
let value = bundle.format_pattern(&pattern, Some(&args), &mut errors);
println!("{}", value);
}
6 changes: 3 additions & 3 deletions fluent-bundle/examples/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,23 +60,23 @@ fn main() {
.get_message("hello-world")
.expect("Message doesn't exist.");
let mut errors = vec![];
let pattern = msg.value.expect("Message has no value.");
let pattern = msg.value().expect("Message has no value.");
let value = bundle.format_pattern(&pattern, None, &mut errors);
assert_eq!(&value, "Hey there! \u{2068}I'm a function!\u{2069}");

let msg = bundle
.get_message("meaning-of-life")
.expect("Message doesn't exist.");
let mut errors = vec![];
let pattern = msg.value.expect("Message has no value.");
let pattern = msg.value().expect("Message has no value.");
let value = bundle.format_pattern(&pattern, None, &mut errors);
assert_eq!(&value, "The answer to life, the universe, and everything");

let msg = bundle
.get_message("all-your-base")
.expect("Message doesn't exist.");
let mut errors = vec![];
let pattern = msg.value.expect("Message has no value.");
let pattern = msg.value().expect("Message has no value.");
let value = bundle.format_pattern(&pattern, None, &mut errors);
assert_eq!(&value, "All your base belong to us");
}
2 changes: 1 addition & 1 deletion fluent-bundle/examples/hello.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ fn main() {
.get_message("hello-world")
.expect("Message doesn't exist.");
let mut errors = vec![];
let pattern = msg.value.expect("Message has no value.");
let pattern = msg.value().expect("Message has no value.");
let value = bundle.format_pattern(&pattern, None, &mut errors);
assert_eq!(&value, "Hello, world!");
}
4 changes: 2 additions & 2 deletions fluent-bundle/examples/message_reference.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ bazbar = { baz } Bar
.get_message("foobar")
.expect("Message doesn't exist.");
let mut errors = vec![];
let pattern = msg.value.expect("Message has no value.");
let pattern = msg.value().expect("Message has no value.");
let value = bundle.format_pattern(&pattern, None, &mut errors);
println!("{}", value);

let msg = bundle
.get_message("bazbar")
.expect("Message doesn't exist.");
let mut errors = vec![];
let pattern = msg.value.expect("Message has no value.");
let pattern = msg.value().expect("Message has no value.");
let value = bundle.format_pattern(&pattern, None, &mut errors);
println!("{}", value);
}
4 changes: 2 additions & 2 deletions fluent-bundle/examples/selector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ hello-world2 = Hello { $name ->
.get_message("hello-world")
.expect("Message doesn't exist.");
let mut errors = vec![];
let pattern = msg.value.expect("Message has no value.");
let pattern = msg.value().expect("Message has no value.");
let value = bundle.format_pattern(&pattern, None, &mut errors);
println!("{}", value);

Expand All @@ -35,7 +35,7 @@ hello-world2 = Hello { $name ->
.get_message("hello-world2")
.expect("Message doesn't exist.");
let mut errors = vec![];
let pattern = msg.value.expect("Message has no value.");
let pattern = msg.value().expect("Message has no value.");
let value = bundle.format_pattern(&pattern, Some(&args), &mut errors);
println!("{}", value);
}
6 changes: 3 additions & 3 deletions fluent-bundle/examples/simple-app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ fn main() {
let msg = bundle
.get_message("response-msg")
.expect("Message doesn't exist.");
let pattern = msg.value.expect("Message has no value.");
let pattern = msg.value().expect("Message has no value.");
let value = bundle.format_pattern(&pattern, Some(&args), &mut errors);
println!("{}", value);
}
Expand All @@ -150,7 +150,7 @@ fn main() {
let msg = bundle
.get_message("input-parse-error")
.expect("Message doesn't exist.");
let pattern = msg.value.expect("Message has no value.");
let pattern = msg.value().expect("Message has no value.");
let value = bundle.format_pattern(&pattern, Some(&args), &mut errors);
println!("{}", value);
}
Expand All @@ -161,7 +161,7 @@ fn main() {
let msg = bundle
.get_message("missing-arg-error")
.expect("Message doesn't exist.");
let pattern = msg.value.expect("Message has no value.");
let pattern = msg.value().expect("Message has no value.");
let value = bundle.format_pattern(&pattern, None, &mut errors);
println!("{}", value);
}
Expand Down
19 changes: 10 additions & 9 deletions fluent-bundle/src/bundle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use std::default::Default;
use std::fmt;

use fluent_syntax::ast;
use intl_memoizer::IntlLangMemoizer;
use unic_langid::LanguageIdentifier;

use crate::args::FluentArgs;
Expand Down Expand Up @@ -170,7 +171,7 @@ impl<R, M> FluentBundle<R, M> {
/// let mut errors = vec![];
/// let msg = bundle.get_message("hello")
/// .expect("Failed to retrieve the message");
/// let value = msg.value.expect("Failed to retrieve the value of the message");
/// let value = msg.value().expect("Failed to retrieve the value of the message");
/// assert_eq!(bundle.format_pattern(value, None, &mut errors), "Another Hi!");
/// ```
///
Expand Down Expand Up @@ -290,7 +291,7 @@ impl<R, M> FluentBundle<R, M> {
/// let msg = bundle.get_message("hello-world");
/// assert_eq!(msg.is_some(), true);
/// ```
pub fn get_message(&self, id: &str) -> Option<FluentMessage>
pub fn get_message<'l>(&'l self, id: &str) -> Option<FluentMessage<'l>>
where
R: Borrow<FluentResource>,
{
Expand Down Expand Up @@ -318,7 +319,7 @@ impl<R, M> FluentBundle<R, M> {
/// let msg = bundle.get_message("hello-world")
/// .expect("Failed to retrieve a FluentMessage.");
///
/// let pattern = msg.value
/// let pattern = msg.value()
/// .expect("Missing Value.");
/// let mut errors = vec![];
///
Expand Down Expand Up @@ -365,7 +366,7 @@ impl<R, M> FluentBundle<R, M> {
/// let msg = bundle.get_message("hello-world")
/// .expect("Failed to retrieve a FluentMessage.");
///
/// let pattern = msg.value
/// let pattern = msg.value()
/// .expect("Missing Value.");
/// let mut errors = vec![];
///
Expand Down Expand Up @@ -417,7 +418,7 @@ impl<R, M> FluentBundle<R, M> {
///
/// let msg = bundle.get_message("length").expect("Message doesn't exist.");
/// let mut errors = vec![];
/// let pattern = msg.value.expect("Message has no value.");
/// let pattern = msg.value().expect("Message has no value.");
/// let value = bundle.format_pattern(&pattern, None, &mut errors);
/// assert_eq!(&value, "5");
/// ```
Expand All @@ -440,13 +441,13 @@ impl<R, M> FluentBundle<R, M> {
}
}

impl<R> Default for FluentBundle<R, intl_memoizer::IntlLangMemoizer> {
impl<R> Default for FluentBundle<R, IntlLangMemoizer> {
fn default() -> Self {
Self::new(vec![LanguageIdentifier::default()])
}
}

impl<R> FluentBundle<R, intl_memoizer::IntlLangMemoizer> {
impl<R> FluentBundle<R, IntlLangMemoizer> {
/// Constructs a FluentBundle. The first element in `locales` should be the
/// language this bundle represents, and will be used to determine the
/// correct plural rules for this bundle. You can optionally provide extra
Expand All @@ -473,15 +474,15 @@ impl<R> FluentBundle<R, intl_memoizer::IntlLangMemoizer> {
locales,
resources: vec![],
entries: FxHashMap::default(),
intls: intl_memoizer::IntlLangMemoizer::new(first_locale),
intls: IntlLangMemoizer::new(first_locale),
use_isolating: true,
transform: None,
formatter: None,
}
}
}

impl crate::memoizer::MemoizerKind for intl_memoizer::IntlLangMemoizer {
impl crate::memoizer::MemoizerKind for IntlLangMemoizer {
fn new(lang: LanguageIdentifier) -> Self
where
Self: Sized,
Expand Down
6 changes: 3 additions & 3 deletions fluent-bundle/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@
//!
//! let mut errors = vec![];
//!
//! let pattern = msg.value
//! let pattern = msg.value()
//! .expect("Message has no value.");
//!
//! let value = bundle.format_pattern(&pattern, None, &mut errors);
Expand All @@ -125,7 +125,7 @@
//! let msg = bundle.get_message("intro")
//! .expect("Message doesn't exist.");
//! let mut errors = vec![];
//! let pattern = msg.value.expect("Message has no value.");
//! let pattern = msg.value().expect("Message has no value.");
//! let value = bundle.format_pattern(&pattern, Some(&args), &mut errors);
//!
//! // The FSI/PDI isolation marks ensure that the direction of
Expand Down Expand Up @@ -190,7 +190,7 @@ use bundle::FluentBundle as FluentBundleBase;
///
/// let msg = bundle.get_message("intro").expect("Message doesn't exist.");
/// let mut errors = vec![];
/// let pattern = msg.value.expect("Message has no value.");
/// let pattern = msg.value().expect("Message has no value.");
/// let value = bundle.format_pattern(&pattern, Some(&args), &mut errors);
/// assert_eq!(&value, "Welcome, \u{2068}Rustacean\u{2069}.");
///
Expand Down
Loading

0 comments on commit cce506e

Please sign in to comment.