mf1-macros 0.1.8

Macros for the mf1 crate
Documentation
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
use convert_case::Case::{Pascal, Snake};
use convert_case::Casing;
use mf1_parser::{parse, ArgType, LexerSpan, Token as AstToken, TokenSlice};
use proc_macro2::Ident;
use proc_macro2::{Span, TokenStream};
use quote::quote;
use serde::{Deserialize, Serialize};
use std::{borrow::Cow, collections::HashMap, fs::File, path::PathBuf};
use std::{io, iter};
use thiserror::Error;
use toml::Value;
#[derive(Debug, Error)]
pub enum Error {
    #[error("Error, can't access env variable \"CARGO_MANIFEST_DIR\": {0}")]
    CargoDirEnvNotPresent(std::env::VarError),
    #[error("Error accessing Cargo.toml: {0}")]
    ManifestNotFound(std::io::Error),
    #[error("Error parsing Cargo.toml: {0}")]
    ConfigFileDeser(toml::de::Error),
    #[error("No locales found")]
    NoDefaultLocale,
    #[error("No locale file: {0}")]
    NoLocaleFile(std::io::Error),
    #[error("Parsing of file {path:?} failed: {err}")]
    LocaleFileDeser {
        path: PathBuf,
        err: serde_json::Error,
    },
    #[error("Parsing of key {key} in {locale} failed: {message} ({span:?})")]
    ParseKeyErr {
        locale: String,
        key: String,
        src: String,
        message: String,
        span: LexerSpan,
    },
    #[error("Unknown error")]
    Misc,
}

impl From<Error> for proc_macro::TokenStream {
    fn from(value: Error) -> Self {
        let error = value.to_string();
        quote!(compile_error!(#error);).into()
    }
}

#[derive(Debug, Serialize, Deserialize, Default)]
pub struct ConfigFile {
    pub locales_dir: Option<String>,
    pub base_locale: Option<String>,
    pub locales: Vec<String>,
}

#[derive(Debug, Clone, PartialEq)]
pub struct StringSet<'a> {
    pub name: &'a str,
    pub keys: HashMap<Cow<'a, str>, StringItem<'a>>,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(untagged)]
pub enum StringItem<'a> {
    String(Cow<'a, str>),
    Subkey(HashMap<Cow<'a, str>, StringItem<'a>>),
}

impl<'a> StringSet<'a> {
    pub fn from_file(name: &'a str, locale_file: File) -> Result<Self, serde_json::Error> {
        let reader = io::BufReader::new(locale_file);
        let mut deser = serde_json::Deserializer::from_reader(reader);
        let keys = HashMap::<Cow<'a, str>, StringItem<'a>>::deserialize(&mut deser)?;
        Ok(Self { name, keys })
    }
    pub fn ident(&self) -> Ident {
        quote::format_ident!("{}", self.name.to_case(Snake))
    }
}

pub fn load_locales() -> Result<TokenStream, Error> {
    let cargo_manifest_dir: PathBuf = std::env::var("CARGO_MANIFEST_DIR")
        .map_err(Error::CargoDirEnvNotPresent)?
        .into();

    let cargo_manifest = std::fs::read_to_string(cargo_manifest_dir.join("Cargo.toml"))
        .map_err(Error::ManifestNotFound)?
        .parse::<toml::Table>()
        .map_err(Error::ConfigFileDeser)?;

    let meta: ConfigFile = cargo_manifest
        .get("package")
        .and_then(|p| p.get("metadata"))
        .and_then(|p| p.get("mf1"))
        .and_then(|p| Value::try_into(p.clone()).ok())
        .unwrap_or_default();

    let mut manifest_dir_path: PathBuf = meta
        .locales_dir
        .clone()
        .map(|d| d.into())
        .unwrap_or_else(|| cargo_manifest_dir.join("locales"));

    let mut locales = Vec::with_capacity(meta.locales.len());
    for locale in meta.locales.iter() {
        manifest_dir_path.push(locale);
        manifest_dir_path.set_extension("json");
        let locale_file = std::fs::File::open(&manifest_dir_path).map_err(Error::NoLocaleFile)?;
        let locale =
            StringSet::from_file(locale, locale_file).map_err(|err| Error::LocaleFileDeser {
                path: manifest_dir_path.clone(),
                err,
            })?;
        locales.push(locale);
        manifest_dir_path.pop();
    }

    let i18n_keys_ident = quote::format_ident!("Mf1Keys");

    let default_locale = meta
        .base_locale
        .as_ref()
        .or_else(|| meta.locales.first())
        .ok_or(Error::NoDefaultLocale)?;

    let base_locale_strings = locales
        .iter()
        .find(|l| l.name == default_locale)
        .ok_or(Error::NoDefaultLocale)?;
    let base_locale_ident = base_locale_strings.ident();

    let locale_idents: Vec<_> = locales.iter().map(StringSet::ident).collect();

    let get_strings_match_arms = locale_idents
        .iter()
        .map(|locale| quote!(Locale::#locale => &#locale));

    let as_str_match_arms = locale_idents
        .iter()
        .zip(locales.iter())
        .map(|(key, l)| (key, l.name))
        .map(|(variant, locale)| quote!(Locale::#variant => #locale));

    let from_str_match_arms = locale_idents
        .iter()
        .zip(locales.iter())
        .map(|(key, l)| (key, l.name))
        .map(|(variant, locale)| quote!(#locale => Ok(Locale::#variant)));

    let locale_list_items = locale_idents.iter().map(|variant| quote!(Locale::#variant));
    let locale_count = locale_idents.len();

    let locales_enum = quote! {
        #[derive(Copy, Clone, Debug, Hash, PartialEq, Eq)]
        #[allow(non_camel_case_types)]
        pub enum Locale {
            #(#locale_idents,)*
        }

        impl Locale {
            const VALUES: [Self; #locale_count] = [#(#locale_list_items,)*];

            fn get_strings(self) -> &'static #i18n_keys_ident {
                match self {
                    #(#get_strings_match_arms,)*
                }
            }

            fn as_str(self) -> &'static str {
                match self {
                    #(#as_str_match_arms,)*
                }
            }
        }

        impl std::str::FromStr for Locale {
            type Err = ();

            fn from_str(s: &str) -> Result<Self, Self::Err> {
                match s.trim() {
                    #(#from_str_match_arms,)*
                    _ => Err(())
                }
            }
        }

        impl Default for Locale {
            fn default() -> Self {
                Locale::#base_locale_ident
            }
        }
    };
    let keys_tokens = generate_keys(
        locales.iter().map(|k| (k.name, k)).collect(),
        base_locale_strings,
        i18n_keys_ident,
    );
    Ok(quote! {
        #locales_enum
        #keys_tokens
    })
}

fn generate_keys(
    locales: HashMap<&str, &StringSet>,
    base_locale_strings: &StringSet,
    i18n_keys_ident: Ident,
) -> TokenStream {
    let base_locale_ident = base_locale_strings.ident();
    let locale_subkeys: HashMap<_, _> = locales
        .iter()
        .map(|(name, string_keys)| {
            let mut keys = HashMap::new();
            string_keys
                .keys
                .iter()
                .filter_map(|(k, v)| match v {
                    StringItem::Subkey(v) => Some((k, v)),
                    _ => None,
                })
                .for_each(|(k, v)| match base_locale_strings.keys.get(k) {
                    Some(StringItem::Subkey(_)) => {
                        keys.insert(k.clone(), v);
                    }
                    Some(_) => eprintln!(
                        "Default locale has incompatible non-subkey key {:?} from locale {}!",
                        k, name
                    ),
                    None => eprintln!(
                        "Default locale is missing key {:?} from locale {}!",
                        k, name
                    ),
                });
            (name, keys)
        })
        .collect();
    let locale_subkeys = base_locale_strings
        .keys
        .iter()
        .filter_map(|(k, v)| match v {
            StringItem::Subkey(v) => Some((k, v)),
            _ => None,
        })
        .map(|(k, v)| {
            let locales: HashMap<&str, StringSet> = locale_subkeys
                .iter()
                .map(|(l, m)| {
                    (
                        **l,
                        StringSet {
                            name: l,
                            keys: m.get(k).map(|s| (*s).clone()).unwrap_or_else(HashMap::new),
                        },
                    )
                })
                .collect();
            (
                k,
                generate_keys(
                    locales.iter().map(|(k, v)| (*k, v)).collect(),
                    &StringSet {
                        name: base_locale_strings.name,
                        keys: v.clone(),
                    },
                    quote::format_ident!("{}", k.to_case(Pascal)),
                ),
            )
        })
        .map(|(k, v)| {
            let k = quote::format_ident!("{}", k.to_case(Snake));
            quote! {
                pub mod #k {
                    #v
                }
            }
        });

    let locale_ast: HashMap<_, _> = locales
        .iter()
        .map(|(name, l)| {
            let mut keys = HashMap::new();
            l.keys
                .iter()
                .filter_map(|(k, v)| match v {
                    StringItem::String(v) => Some((k, v)),
                    StringItem::Subkey(_) => None,
                })
                .for_each(|(k, v)| {
                    keys.insert(
                        k.clone(),
                        parse::<String>(v).map_err(|(message, span)| Error::ParseKeyErr {
                            locale: l.name.to_string(),
                            key: k.to_string(),
                            src: v.to_string(),
                            message,
                            span,
                        }),
                    );
                });
            (name, keys)
        })
        .collect();

    let string_keys = base_locale_strings
        .keys
        .iter()
        .filter(|(k, v)| {
            match v{
                StringItem::String(_) => (),
                StringItem::Subkey(_) => return false,
            }
            locale_ast.iter().all(|(_l, v)| match v.get(*k) {
                Some(s) => matches!(s, Ok(r) if r.iter().all(|t| matches!(t, AstToken::Content { value: _ }))),
                None => true,
            })
        })
        .map(|(k, _)| k)
        .collect::<Vec<_>>();

    let mut dyn_keys = HashMap::new();
    for (locale, asts) in locale_ast.iter() {
        for (k, ast) in asts.iter().filter(|(k, _)| !string_keys.contains(k)) {
            if base_locale_strings.keys.contains_key(k) {
                if let Ok(ast) = ast {
                    dyn_keys
                        .entry(k)
                        .and_modify(|args| ast.get_args_into(args))
                        .or_insert_with(|| ast.get_args());
                }
            } else {
                // Default locale is missing this key!
                eprintln!(
                    "Default locale is missing key {:?} from locale {}!",
                    k, locale
                )
            }
        }
    }
    let dyn_keys = dyn_keys
        .into_iter()
        .map(|(k, a)| {
            (
                k,
                a.into_iter()
                    .map(|(a, v)| {
                        if v.len() > 1 {
                            eprintln!(
                    "Argument {a:?} from key {k} is used in multiple ways! Picking first type",
                )
                        }
                        let arg_type = *v.iter().next().expect("arguments should have a type");
                        (a, arg_type)
                    })
                    .collect::<HashMap<_, _>>(),
            )
        })
        .collect::<HashMap<_, _>>();

    let builder_defs: Vec<TokenStream> = dyn_keys
        .iter()
        .map(|(key, args)| {
            let ident = Ident::new(key, Span::call_site());
            let type_params = args
              .keys()
              .map(|arg| Ident::new(&format!("__{}", arg), Span::call_site()));
            let concrete_types: Vec<_> = args
              .values()
              .map(|arg_type| match arg_type {
                  ArgType::OrdinalArg => quote! {i32},
                  ArgType::PlainArg | ArgType::SelectArg => quote! {&str},
                  ArgType::FunctionArg => todo!()
              }).collect();

            let field_names: Vec<_> = args.keys().map(|arg| {
                Ident::new(&format!("arg_{}", arg), Span::call_site())
            }).collect();
            let fields = args.keys().map(|arg| {
                let key = Ident::new(&format!("arg_{}", arg), Span::call_site());
                let type_param = Ident::new(&format!("__{}", arg), Span::call_site());
                quote!(#key: #type_param)
            });
            let default_type_params = args.iter().map(|_| quote!(EmptyValue));
            let default_fields = field_names.iter().map(|key| {
                quote!(#key: EmptyValue)
            });
            let formatter_args = field_names.iter().map(|key| {
                quote!(self.#key)
            });
            let formatter_type = quote!(&'a for<'x, 'y> fn(&'x mut dyn mf1::Formattable<'y>, #(#concrete_types,)*) -> Result<(), Box<dyn std::error::Error>>);
            fn gen_setter(ident: &syn::Ident, field: &syn::Ident, left_fields: &[Ident], right_fields: &[Ident]) -> proc_macro2::TokenStream {
                let restructure_others = left_fields.iter().chain(right_fields.iter());
                let other_fields = restructure_others.clone();

                let left_type_params: Vec<_> = left_fields.iter().map(|arg| Ident::new(&format!("__{}", arg), Span::call_site())).collect();
                let right_type_params: Vec<_> = right_fields.iter().map(|arg| Ident::new(&format!("__{}", arg), Span::call_site())).collect();

                let all_type_params = left_fields.iter().chain(right_fields.iter()).map(|arg| Ident::new(&format!("__{}", arg), Span::call_site()));

                quote! {

                    impl<'a, #(#all_type_params,)*> #ident<'a, #(#left_type_params,)*EmptyValue,#(#right_type_params,)*> {
                        pub fn #field(self, #field: &str) -> #ident<'a,#(#left_type_params,)*&str,#(#right_type_params,)*> {
                            let #ident { formatter, #(#other_fields,)* .. } = self;
                            #ident { formatter, #field, #(#restructure_others,)*  }
                        }
                    }
                }
            }
    fn split_at<T>(slice: &[T], i: usize) -> (&[T], &T, &[T]) {
        let (left, rest) = slice.split_at(i);
        let (mid, right) = rest.split_first().unwrap();
        (left, mid, right)
    }
            let setters = (0..field_names.len())
            .map(|i| split_at(&field_names, i))
            .map(|(left_fields, field, right_fields)| {
                gen_setter(&ident, field, left_fields, right_fields)
            });
            quote! {
                #[allow(non_camel_case_types, non_snake_case)]
                // Function pointers are compared in the automatically derived PartialEq, Eq, and Hash implementations.
                // While comparing function pointers is generally unreliable (identical functions can have different addresses
                // across compilation units), it's acceptable here as we only care about the *exact* same function.
                #[allow(unpredictable_function_pointer_comparisons)]
                #[derive(Copy, Clone, Debug, Hash, PartialEq, Eq)]
                // #[derive(Clone, Copy, Hash, PartialEq, Eq, Debug)]
                pub struct #ident<'a, #(#type_params,)*> {
                    formatter: #formatter_type,
                    #(#fields,)*
                }

                impl<'a> #ident<'a, #(#default_type_params,)*> {
                    pub const fn new(formatter: #formatter_type) -> Self {
                        Self {
                            formatter,
                            #(#default_fields,)*
                        }
                    }
                }
                #(#setters)*
                impl<'a> mf1::BuildStr for #ident<'a, #(#concrete_types,)*> {
                    #[inline]
                    fn build_string(self) -> std::borrow::Cow<'static, str> {
                        std::borrow::Cow::Owned(format!("{}", self))
                    }
                }
                impl<'a> std::fmt::Display for #ident<'a, #(#concrete_types,)*> {
                    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
                        match (self.formatter)(f, #(#formatter_args,)*) {
                            Ok(_) => Ok(()),
                            Err(e) => Err(*e.downcast().unwrap()),
                        }
                    }
                }

            }
        })
        .collect::<Vec<_>>();

    let subkey_field_defs = base_locale_strings
        .keys
        .iter()
        .filter_map(|(k, v)| match v {
            StringItem::Subkey(_) => {
                let mod_name: Ident = quote::format_ident!("{}", k.to_case(Snake));
                let type_name = quote::format_ident!("{}", k.to_case(Pascal));
                let k = quote::format_ident!("{}", k);
                Some(quote!(pub #k: subkeys::#mod_name::#type_name))
            }
            _ => None,
        });
    let dyn_field_defs = dyn_keys.iter().map(|(key, args)| {
        let key: Ident = quote::format_ident!("{}", key);
        let type_params = args.iter().map(|_| quote!(builders::EmptyValue));
        quote!(pub #key: builders::#key<'static, #(#type_params,)*>)
    });

    let string_field_defs = string_keys
        .iter()
        .map(|key| Ident::new(key, Span::call_site()))
        .map(|key| quote!(pub #key: &'static str));

    let keys_type = quote! {
        #[doc(hidden)]
        pub mod builders {
            #[derive(Debug, Clone, Copy, Hash, PartialEq, Eq)]
            pub struct EmptyValue;
            #(#builder_defs)*
        }

        #[derive(Copy, Clone, Debug, Hash, PartialEq, Eq)]
        #[allow(non_camel_case_types, non_snake_case)]
        pub struct #i18n_keys_ident {
            #(#string_field_defs,)*
            #(#dyn_field_defs,)*
            #(#subkey_field_defs,)*
        }
    };

    let locale_values = locales.iter().map(|locale| {
        let ident = locale.1.ident();
        let subkey_fields = base_locale_strings
        .keys
        .iter()
        .filter_map(|(k, v)| match v {
            StringItem::Subkey(_) => {
                let mod_name: Ident = quote::format_ident!("{}", k.to_case(Snake));
                let k = quote::format_ident!("{}", k);
            Some(quote!(#k: subkeys::#mod_name::#ident))
            },
            _ => None,
        });
        let string_fields = string_keys.iter().map(|key| {
            let key_ident = Ident::new(key, Span::call_site());
            match locale.1.keys.get(*key) {
                Some(StringItem::String(value)) => quote!(#key_ident: #value),
                Some(StringItem::Subkey(_)) => unreachable!(),
                _ => {
                    quote!(#key_ident: #base_locale_ident.#key_ident)
                }
            }
        });
        let formatter_fields = dyn_keys.iter().map(|(key, arg_types)| {
            let key_ident = Ident::new(key, Span::call_site());
            match locale_ast.get(locale.0).unwrap().get(*key) {
                Some(Ok(ast)) => {
                    let args = arg_types
                        .iter()
                        .map(|(name, arg_type)| {
                            let name = Ident::new(name, Span::call_site());
                            match arg_type {
                                ArgType::OrdinalArg => quote! {#name: i32},
                                ArgType::PlainArg | ArgType::SelectArg => quote! {#name: &str},
                                ArgType::FunctionArg => todo!()
                            }
                        });
                    fn gen_items(token: &AstToken<String>) -> impl Iterator<Item = TokenStream> {
                        match token {
                            AstToken::Content { value } => iter::once(quote! {fmt.write_str(#value)?;}),
                            AstToken::PlainArg { arg } => {
                                let arg = Ident::new(arg, Span::call_site());
                                iter::once(quote! {fmt.write_str(#arg)?;})
                            },
                            AstToken::Octothorpe {  } => iter::once(quote! {fmt.write_str("#")?;}),
                            select @ AstToken::Select { arg, cases, plural_offset: _ } => {
                                let case_statements = cases.iter().filter(|case| case.key != "other").map(|case| {
                                    let key = &case.key;
                                    let items = case.tokens.iter().flat_map(gen_items);
                                    quote! { #key => { #(#items)* }}
                                });
                                let other = if let Some(case) = cases.iter().find(|case| case.key == "other") {
                                    let items = case.tokens.iter().flat_map(gen_items);
                                    quote! { _ => { #(#items)* }
                                }} else {
                                    eprintln!("Missing other case in select {select:?}");
                                    quote! {}
                                };
                                let arg = Ident::new(arg, Span::call_site());
                                iter::once(quote! {
                                    match #arg {
                                        #(#case_statements,)*
                                        #other
                                    }
                                })
                            }
                            _ => todo!(),
                        }
                    }
                    let items = ast.iter().flat_map(gen_items);
                    quote!(#key_ident: builders::#key_ident::new(&(|fmt: &mut dyn mf1::Formattable, #(#args,)*| -> Result<(), _> {
                        #(#items)*
                        Ok(())
                    } as _)))
                },
                _ => {
                    quote!(#key_ident: #base_locale_ident.#key_ident)
                }
            }
        });


        quote! {
            #[allow(non_upper_case_globals)]
            pub(crate) const #ident: #i18n_keys_ident = #i18n_keys_ident {
                #(#string_fields,)*
                #(#formatter_fields,)*
                #(#subkey_fields,)*
            };
        }
    });

    let locale_static = quote! {
        #(
            #locale_values
        )*
    };
    quote! {
        #[doc(hidden)]
        pub mod subkeys {
            #(#locale_subkeys)*
        }
        #keys_type
        #locale_static
    }
}