summaryrefslogtreecommitdiffstats
path: root/vendor/icu_provider_macros/src/lib.rs
blob: 8d39f0f8747ad388c8a23bc7a05e005e680587a9 (plain)
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
// 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 ).

// https://github.com/unicode-org/icu4x/blob/main/docs/process/boilerplate.md#library-annotations
#![cfg_attr(
    not(test),
    deny(
        clippy::indexing_slicing,
        clippy::unwrap_used,
        clippy::expect_used,
        // Panics are OK in proc macros
        // clippy::panic,
        clippy::exhaustive_structs,
        clippy::exhaustive_enums,
        missing_debug_implementations,
    )
)]
#![warn(missing_docs)]

//! Proc macros for the ICU4X data provider.
//!
//! These macros are re-exported from `icu_provider`.

extern crate proc_macro;
use proc_macro::TokenStream;
use proc_macro2::TokenStream as TokenStream2;
use quote::quote;
use syn::parse_macro_input;
use syn::spanned::Spanned;
use syn::AttributeArgs;
use syn::DeriveInput;
use syn::Lit;
use syn::Meta;
use syn::NestedMeta;

#[cfg(test)]
mod tests;

#[proc_macro_attribute]
/// The `#[data_struct]` attribute should be applied to all types intended
/// for use in a `DataStruct`.
///
/// It does the following things:
///
/// - `Apply #[derive(Yokeable, ZeroFrom)]`. The `ZeroFrom` derive can
///    be customized with `#[zerofrom(clone)]` on non-ZeroFrom fields.
///
/// In addition, the attribute can be used to implement `DataMarker` and/or `KeyedDataMarker`
/// by adding symbols with optional key strings:
///
/// ```
/// use icu_provider::prelude::*;
/// use std::borrow::Cow;
///
/// #[icu_provider::data_struct(
///     FooV1Marker,
///     BarV1Marker = "demo/bar@1",
///     marker(
///         BazV1Marker,
///         "demo/baz@1",
///         fallback_by = "region",
///         extension_key = "ca"
///     )
/// )]
/// pub struct FooV1<'data> {
///     message: Cow<'data, str>,
/// };
///
/// // Note: FooV1Marker implements `DataMarker` but not `KeyedDataMarker`.
/// // The other two implement `KeyedDataMarker`.
///
/// assert_eq!(&*BarV1Marker::KEY.path(), "demo/bar@1");
/// assert_eq!(
///     BarV1Marker::KEY.metadata().fallback_priority,
///     icu_provider::FallbackPriority::Language
/// );
/// assert_eq!(BarV1Marker::KEY.metadata().extension_key, None);
///
/// assert_eq!(&*BazV1Marker::KEY.path(), "demo/baz@1");
/// assert_eq!(
///     BazV1Marker::KEY.metadata().fallback_priority,
///     icu_provider::FallbackPriority::Region
/// );
/// assert_eq!(
///     BazV1Marker::KEY.metadata().extension_key,
///     Some(icu::locid::extensions_unicode_key!("ca"))
/// );
/// ```
///
/// If the `#[databake(path = ...)]` attribute is present on the data struct, this will also
/// implement it on the markers.
pub fn data_struct(attr: TokenStream, item: TokenStream) -> TokenStream {
    TokenStream::from(data_struct_impl(
        parse_macro_input!(attr as AttributeArgs),
        parse_macro_input!(item as DeriveInput),
    ))
}

fn data_struct_impl(attr: AttributeArgs, input: DeriveInput) -> TokenStream2 {
    if input.generics.type_params().count() > 0 {
        return syn::Error::new(
            input.generics.span(),
            "#[data_struct] does not support type parameters",
        )
        .to_compile_error();
    }
    let lifetimes = input.generics.lifetimes().collect::<Vec<_>>();

    let name = &input.ident;

    let name_with_lt = if lifetimes.get(0).is_some() {
        quote!(#name<'static>)
    } else {
        quote!(#name)
    };

    if lifetimes.len() > 1 {
        return syn::Error::new(
            input.generics.span(),
            "#[data_struct] does not support more than one lifetime parameter",
        )
        .to_compile_error();
    }

    let bake_derive = input
        .attrs
        .iter()
        .find(|a| a.path.is_ident("databake"))
        .map(|a| {
            quote! {
                #[derive(databake::Bake)]
                #a
            }
        })
        .unwrap_or_else(|| quote! {});

    let mut result = TokenStream2::new();

    for single_attr in attr.into_iter() {
        let mut marker_name: Option<syn::Path> = None;
        let mut key_lit: Option<syn::LitStr> = None;
        let mut fallback_by: Option<syn::LitStr> = None;
        let mut extension_key: Option<syn::LitStr> = None;
        let mut fallback_supplement: Option<syn::LitStr> = None;

        match single_attr {
            NestedMeta::Meta(Meta::List(meta_list)) => {
                match meta_list.path.get_ident() {
                    Some(ident) if ident.to_string().as_str() == "marker" => (),
                    _ => panic!("Meta list must be `marker(...)`"),
                }
                for inner_meta in meta_list.nested.into_iter() {
                    match inner_meta {
                        NestedMeta::Meta(Meta::Path(path)) => {
                            marker_name = Some(path);
                        }
                        NestedMeta::Lit(Lit::Str(lit_str)) => {
                            key_lit = Some(lit_str);
                        }
                        NestedMeta::Meta(Meta::NameValue(name_value)) => {
                            let lit_str = match name_value.lit {
                                Lit::Str(lit_str) => lit_str,
                                _ => panic!("Values in marker() must be strings"),
                            };
                            let name_ident_str = match name_value.path.get_ident() {
                                Some(ident) => ident.to_string(),
                                None => panic!("Names in marker() must be identifiers"),
                            };
                            match name_ident_str.as_str() {
                                "fallback_by" => fallback_by = Some(lit_str),
                                "extension_key" => extension_key = Some(lit_str),
                                "fallback_supplement" => fallback_supplement = Some(lit_str),
                                _ => panic!("Invalid argument name in marker()"),
                            }
                        }
                        _ => panic!("Invalid argument in marker()"),
                    }
                }
            }
            NestedMeta::Meta(Meta::NameValue(name_value)) => {
                marker_name = Some(name_value.path);
                match name_value.lit {
                    syn::Lit::Str(lit_str) => key_lit = Some(lit_str),
                    _ => panic!("Key must be a string"),
                };
            }
            NestedMeta::Meta(Meta::Path(path)) => {
                marker_name = Some(path);
            }
            _ => {
                panic!("Invalid attribute to #[data_struct]")
            }
        }

        let marker_name = match marker_name {
            Some(path) => path,
            None => panic!("#[data_struct] arguments must include a marker name"),
        };

        let docs = if let Some(key_lit) = &key_lit {
            let fallback_by_docs_str = match &fallback_by {
                Some(fallback_by) => fallback_by.value(),
                None => "language (default)".to_string(),
            };
            let extension_key_docs_str = match &extension_key {
                Some(extension_key) => extension_key.value(),
                None => "none (default)".to_string(),
            };
            format!("Marker type for [`{}`]: \"{}\"\n\n- Fallback priority: {}\n- Extension keyword: {}", name, key_lit.value(), fallback_by_docs_str, extension_key_docs_str)
        } else {
            format!("Marker type for [`{}`]", name)
        };

        result.extend(quote!(
            #[doc = #docs]
            #bake_derive
            pub struct #marker_name;
            impl icu_provider::DataMarker for #marker_name {
                type Yokeable = #name_with_lt;
            }
        ));

        if let Some(key_lit) = &key_lit {
            let key_str = key_lit.value();
            let fallback_by_expr = if let Some(fallback_by_lit) = fallback_by {
                match fallback_by_lit.value().as_str() {
                    "region" => quote! {icu_provider::FallbackPriority::Region},
                    "collation" => quote! {icu_provider::FallbackPriority::Collation},
                    "language" => quote! {icu_provider::FallbackPriority::Language},
                    _ => panic!("Invalid value for fallback_by"),
                }
            } else {
                quote! {icu_provider::FallbackPriority::const_default()}
            };
            let extension_key_expr = if let Some(extension_key_lit) = extension_key {
                quote! {Some(icu_provider::_internal::extensions_unicode_key!(#extension_key_lit))}
            } else {
                quote! {None}
            };
            let fallback_supplement_expr =
                if let Some(fallback_supplement_lit) = fallback_supplement {
                    match fallback_supplement_lit.value().as_str() {
                        "collation" => quote! {Some(icu_provider::FallbackSupplement::Collation)},
                        _ => panic!("Invalid value for fallback_supplement"),
                    }
                } else {
                    quote! {None}
                };
            result.extend(quote!(
                impl icu_provider::KeyedDataMarker for #marker_name {
                    const KEY: icu_provider::DataKey = icu_provider::data_key!(#key_str, icu_provider::DataKeyMetadata::construct_internal(
                        #fallback_by_expr,
                        #extension_key_expr,
                        #fallback_supplement_expr
                    ));
                }
            ));
        }
    }

    result.extend(quote!(
        #[derive(yoke::Yokeable, zerofrom::ZeroFrom)]
        #input
    ));

    result
}