Skip to content

Commit

Permalink
Use from_fn for generate_messages generator
Browse files Browse the repository at this point in the history
  • Loading branch information
Zibi Braniecki committed Apr 18, 2019
1 parent d55e2eb commit 934dd77
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 73 deletions.
38 changes: 20 additions & 18 deletions fluent-fallback/examples/simple.rs
Expand Up @@ -27,6 +27,7 @@ use std::fs;
use std::fs::File;
use std::io;
use std::io::prelude::*;
use std::iter::from_fn;
use std::str::FromStr;

/// We need a generic file read helper function to
Expand Down Expand Up @@ -112,26 +113,27 @@ fn main() {
// the iterator over FluentBundle instances.
let res_path_scheme = "./examples/resources/{locale}/{res_id}";
let generate_messages = |res_ids: &[String]| {
let mut bundles = vec![];
let mut locales = locales.iter();
let res_mgr = &resources;
let res_ids = res_ids.to_vec();

for locale in locales.clone() {
let mut bundle = FluentBundle::new(&[&locale]);
let res_path = res_path_scheme.replace("{locale}", &locale);
for res_id in res_ids {
let path = res_path.replace("{res_id}", res_id);
let res = if let Some(res) = resources.get(&path) {
res
} else {
let source = read_file(&path).unwrap();
let res = FluentResource::try_new(source).unwrap();
resources.insert(path, Box::new(res))
};
bundle.add_resource(&res).unwrap();
}
bundles.push(bundle);
}
from_fn(move || {
locales.next().map(|locale| {
let mut bundle = FluentBundle::new(&[locale]);
let res_path = res_path_scheme.replace("{locale}", locale);

return bundles.into_iter();
for res_id in &res_ids {
let path = res_path.replace("{res_id}", res_id);
let res = res_mgr.get(&path).unwrap_or_else(|| {
let source = read_file(&path).unwrap();
let res = FluentResource::try_new(source).unwrap();
res_mgr.insert(path, Box::new(res))
});
bundle.add_resource(res).unwrap();
}
bundle
})
})
};

// 6. Create a new Localization instance which will be used to maintain the localization
Expand Down
45 changes: 23 additions & 22 deletions fluent-fallback/tests/localization_test.rs
Expand Up @@ -6,6 +6,7 @@ use fluent_fallback::Localization;
use std::cell::RefCell;
use std::fs;
use std::io;
use std::iter::from_fn;

fn read_file(path: &str) -> Result<String, io::Error> {
fs::read_to_string(path)
Expand All @@ -17,30 +18,30 @@ fn localization_format() {

let resource_ids: Vec<String> = vec!["test.ftl".into(), "test2.ftl".into()];
let res_path_scheme = "./tests/resources/{locale}/{res_id}";
let locales = vec!["pl", "en-US"];

let generate_messages = |res_ids: &[String]| {
let locales = vec!["pl", "en-US"];

let mut bundles = vec![];

for locale in locales {
let mut bundle = FluentBundle::new(&[locale]);
let res_path = res_path_scheme.replace("{locale}", locale);
for res_id in res_ids {
let path = res_path.replace("{res_id}", res_id);
let res = if let Some(res) = resources.get(&path) {
res
} else {
let source = read_file(&path).unwrap();
let res = FluentResource::try_new(source).unwrap();
resources.insert(path, Box::new(res))
};
bundle.add_resource(&res).unwrap();
}
bundles.push(bundle);
}

return bundles.into_iter();
let mut locales = locales.iter();
let res_mgr = &resources;
let res_ids = res_ids.to_vec();

from_fn(move || {
locales.next().map(|locale| {
let mut bundle = FluentBundle::new(&[locale]);
let res_path = res_path_scheme.replace("{locale}", locale);

for res_id in &res_ids {
let path = res_path.replace("{res_id}", res_id);
let res = res_mgr.get(&path).unwrap_or_else(|| {
let source = read_file(&path).unwrap();
let res = FluentResource::try_new(source).unwrap();
res_mgr.insert(path, Box::new(res))
});
bundle.add_resource(res).unwrap();
}
bundle
})
})
};

let mut loc = Localization::new(resource_ids, generate_messages);
Expand Down
49 changes: 16 additions & 33 deletions fluent-resmgr/src/resource_manager.rs
Expand Up @@ -2,32 +2,7 @@ use elsa::FrozenMap;
use fluent::{FluentBundle, FluentResource};
use std::fs;
use std::io;

pub struct BundleIterator<'l> {
res_mgr: &'l ResourceManager,
locales: Vec<String>,
locales_ptr: usize,
resource_ids: Vec<String>,
}

impl<'l> Iterator for BundleIterator<'l> {
type Item = FluentBundle<'l>;

fn next(&mut self) -> Option<FluentBundle<'l>> {
if self.locales_ptr >= self.locales.len() {
return None;
}
let locale = &self.locales[self.locales_ptr];

let mut bundle = FluentBundle::new(&[locale]);
for res_id in &self.resource_ids {
let res = self.res_mgr.get_resource(&res_id, locale);
bundle.add_resource(res).unwrap();
}
self.locales_ptr += 1;
Some(bundle)
}
}
use std::iter::from_fn;

fn read_file(path: &str) -> Result<String, io::Error> {
fs::read_to_string(path)
Expand Down Expand Up @@ -76,12 +51,20 @@ impl ResourceManager {
&'l self,
locales: Vec<String>,
resource_ids: Vec<String>,
) -> BundleIterator<'l> {
BundleIterator {
res_mgr: self,
locales,
locales_ptr: 0,
resource_ids,
}
) -> impl Iterator<Item = FluentBundle<'l>> {
let res_mgr = self;
let mut ptr = 0;

from_fn(move || {
locales.get(ptr).map(|locale| {
ptr += 1;
let mut bundle = FluentBundle::new(&[locale]);
for res_id in &resource_ids {
let res = res_mgr.get_resource(&res_id, locale);
bundle.add_resource(res).unwrap();
}
bundle
})
})
}
}

0 comments on commit 934dd77

Please sign in to comment.