Skip to content
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

Use proc-macro-crate to handle icu vs icu_locale #338

Merged
merged 1 commit into from
Oct 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions components/icu/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ include = [
[dependencies]
icu-datetime = { path = "../datetime" }
icu-locale = { path = "../locale" }
icu-locale-macros = { path = "../locale/macros" }
icu-plurals = { path = "../plurals" }
icu-uniset = { path = "../uniset" }

Expand Down
11 changes: 9 additions & 2 deletions components/icu/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,15 @@
//! [`FsDataProvider`]: ../icu_fs_data_provider/struct.FsDataProvider.html
#[doc(inline)]
pub use icu_datetime as datetime;
#[doc(inline)]
pub use icu_locale as locale;

pub mod locale {
pub use icu_locale::*;

pub mod macros {
pub use icu_locale_macros::*;
}
}

#[doc(inline)]
pub use icu_plurals as plurals;
#[doc(inline)]
Expand Down
1 change: 1 addition & 0 deletions components/locale/macros/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,6 @@ include = [
proc_macro = true

[dependencies]
proc-macro-crate = "0.1.5"
icu-locale = { version = "0.0.1", path = "../" }
tinystr = "0.3"
21 changes: 19 additions & 2 deletions components/locale/macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,21 @@ mod token_stream;
extern crate proc_macro;

use proc_macro::TokenStream;
use proc_macro_crate::crate_name;
use token_stream::IntoTokenStream;

fn get_crate_name() -> String {
if let Ok(name) = crate_name("icu") {
format!("{}::locale", name)
} else {
if let Ok(name) = crate_name("icu_locale") {
name.to_string()
} else {
"icu_locale".to_string()
}
}
}

fn get_value_from_token_stream(input: TokenStream) -> String {
let val = format!("{}", input);
if !val.starts_with('"') || !val.ends_with('"') {
Expand Down Expand Up @@ -181,14 +194,18 @@ pub fn langid(input: TokenStream) -> TokenStream {

let output = format!(
r#"
icu_locale::LanguageIdentifier {{
{}::LanguageIdentifier {{
language: {},
script: {},
region: {},
variants: {},
}}
"#,
lang, script, region, variants
get_crate_name(),
lang,
script,
region,
variants
);

output.parse().unwrap()
Expand Down
16 changes: 11 additions & 5 deletions components/locale/macros/src/token_stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use icu_locale::subtags;

extern crate proc_macro;

use super::get_crate_name;
use proc_macro::TokenStream;

pub(crate) trait IntoTokenStream {
Expand Down Expand Up @@ -32,7 +33,8 @@ impl IntoTokenStream for u64 {
impl IntoTokenStream for subtags::Language {
fn into_token_stream_string(self) -> String {
format!(
"unsafe {{ icu_locale::subtags::Language::from_raw_unchecked({}) }}",
"unsafe {{ {}::subtags::Language::from_raw_unchecked({}) }}",
get_crate_name(),
self.into_raw().into_token_stream_string()
)
}
Expand All @@ -41,7 +43,8 @@ impl IntoTokenStream for subtags::Language {
impl IntoTokenStream for subtags::Script {
fn into_token_stream_string(self) -> String {
format!(
"unsafe {{ icu_locale::subtags::Script::from_raw_unchecked({}) }}",
"unsafe {{ {}::subtags::Script::from_raw_unchecked({}) }}",
get_crate_name(),
self.into_raw().into_token_stream_string()
)
}
Expand All @@ -50,7 +53,8 @@ impl IntoTokenStream for subtags::Script {
impl IntoTokenStream for subtags::Region {
fn into_token_stream_string(self) -> String {
format!(
"unsafe {{ icu_locale::subtags::Region::from_raw_unchecked({}) }}",
"unsafe {{ {}::subtags::Region::from_raw_unchecked({}) }}",
get_crate_name(),
self.into_raw().into_token_stream_string()
)
}
Expand All @@ -59,7 +63,8 @@ impl IntoTokenStream for subtags::Region {
impl IntoTokenStream for subtags::Variant {
fn into_token_stream_string(self) -> String {
format!(
"unsafe {{ icu_locale::subtags::Variant::from_raw_unchecked({}) }}",
"unsafe {{ {}::subtags::Variant::from_raw_unchecked({}) }}",
get_crate_name(),
self.into_raw().into_token_stream_string()
)
}
Expand All @@ -77,7 +82,8 @@ impl IntoTokenStream for subtags::Variants {
"None".to_string()
};
format!(
"unsafe {{ icu_locale::subtags::Variants::from_raw_unchecked({}) }}",
"unsafe {{ {}::subtags::Variants::from_raw_unchecked({}) }}",
get_crate_name(),
variants
)
}
Expand Down