-
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
b69872e
commit 08a41b8
Showing
9 changed files
with
442 additions
and
11 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,109 @@ | ||
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 script_output(input: u32) -> String { | ||
format!("unsafe {{ icu_locale::subtags::Script::from_raw_unchecked({}) }}", input) | ||
} | ||
|
||
fn region_output(input: u32) -> String { | ||
format!("unsafe {{ icu_locale::subtags::Region::from_raw_unchecked({}) }}", input) | ||
} | ||
|
||
fn variant_output(input: u64) -> String { | ||
format!("unsafe {{ icu_locale::subtags::Variant::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 script(input: TokenStream) -> TokenStream { | ||
let val = get_value_from_token_stream(input); | ||
|
||
let parsed: icu_locale::subtags::Script = val.parse().expect("Malformed Script Subtag"); | ||
|
||
let script = script_output(parsed.into_raw()); | ||
|
||
script.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 variant(input: TokenStream) -> TokenStream { | ||
let val = get_value_from_token_stream(input); | ||
|
||
let parsed: icu_locale::subtags::Variant = val.parse().expect("Malformed Variant Subtag"); | ||
|
||
let variant = variant_output(parsed.into_raw()); | ||
|
||
variant.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 script = if let Some(script) = langid.script { | ||
format!("Some({})", script_output(script.into_raw())) | ||
} else { | ||
"None".to_string() | ||
}; | ||
let region = if let Some(region) = langid.region { | ||
format!("Some({})", region_output(region.into_raw())) | ||
} else { | ||
"None".to_string() | ||
}; | ||
let variants = format!("icu_locale::subtags::Variants::from_vec_unchecked(vec![])"); | ||
|
||
let output = format!(r#" | ||
icu_locale::LanguageIdentifier {{ | ||
language: {}, | ||
script: {}, | ||
region: {}, | ||
variants: {}, | ||
}} | ||
"#, lang, script, region, variants); | ||
|
||
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,54 @@ | ||
use icu_locale_macros::*; | ||
use icu_locale::subtags; | ||
use icu_locale::LanguageIdentifier; | ||
|
||
const LANG_PL: subtags::Language = language!("pL"); | ||
const SCRIPT_LATN: subtags::Script = script!("lAtN"); | ||
const REGION_US: subtags::Region = region!("us"); | ||
const VARIANT_MACOS: subtags::Variant = variant!("MACOS"); | ||
const LANGID: LanguageIdentifier = langid!("de-Arab-AT"); | ||
|
||
#[test] | ||
fn language() { | ||
let lang = language!("Pl"); | ||
|
||
assert_eq!(lang, "pl"); | ||
assert_eq!(LANG_PL, "pl"); | ||
assert_eq!(lang, LANG_PL); | ||
} | ||
|
||
#[test] | ||
fn script() { | ||
let script = script!("latn"); | ||
|
||
assert_eq!(script, "Latn"); | ||
assert_eq!(SCRIPT_LATN, "Latn"); | ||
assert_eq!(script, SCRIPT_LATN); | ||
} | ||
|
||
#[test] | ||
fn region() { | ||
let region = region!("us"); | ||
|
||
assert_eq!(region, "US"); | ||
assert_eq!(REGION_US, "US"); | ||
assert_eq!(region, REGION_US); | ||
} | ||
|
||
#[test] | ||
fn variant() { | ||
let variant = variant!("macOS"); | ||
|
||
assert_eq!(variant, "macos"); | ||
assert_eq!(VARIANT_MACOS, "macos"); | ||
assert_eq!(variant, VARIANT_MACOS); | ||
} | ||
|
||
#[test] | ||
fn langid() { | ||
let langid = langid!("de_Arab_aT"); | ||
|
||
assert_eq!(langid.to_string(), "de-Arab-AT"); | ||
assert_eq!(LANGID.to_string(), "de-Arab-AT"); | ||
assert_eq!(langid, LANGID); | ||
} |
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
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
Oops, something went wrong.