From 01989886acec575328184c9c86ec63af80d60695 Mon Sep 17 00:00:00 2001 From: Zibi Braniecki Date: Tue, 13 Oct 2020 09:52:02 -0700 Subject: [PATCH] Use proc-macro-crate to handle icu vs icu_locale --- components/icu/Cargo.toml | 1 + components/icu/src/lib.rs | 11 ++++++++-- components/locale/macros/Cargo.toml | 1 + components/locale/macros/src/lib.rs | 21 ++++++++++++++++++-- components/locale/macros/src/token_stream.rs | 16 ++++++++++----- 5 files changed, 41 insertions(+), 9 deletions(-) diff --git a/components/icu/Cargo.toml b/components/icu/Cargo.toml index 337a45fb181..6af4d41e1cf 100644 --- a/components/icu/Cargo.toml +++ b/components/icu/Cargo.toml @@ -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" } diff --git a/components/icu/src/lib.rs b/components/icu/src/lib.rs index 6bee007d472..34ae8900a09 100644 --- a/components/icu/src/lib.rs +++ b/components/icu/src/lib.rs @@ -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)] diff --git a/components/locale/macros/Cargo.toml b/components/locale/macros/Cargo.toml index e0283218820..ea14449b895 100644 --- a/components/locale/macros/Cargo.toml +++ b/components/locale/macros/Cargo.toml @@ -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" diff --git a/components/locale/macros/src/lib.rs b/components/locale/macros/src/lib.rs index 9717df000aa..686c7aa9c9d 100644 --- a/components/locale/macros/src/lib.rs +++ b/components/locale/macros/src/lib.rs @@ -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('"') { @@ -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() diff --git a/components/locale/macros/src/token_stream.rs b/components/locale/macros/src/token_stream.rs index 6576b878b6c..c02d37e037b 100644 --- a/components/locale/macros/src/token_stream.rs +++ b/components/locale/macros/src/token_stream.rs @@ -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 { @@ -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() ) } @@ -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() ) } @@ -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() ) } @@ -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() ) } @@ -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 ) }