Skip to content

Commit

Permalink
Update the return signature of fluent bundle methods to match latest …
Browse files Browse the repository at this point in the history
…proposal
  • Loading branch information
Zibi Braniecki committed Aug 31, 2018
1 parent d2d9992 commit 21f3a31
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 61 deletions.
13 changes: 7 additions & 6 deletions fluent/examples/external_arguments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ use std::collections::HashMap;

fn main() {
let mut bundle = FluentBundle::new(&["en"]);
bundle.add_messages(
"
bundle
.add_messages(
"
hello-world = Hello { $name }
ref = The previous message says { hello-world }
unread-emails =
Expand All @@ -16,26 +17,26 @@ unread-emails =
*[other] You have { $emailCount } unread emails
}
",
);
).unwrap();

let mut args = HashMap::new();
args.insert("name", FluentValue::from("John"));

match bundle.format("hello-world", Some(&args)) {
Some(Ok(value)) => println!("{}", value),
Some((value, _)) => println!("{}", value),
_ => println!("None"),
}

match bundle.format("ref", Some(&args)) {
Some(Ok(value)) => println!("{}", value),
Some((value, _)) => println!("{}", value),
_ => println!("None"),
}

let mut args = HashMap::new();
args.insert("emailCount", FluentValue::as_number("1.0").unwrap());

match bundle.format("unread-emails", Some(&args)) {
Some(value) => println!("{}", value.unwrap()),
Some((value, _)) => println!("{}", value),
None => println!("None"),
}
}
12 changes: 8 additions & 4 deletions fluent/examples/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,20 @@ fn main() {
.unwrap();

let value = bundle.format("hello-world", None);
assert_eq!(value, Some(Ok("Hey there! I'm a function!".to_string())));
assert_eq!(
value,
Some(("Hey there! I'm a function!".to_string(), vec![]))
);

let value = bundle.format("meaning-of-life", None);
assert_eq!(
value,
Some(Ok(
"The answer to life, the universe, and everything".to_string()
Some((
"The answer to life, the universe, and everything".to_string(),
vec![]
))
);

let value = bundle.format("all-your-base", None).unwrap().unwrap();
let (value, _) = bundle.format("all-your-base", None).unwrap();
assert_eq!(&value, "All your base belong to us");
}
13 changes: 3 additions & 10 deletions fluent/examples/hello.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,7 @@ use fluent::FluentBundle;

fn main() {
let mut bundle = FluentBundle::new(&["en-US"]);
bundle.add_messages(
"
hello-world = Hello, world!
.title = Foo
",
);

let value = bundle.format_message("hello-world", None);
println!("{:#?}", value);
// assert_eq!(value, Some("Hello, world!".to_string()));
bundle.add_messages("hello-world = Hello, world!").unwrap();
let value = bundle.format("hello-world", None).unwrap();
assert_eq!(&value.0, "Hello, world!");
}
11 changes: 6 additions & 5 deletions fluent/examples/message_reference.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,22 @@ use fluent::context::FluentBundle;

fn main() {
let mut bundle = FluentBundle::new(&["x-testing"]);
bundle.add_messages(
"
bundle
.add_messages(
"
foo = Foo
foobar = { foo } Bar
bazbar = { baz } Bar
",
);
).unwrap();

match bundle.format("foobar", None) {
Some(Ok(value)) => println!("{}", value),
Some((value, _)) => println!("{}", value),
_ => println!("None"),
}

match bundle.format("bazbar", None) {
Some(Ok(value)) => println!("{}", value),
Some((value, _)) => println!("{}", value),
_ => println!("None"),
}
}
11 changes: 6 additions & 5 deletions fluent/examples/selector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ use std::collections::HashMap;

fn main() {
let mut bundle = FluentBundle::new(&["x-testing"]);
bundle.add_messages(
"
bundle
.add_messages(
"
hello-world = Hello {
*[one] World
[two] Moon
Expand All @@ -18,18 +19,18 @@ hello-world2 = Hello { $name ->
[moon] Moon
}
",
);
).unwrap();

match bundle.format("hello-world", None) {
Some(Ok(value)) => println!("{}", value),
Some((value, _)) => println!("{}", value),
_ => println!("None"),
}

let mut args = HashMap::new();
args.insert("name", FluentValue::from("moon"));

match bundle.format("hello-world2", Some(&args)) {
Some(Ok(value)) => println!("{}", value),
Some((value, _)) => println!("{}", value),
_ => println!("None"),
}
}
12 changes: 3 additions & 9 deletions fluent/examples/simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,10 +136,7 @@ fn main() {
args.insert("input", FluentValue::from(i));
args.insert("value", FluentValue::from(collatz(i)));
// 6.3. Format the message.
println!(
"{}",
bundle.format("response-msg", Some(&args)).unwrap().unwrap()
);
println!("{}", bundle.format("response-msg", Some(&args)).unwrap().0);
}
Err(err) => {
let mut args = HashMap::new();
Expand All @@ -150,16 +147,13 @@ fn main() {
bundle
.format("input-parse-error-msg", Some(&args))
.unwrap()
.unwrap()
.0
);
}
}
}
None => {
println!(
"{}",
bundle.format("missing-arg-error", None).unwrap().unwrap()
);
println!("{}", bundle.format("missing-arg-error", None).unwrap().0);
}
}
}
Expand Down
25 changes: 13 additions & 12 deletions fluent/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,8 @@ impl<'bundle> FluentBundle<'bundle> {
match FluentResource::from_string(source) {
Ok(res) => self.add_resource(res),
Err((res, err)) => {
let mut errors: Vec<FluentError> = err
.into_iter()
.map(FluentError::ParserError)
.collect();
let mut errors: Vec<FluentError> =
err.into_iter().map(FluentError::ParserError).collect();

self.add_resource(res).map_err(|err| {
for e in err {
Expand Down Expand Up @@ -153,12 +151,13 @@ impl<'bundle> FluentBundle<'bundle> {
&self,
path: &str,
args: Option<&HashMap<&str, FluentValue>>,
) -> Option<Result<String, (String, Vec<FluentError>)>> {
) -> Option<(String, Vec<FluentError>)> {
let env = Env {
bundle: self,
args,
travelled: RefCell::new(Vec::new()),
};

let mut errors = vec![];

if let Some(ptr_pos) = path.find('.') {
Expand All @@ -170,12 +169,14 @@ impl<'bundle> FluentBundle<'bundle> {
if attribute.id.name == attr_name {
match attribute.to_value(&env) {
Ok(val) => {
let s = val.format(self);
return Some(Ok(s));
return Some((val.format(self), errors));
}
Err(err) => {
errors.push(FluentError::ResolverError(err));
return Some(Err((path.to_string(), errors)));
// XXX: In the future we'll want to get the partial
// value out of resolver and return it here.
// We also expect to get a Vec or errors out of resolver.
return Some((path.to_string(), errors));
}
}
}
Expand All @@ -187,11 +188,11 @@ impl<'bundle> FluentBundle<'bundle> {
match message.to_value(&env) {
Ok(val) => {
let s = val.format(self);
return Some(Ok(s));
return Some((s, errors));
}
Err(err) => {
errors.push(FluentError::ResolverError(err));
return Some(Err((message_id.to_string(), errors)));
return Some((message_id.to_string(), errors));
}
}
}
Expand All @@ -203,7 +204,7 @@ impl<'bundle> FluentBundle<'bundle> {
&self,
message_id: &str,
args: Option<&HashMap<&str, FluentValue>>,
) -> Option<Result<Message, (Message, Vec<FluentError>)>> {
) -> Option<(Message, Vec<FluentError>)> {
let mut errors = vec![];

let env = Env {
Expand Down Expand Up @@ -237,6 +238,6 @@ impl<'bundle> FluentBundle<'bundle> {
}
}

Some(Ok(Message { value, attributes }))
Some((Message { value, attributes }, errors))
}
}
4 changes: 2 additions & 2 deletions fluent/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@
//! );
//!
//! let value = bundle.format("hello-world", None);
//! assert_eq!(value, Some(Ok("Hello, world!".to_string())));
//! assert_eq!(value, Some(("Hello, world!".to_string(), vec![])));
//!
//! let mut args = HashMap::new();
//! args.insert("name", FluentValue::from("John"));
//!
//! let value = bundle.format("intro", Some(&args));
//! assert_eq!(value, Some(Ok("Welcome, John.".to_string())));
//! assert_eq!(value, Some(("Welcome, John.".to_string(), vec![])));
//! ```

extern crate failure;
Expand Down
13 changes: 5 additions & 8 deletions fluent/tests/helpers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,22 @@ use fluent::context::FluentError;
use fluent::context::Message;

#[allow(dead_code)]
pub fn assert_format_none(result: Option<Result<String, (String, Vec<FluentError>)>>) {
pub fn assert_format_none(result: Option<(String, Vec<FluentError>)>) {
assert!(result.is_none());
}

#[allow(dead_code)]
pub fn assert_format_no_errors(
result: Option<Result<String, (String, Vec<FluentError>)>>,
expected: &str,
) {
pub fn assert_format_no_errors(result: Option<(String, Vec<FluentError>)>, expected: &str) {
assert!(result.is_some());
assert_eq!(result, Some(Ok(expected.to_string())));
assert_eq!(result, Some((expected.to_string(), vec![])));
}

#[allow(dead_code)]
pub fn assert_format_message_no_errors(
result: Option<Result<Message, (Message, Vec<FluentError>)>>,
result: Option<(Message, Vec<FluentError>)>,
expected: Message,
) {
assert_eq!(result, Some(Ok(expected)));
assert_eq!(result, Some((expected, vec![])));
}

pub fn assert_add_messages_no_errors(result: Result<(), Vec<FluentError>>) {
Expand Down

0 comments on commit 21f3a31

Please sign in to comment.