-
Notifications
You must be signed in to change notification settings - Fork 185
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add example for icu_locale #250
Merged
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,3 +34,7 @@ harness = false | |
[[bench]] | ||
name = "locale" | ||
harness = false | ||
|
||
[[example]] | ||
name = "filter_langids" | ||
test = true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// A sample application which takes a comma separated list of language identifiers, | ||
// filters out identifiers with language subtags different than `en` and serializes | ||
// the list back into a comma separated list in canonical syntax. | ||
use std::env; | ||
|
||
use icu_locale::{subtags, LanguageIdentifier}; | ||
|
||
const DEFAULT_INPUT: &str = | ||
"de, en-us, zh-hant, sr-cyrl, fr-ca, es-cl, pl, en-latn-us, ca-valencia, und-arab"; | ||
|
||
fn filter_input(input: &str) -> String { | ||
// 1. Parse the input string into a list of language identifiers. | ||
let langids: Vec<LanguageIdentifier> = input | ||
.split(',') | ||
.filter_map(|s| s.trim().parse().ok()) | ||
.collect(); | ||
|
||
// 2. Filter for LanguageIdentifiers with Language subtag `en`. | ||
let en_lang: subtags::Language = "en".parse().expect("Failed to parse language subtag."); | ||
|
||
let en_langids = langids | ||
.into_iter() | ||
.filter(|langid| langid.language == en_lang); | ||
|
||
// 3. Serialize the output. | ||
let en_strs: Vec<String> = en_langids.map(|langid| langid.to_string()).collect(); | ||
|
||
en_strs.join(", ") | ||
} | ||
|
||
fn main() { | ||
let args: Vec<String> = env::args().collect(); | ||
|
||
let input = if let Some(input) = args.get(1) { | ||
input.as_str() | ||
} else { | ||
DEFAULT_INPUT | ||
}; | ||
let _output = filter_input(&input); | ||
|
||
#[cfg(debug_assertions)] | ||
println!("\nInput: {}\nOutput: {}", input, _output); | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
const DEFAULT_OUTPUT: &str = "en-US, en-Latn-US"; | ||
|
||
#[test] | ||
fn ensure_default_output() { | ||
assert_eq!(filter_input(DEFAULT_INPUT), DEFAULT_OUTPUT); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this a good place to demo the macros?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
once we land them, I'll switch to them in examples :)