Skip to content

Commit

Permalink
key convenience fns
Browse files Browse the repository at this point in the history
  • Loading branch information
robertbastian committed Apr 27, 2022
1 parent 391dba0 commit 2c1c5ea
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 27 deletions.
38 changes: 11 additions & 27 deletions provider/datagen/src/bin/datagen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,7 @@ use icu_locid::LanguageIdentifier;
use icu_provider::hello_world::HelloWorldV1Marker;
use icu_provider::prelude::*;
use simple_logger::SimpleLogger;
use std::borrow::Cow;
use std::collections::HashSet;
use std::fs::File;
use std::io;
use std::io::BufRead;
use std::path::PathBuf;
use std::str::FromStr;

Expand Down Expand Up @@ -254,32 +250,20 @@ fn main() -> eyre::Result<()> {
all_keys
} else if matches.is_present("HELLO_WORLD") {
vec![HelloWorldV1Marker::KEY]
} else if let Some(paths) = matches.values_of("KEYS") {
icu_datagen::keys(&paths.collect::<Vec<_>>())
} else if let Some(key_file_path) = matches.value_of_os("KEY_FILE") {
File::open(key_file_path)
.and_then(icu_datagen::keys_from_file)
.with_context(|| key_file_path.to_string_lossy().into_owned())?
} else {
let mut keys = HashSet::new();

if let Some(paths) = matches.values_of("KEYS") {
keys.extend(paths.map(Cow::Borrowed));
} else if let Some(key_file_path) = matches.value_of_os("KEY_FILE") {
let file = File::open(key_file_path)
.with_context(|| key_file_path.to_string_lossy().into_owned())?;
for line in io::BufReader::new(file).lines() {
let path = line.with_context(|| key_file_path.to_string_lossy().into_owned())?;
keys.insert(Cow::Owned(path));
}
}

let filtered: Vec<_> = all_keys
.into_iter()
.filter(|k| keys.contains(k.get_path()))
.collect();

if filtered.is_empty() {
eyre::bail!("No keys selected");
}

filtered
unreachable!();
};

if selected_keys.is_empty() {
eyre::bail!("No keys selected");
}

let mut source_data = SourceData::default();

if matches.is_present("INPUT_FROM_TESTDATA") {
Expand Down
53 changes: 53 additions & 0 deletions provider/datagen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,61 @@ use icu_provider::serde::SerializeMarker;
use icu_provider_adapters::filter::Filterable;
use icu_provider_fs::export::serializers;
use rayon::prelude::*;
use std::collections::HashSet;
use std::io::{Read, BufRead, BufReader};
use std::path::PathBuf;

/// Parses a list of human-readable key identifiers and returns a
/// list of [`ResourceKey`]s. Invalid key names are ignored.
///
/// # Example
/// ```
/// # use icu_provider::ResourceMarker;
/// assert_eq!(
/// icu_datagen::keys(&["list/and@1", "list/or@1"]),
/// vec![
/// icu_list::provider::AndListV1Marker::KEY,
/// icu_list::provider::OrListV1Marker::KEY,
/// ],
/// );
/// ```
pub fn keys<S: AsRef<str>>(strings: &[S]) -> Vec<ResourceKey> {
let keys = strings.iter().map(AsRef::as_ref).collect::<HashSet<&str>>();
get_all_keys()
.into_iter()
.filter(|k| keys.contains(k.get_path()))
.collect()
}

/// Parses a file of human-readable key identifiers and returns a
/// list of [`ResourceKey`]s. Invalid key names are ignored.
///
/// # Example
///
/// #### keys.txt
/// ```text
/// list/and@1
/// list/or@1
/// ```
/// #### build.rs
/// ```no_run
/// # use icu_provider::ResourceMarker;
/// # use std::fs::File;
/// # fn main() -> std::io::Result<()> {
/// assert_eq!(
/// icu_datagen::keys_from_file(File::open("keys.txt")?)?,
/// vec![
/// icu_list::provider::AndListV1Marker::KEY,
/// icu_list::provider::OrListV1Marker::KEY,
/// ],
/// );
/// # Ok(())
/// # }
/// ```
pub fn keys_from_file<R: Read>(file: R) -> std::io::Result<Vec<ResourceKey>> {
Ok(keys(&BufReader::new(file).lines().collect::<std::io::Result<Vec<String>>>()?))
}

/// The output format.
#[non_exhaustive]
pub enum Out {
Expand Down

0 comments on commit 2c1c5ea

Please sign in to comment.