Skip to content

Commit

Permalink
Merge pull request #33 from stasm/cargo-fmt
Browse files Browse the repository at this point in the history
Run rustfmt-nightly on Travis
  • Loading branch information
stasm committed Oct 16, 2017
2 parents 9fa3c0a + c3bc55b commit 72c72ec
Show file tree
Hide file tree
Showing 22 changed files with 317 additions and 355 deletions.
15 changes: 7 additions & 8 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,15 @@ addons:
- kalakris-cmake

before_script:
- |
cargo install --force cargo-travis &&
export PATH=$HOME/.cargo/bin:$PATH
- cargo install --force cargo-travis
- export PATH=$HOME/.cargo/bin:$PATH
- which rustfmt || cargo install rustfmt-nightly

# the main build
script:
- |
cargo build &&
cargo test &&
cargo doc
- cargo fmt -- --write-mode=diff
- cargo build
- cargo test
- cargo doc

after_success:
# measure code coverage and upload to coveralls.io
Expand Down
18 changes: 12 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,15 @@ extern crate fluent;

use fluent::MessageContext;

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

let value = ctx.get_message("hello-world")
.and_then(|message| ctx.format(message, None));
let value = ctx.get_message("hello-world")
.and_then(|message| ctx.format(message, None));

assert_eq!(value, Some("Hello, world!".to_string()));
assert_eq!(value, Some("Hello, world!".to_string()));
}
```


Expand All @@ -64,7 +66,11 @@ Local Development
cargo build
cargo test
cargo bench
cargo run --example simple
cargo run --example hello

When submitting a PR please use [`cargo fmt`][] (nightly).

[`cargo fmt`]: https://github.com/rust-lang-nursery/rustfmt


Learn the FTL syntax
Expand Down
40 changes: 18 additions & 22 deletions benches/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#![feature(test)]

extern crate test;
extern crate fluent;
extern crate test;

use std::io;
use std::io::Read;
Expand All @@ -23,7 +23,7 @@ fn read_file(path: &str) -> Result<String, io::Error> {
fn bench_simple_parse(b: &mut Bencher) {
let source = read_file("./benches/simple.ftl").expect("Couldn't load file");

b.iter(|| { parse(&source).unwrap(); });
b.iter(|| parse(&source).unwrap());
}

#[bench]
Expand All @@ -43,11 +43,9 @@ fn bench_simple_format(b: &mut Bencher) {
let mut ctx = MessageContext::new(&["x-testing"]);
ctx.add_messages(&source);

b.iter(|| {
for id in &ids {
if let Some(message) = ctx.get_message(id.as_str()) {
let _value = ctx.format(message, None);
}
b.iter(|| for id in &ids {
if let Some(message) = ctx.get_message(id.as_str()) {
let _value = ctx.format(message, None);
}
});
}
Expand All @@ -56,7 +54,7 @@ fn bench_simple_format(b: &mut Bencher) {
fn bench_menubar_parse(b: &mut Bencher) {
let source = read_file("./benches/menubar.ftl").expect("Couldn't load file");

b.iter(|| { parse(&source).unwrap(); });
b.iter(|| parse(&source).unwrap());
}

#[bench]
Expand All @@ -76,20 +74,18 @@ fn bench_menubar_format(b: &mut Bencher) {
let mut ctx = MessageContext::new(&["x-testing"]);
ctx.add_messages(&source);

b.iter(|| {
for id in &ids {
// In real-life usage we'd have different fallback strategies for missing messages
// depending on the type of the widget this message was supposed to translate. Some
// widgets may only expect attributes and they shouldn't be forced to display a value.
// Here however it doesn't matter because we know for certain that the message for `id`
// exists.
if let Some(message) = ctx.get_message(id.as_str()) {
let _value = ctx.format(message, None);

if let Some(ref attributes) = message.attributes {
for attr in attributes {
let _value = ctx.format(attr, None);
}
b.iter(|| for id in &ids {
// In real-life usage we'd have different fallback strategies for missing messages
// depending on the type of the widget this message was supposed to translate. Some
// widgets may only expect attributes and they shouldn't be forced to display a value.
// Here however it doesn't matter because we know for certain that the message for `id`
// exists.
if let Some(message) = ctx.get_message(id.as_str()) {
let _value = ctx.format(message, None);

if let Some(ref attributes) = message.attributes {
for attr in attributes {
let _value = ctx.format(attr, None);
}
}
}
Expand Down
18 changes: 9 additions & 9 deletions examples/external_arguments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,26 @@ fn main() {
let mut args = HashMap::new();
args.insert("name", FluentValue::from("John"));

match ctx.get_message("hello-world").and_then(|msg| {
ctx.format(msg, Some(&args))
}) {
match ctx.get_message("hello-world")
.and_then(|msg| ctx.format(msg, Some(&args)))
{
Some(value) => println!("{}", value),
None => println!("None"),
}

match ctx.get_message("ref").and_then(
|msg| ctx.format(msg, Some(&args)),
) {
match ctx.get_message("ref")
.and_then(|msg| ctx.format(msg, Some(&args)))
{
Some(value) => println!("{}", value),
None => println!("None"),
}

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

match ctx.get_message("unread-emails").and_then(|msg| {
ctx.format(msg, Some(&args))
}) {
match ctx.get_message("unread-emails")
.and_then(|msg| ctx.format(msg, Some(&args)))
{
Some(value) => println!("{}", value),
None => println!("None"),
}
Expand Down
13 changes: 13 additions & 0 deletions examples/hello.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
extern crate fluent;

use fluent::MessageContext;

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

let value = ctx.get_message("hello-world")
.and_then(|message| ctx.format(message, None));

assert_eq!(value, Some("Hello, world!".to_string()));
}
12 changes: 6 additions & 6 deletions examples/message_reference.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@ fn main() {
ctx.add_messages("foobar = { foo } Bar");
ctx.add_messages("bazbar = { baz } Bar");

match ctx.get_message("foobar").and_then(
|msg| ctx.format(msg, None),
) {
match ctx.get_message("foobar")
.and_then(|msg| ctx.format(msg, None))
{
Some(value) => println!("{}", value),
None => println!("None"),
}

match ctx.get_message("bazbar").and_then(
|msg| ctx.format(msg, None),
) {
match ctx.get_message("bazbar")
.and_then(|msg| ctx.format(msg, None))
{
Some(value) => println!("{}", value),
None => println!("None"),
}
Expand Down
12 changes: 6 additions & 6 deletions examples/selector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,19 @@ hello-world2 = Hello { $name ->
",
);

match ctx.get_message("hello-world").and_then(
|msg| ctx.format(msg, None),
) {
match ctx.get_message("hello-world")
.and_then(|msg| ctx.format(msg, None))
{
Some(value) => println!("{}", value),
None => println!("None"),
}

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

match ctx.get_message("hello-world2").and_then(|msg| {
ctx.format(msg, Some(&args))
}) {
match ctx.get_message("hello-world2")
.and_then(|msg| ctx.format(msg, Some(&args)))
{
Some(value) => println!("{}", value),
None => println!("None"),
}
Expand Down
12 changes: 6 additions & 6 deletions examples/simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@ fn main() {
ctx.add_messages("key1 = Value 1");
ctx.add_messages("key2 = Value 2");

match ctx.get_message("key1").and_then(
|msg| ctx.format(msg, None),
) {
match ctx.get_message("key1")
.and_then(|msg| ctx.format(msg, None))
{
Some(value) => println!("{}", value),
None => println!("None"),
}

match ctx.get_message("key2").and_then(
|msg| ctx.format(msg, None),
) {
match ctx.get_message("key2")
.and_then(|msg| ctx.format(msg, None))
{
Some(value) => println!("{}", value),
None => println!("None"),
}
Expand Down
21 changes: 8 additions & 13 deletions src/intl/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -320,8 +320,8 @@ static PLURAL_RULES: &[fn(f32) -> &'static str] = &[
if is_between(n % 10.0, 2.0, 4.0) && !is_between(n % 100.0, 12.0, 14.0) {
return "few";
}
if n != 1.0 && is_between(n % 10.0, 0.0, 1.0) || is_between(n % 10.0, 5.0, 9.0) ||
is_between(n % 100.0, 12.0, 14.0)
if n != 1.0 && is_between(n % 10.0, 0.0, 1.0) || is_between(n % 10.0, 5.0, 9.0)
|| is_between(n % 100.0, 12.0, 14.0)
{
return "many";
}
Expand Down Expand Up @@ -403,9 +403,9 @@ static PLURAL_RULES: &[fn(f32) -> &'static str] = &[
},
/* 20 */
|n| {
if (is_between(n % 10.0, 3.0, 4.0) || n % 10.0 == 9.0) &&
!(is_between(n % 100.0, 10.0, 19.0) || is_between(n % 100.0, 70.0, 79.0) ||
is_between(n % 100.0, 90.0, 99.0))
if (is_between(n % 10.0, 3.0, 4.0) || n % 10.0 == 9.0)
&& !(is_between(n % 100.0, 10.0, 19.0) || is_between(n % 100.0, 70.0, 79.0)
|| is_between(n % 100.0, 90.0, 99.0))
{
return "few";
}
Expand Down Expand Up @@ -438,9 +438,7 @@ static PLURAL_RULES: &[fn(f32) -> &'static str] = &[
"other"
},
/* 23 */
|n| if is_between(n, 0.0, 1.0) ||
is_between(n, 11.0, 99.0)
{
|n| if is_between(n, 0.0, 1.0) || is_between(n, 11.0, 99.0) {
"one"
} else {
"other"
Expand Down Expand Up @@ -644,11 +642,8 @@ pub struct PluralRules {

impl PluralRules {
pub fn new(locales: &[&str]) -> PluralRules {
let supported = negotiate_languages(
locales,
LOCALES,
Some("en"),
&NegotiationStrategy::Lookup);
let supported =
negotiate_languages(locales, LOCALES, Some("en"), &NegotiationStrategy::Lookup);

let locale = supported[0].to_owned();
let f = match get_plural_rule(supported[0]) {
Expand Down
Loading

0 comments on commit 72c72ec

Please sign in to comment.