1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
// This file is part of ICU4X. For terms of use, please see the file
// called LICENSE at the top level of the ICU4X source tree
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).

//! [`icu_locid_macros`](crate) is one of the ICU4X components.
//!
//! This API provides convenience macros for [`icu_locid`].
//!
//! # Examples
//!
//! ```rust
//! use icu_locid_macros::{language, region, langid};
//!
//! let lid = langid!("EN_US");
//!
//! assert_eq!(lid.language, language!("en"));
//! assert_eq!(lid.region, Some(region!("US")));
//! ```

mod token_stream;

extern crate proc_macro;

use proc_macro::TokenStream;
use proc_macro_crate::{crate_name, FoundCrate};
use token_stream::IntoTokenStream;

fn get_crate_name() -> String {
    match crate_name("icu") {
        Ok(FoundCrate::Itself) => "icu::locid".to_string(),
        Ok(FoundCrate::Name(name)) => format!("{}::locid", name),
        Err(_) => match crate_name("icu_locid") {
            Ok(FoundCrate::Name(name)) => name,
            _ => "icu_locid".to_string(),
        },
    }
}

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()
}

/// A macro allowing for compile-time construction of valid [`Language`] subtag.
///
/// The macro will perform syntax canonicalization of the tag.
///
/// # Examples
///
/// ```
/// use icu::locid::subtags::Language;
/// use icu::locid::macros::language;
///
/// const DE: Language = language!("DE");
///
/// let de: Language = "DE".parse()
///     .expect("Failed to parse language subtag.");
///
/// assert_eq!(DE, "de");
/// assert_eq!(DE, de);
/// ```
///
/// [`Language`]: icu_locid::subtags::Language
#[proc_macro]
pub fn language(input: TokenStream) -> TokenStream {
    let val = get_value_from_token_stream(input);

    let parsed: icu_locid::subtags::Language = val.parse().expect("Malformed Language Subtag");

    parsed
        .into_token_stream()
        .expect("Failed to parse token stream.")
}

/// A macro allowing for compile-time construction of valid [`Script`] subtag.
///
/// The macro will perform syntax canonicalization of the tag.
///
/// # Examples
///
/// ```
/// use icu::locid::subtags::Script;
/// use icu::locid::macros::script;
///
/// const ARAB: Script = script!("aRAB");
///
/// let arab: Script = "aRaB".parse()
///     .expect("Failed to parse script subtag.");
///
/// assert_eq!(ARAB, "Arab");
/// assert_eq!(ARAB, arab);
/// ```
///
/// [`Script`]: icu_locid::subtags::Script
#[proc_macro]
pub fn script(input: TokenStream) -> TokenStream {
    let val = get_value_from_token_stream(input);

    let parsed: icu_locid::subtags::Script = val.parse().expect("Malformed Script Subtag");

    parsed
        .into_token_stream()
        .expect("Failed to parse token stream.")
}

/// A macro allowing for compile-time construction of valid [`Region`] subtag.
///
/// The macro will perform syntax canonicalization of the tag.
///
/// # Examples
///
/// ```
/// use icu::locid::subtags::Region;
/// use icu::locid::macros::region;
///
/// const CN: Region = region!("cn");
///
/// let cn: Region = "cn".parse()
///     .expect("Failed to parse region subtag.");
///
/// assert_eq!(CN, "CN");
/// assert_eq!(CN, cn);
/// ```
///
/// [`Region`]: icu_locid::subtags::Region
#[proc_macro]
pub fn region(input: TokenStream) -> TokenStream {
    let val = get_value_from_token_stream(input);

    let parsed: icu_locid::subtags::Region = val.parse().expect("Malformed Region Subtag");

    parsed
        .into_token_stream()
        .expect("Failed to parse token stream.")
}

/// A macro allowing for compile-time construction of valid [`Variant`] subtag.
///
/// The macro will perform syntax canonicalization of the tag.
///
/// # Examples
///
/// ```
/// use icu::locid::subtags::Variant;
/// use icu::locid::macros::variant;
///
/// const POSIX: Variant = variant!("Posix");
///
/// let posix: Variant = "Posix".parse()
///     .expect("Failed to parse variant subtag.");
///
/// assert_eq!(POSIX, "posix");
/// assert_eq!(POSIX, posix);
/// ```
///
/// [`Variant`]: icu_locid::subtags::Variant
#[proc_macro]
pub fn variant(input: TokenStream) -> TokenStream {
    let val = get_value_from_token_stream(input);

    let parsed: icu_locid::subtags::Variant = val.parse().expect("Malformed Variant Subtag");

    parsed
        .into_token_stream()
        .expect("Failed to parse token stream.")
}

/// A macro allowing for compile-time construction of valid [`LanguageIdentifier`].
///
/// The macro will perform syntax canonicalization of the tag.
///
/// # Examples
///
/// ```
/// use icu::locid::LanguageIdentifier;
/// use icu::locid::macros::langid;
///
/// const DE_AT: LanguageIdentifier = langid!("de_at");
///
/// let de_at: LanguageIdentifier = "de_at".parse()
///     .expect("Failed to parse language identifier.");
///
/// assert_eq!(DE_AT, "de-AT");
/// assert_eq!(DE_AT, de_at);
/// ```
///
/// *Note*: As of Rust 1.47, the macro cannot produce language identifier
/// with variants in the const mode pending [`Heap Allocations in Constants`].
///
/// [`LanguageIdentifier`]: icu_locid::LanguageIdentifier
/// [`Heap Allocations in Constants`]: https://github.com/rust-lang/const-eval/issues/20
#[proc_macro]
pub fn langid(input: TokenStream) -> TokenStream {
    let val = get_value_from_token_stream(input);

    let langid: icu_locid::LanguageIdentifier = val.parse().expect("Malformed Language Identifier");

    let lang = langid.language.into_token_stream_string();

    let script = langid.script.into_token_stream_string();
    let region = langid.region.into_token_stream_string();

    let variants = langid.variants.into_token_stream_string();

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

    output.parse().expect("Output should parse.")
}