-
Notifications
You must be signed in to change notification settings - Fork 184
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3ebc97d
commit f2eb526
Showing
7 changed files
with
157 additions
and
0 deletions.
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
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,21 @@ | ||
[package] | ||
name = "icu-locale-macros" | ||
description = "proc-macros for icu-locale" | ||
version = "0.0.1" | ||
authors = ["The ICU4X Project Developers"] | ||
edition = "2018" | ||
readme = "README.md" | ||
repository = "https://github.com/unicode-org/icu4x" | ||
license-file = "../../LICENSE" | ||
categories = ["internationalization"] | ||
include = [ | ||
"src/**/*", | ||
"Cargo.toml", | ||
"README.md" | ||
] | ||
|
||
[lib] | ||
proc_macro = true | ||
|
||
[dependencies] | ||
icu-locale = { version = "0.0.1", path = "../" } |
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,71 @@ | ||
extern crate proc_macro; | ||
|
||
use proc_macro::TokenStream; | ||
|
||
fn get_value_from_token_stream(input: TokenStream) -> String { | ||
let val = format!("{}", input); | ||
if !val.starts_with('"') || !val.ends_with('"') { | ||
panic!("Argument must be a string literal."); | ||
} | ||
let len = val.len(); | ||
(&val[1..len - 1]).to_string() | ||
} | ||
|
||
fn language_output(input: Option<u64>) -> String { | ||
let val = if let Some(raw) = input { | ||
format!("Some({})", raw) | ||
} else { | ||
"None".to_string() | ||
}; | ||
format!("unsafe {{ icu_locale::subtags::Language::from_raw_unchecked({}) }}", val) | ||
} | ||
|
||
fn region_output(input: u32) -> String { | ||
format!("unsafe {{ icu_locale::subtags::Region::from_raw_unchecked({}) }}", input) | ||
} | ||
|
||
#[proc_macro] | ||
pub fn language(input: TokenStream) -> TokenStream { | ||
let val = get_value_from_token_stream(input); | ||
|
||
let parsed: icu_locale::subtags::Language = val.parse().expect("Malformed Language Subtag"); | ||
|
||
let lang = language_output(parsed.into_raw()); | ||
|
||
lang.parse().unwrap() | ||
} | ||
|
||
#[proc_macro] | ||
pub fn region(input: TokenStream) -> TokenStream { | ||
let val = get_value_from_token_stream(input); | ||
|
||
let parsed: icu_locale::subtags::Region = val.parse().expect("Malformed Region Subtag"); | ||
|
||
let region = region_output(parsed.into_raw()); | ||
|
||
region.parse().unwrap() | ||
} | ||
|
||
#[proc_macro] | ||
pub fn langid(input: TokenStream) -> TokenStream { | ||
let val = get_value_from_token_stream(input); | ||
|
||
let langid: icu_locale::LanguageIdentifier = val.parse().expect("Malformed Language Identifier"); | ||
|
||
let lang = language_output(langid.language.into_raw()); | ||
let region = if let Some(region) = langid.region { | ||
format!("Some({})", region_output(region.into_raw())) | ||
} else { | ||
"None".to_string() | ||
}; | ||
|
||
let output = format!(r#" | ||
icu_locale::LanguageIdentifier {{ | ||
language: {}, | ||
region: {}, | ||
..Default::default() | ||
}} | ||
"#, lang, region); | ||
|
||
output.parse().unwrap() | ||
} |
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,25 @@ | ||
use icu_locale_macros::*; | ||
|
||
const LANG_PL: icu_locale::subtags::Language = language!("pL"); | ||
|
||
#[test] | ||
fn language() { | ||
let lang = language!("En"); | ||
|
||
assert_eq!(lang, "en"); | ||
assert_eq!(LANG_PL, "pl"); | ||
} | ||
|
||
#[test] | ||
fn region() { | ||
let region = region!("us"); | ||
|
||
assert_eq!(region, "US"); | ||
} | ||
|
||
#[test] | ||
fn langid() { | ||
let langid = langid!("en_uS"); | ||
|
||
assert_eq!(langid.to_string(), "en-US"); | ||
} |
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
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
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