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
use std::{
    collections::HashMap,
    path::{Path, PathBuf},
};

use proc_macro2::TokenStream;
use quote::quote;
use syn::{
    braced,
    parse::{Parse, ParseStream},
    parse_macro_input, token, Ident, Result,
};

struct StaticLoader {
    vis: Option<syn::Visibility>,
    name: Ident,
    locales_directory: PathBuf,
    fallback_language: syn::LitStr,
    core_locales: Option<PathBuf>,
    customise: Option<syn::ExprClosure>,
}

impl Parse for StaticLoader {
    fn parse(input: ParseStream) -> Result<Self> {
        let workspace_path = std::path::PathBuf::from(
            std::env::var("CARGO_MANIFEST_DIR").unwrap_or_else(|_| String::from("./")),
        );
        let vis = input.parse::<syn::Visibility>().ok();
        input.parse::<token::Static>()?;
        let name = input.parse::<Ident>()?;
        input.parse::<token::Eq>()?;
        let fields;
        braced!(fields in input);
        let mut core_locales: Option<syn::LitStr> = None;
        let mut customise = None;
        let mut fallback_language = None;
        let mut locales_directory: Option<syn::LitStr> = None;

        while !fields.is_empty() {
            let k = fields.parse::<Ident>()?;
            fields.parse::<syn::Token![:]>()?;

            if k == "customise" {
                customise = Some(fields.parse()?);
            } else if k == "core_locales" {
                core_locales = Some(fields.parse()?);
            } else if k == "fallback_language" {
                fallback_language = Some(fields.parse()?);
            } else if k == "locales" {
                locales_directory = Some(fields.parse()?);
            } else {
                return Err(syn::Error::new(k.span(), "Not a valid parameter"));
            }

            if fields.is_empty() {
                break;
            }
            fields.parse::<token::Comma>()?;
        }
        input.parse::<token::Semi>()?;

        let locales_directory = locales_directory
            .ok_or_else(|| syn::Error::new(name.span(), "Missing `locales` field"))?;

        let locales_directory_path = workspace_path.join(locales_directory.value());

        if std::fs::metadata(&locales_directory_path).is_err() {
            return Err(syn::Error::new(locales_directory.span(), format!("Couldn't read locales directory, this path should be relative to your crate's `Cargo.toml`. Looking for: {:?}", locales_directory_path)));
        }

        let core_locales = if let Some(core_locales) = &core_locales {
            let core_locales_path = workspace_path.join(core_locales.value());
            if std::fs::metadata(&core_locales_path).is_err() {
                return Err(syn::Error::new(core_locales.span(), "Couldn't read core fluent resource, this path should be relative to your crate's `Cargo.toml`."));
            }
            Some(core_locales_path)
        } else {
            None
        };

        let fallback_language = fallback_language
            .ok_or_else(|| syn::Error::new(name.span(), "Missing `fallback_language` field"))?;

        Ok(Self {
            vis,
            name,
            locales_directory: locales_directory_path,
            fallback_language,
            core_locales,
            customise,
        })
    }
}

/// Copied from `fluent_templates::loader` to avoid needing a seperate crate to
/// share the function.
fn build_resources(dir: impl AsRef<std::path::Path>) -> HashMap<String, Vec<String>> {
    let mut all_resources = HashMap::new();
    for entry in std::fs::read_dir(dir)
        .unwrap()
        .filter_map(|rs| rs.ok())
        .filter(|entry| entry.file_type().unwrap().is_dir())
    {
        if let Some(lang) = entry
            .file_name()
            .into_string()
            .ok()
            .filter(|l| l.parse::<unic_langid::LanguageIdentifier>().is_ok())
        {
            let resources = read_from_dir(entry.path());
            all_resources.insert(lang, resources);
        }
    }
    all_resources
}

/// Copied from `fluent_templates::fs` to avoid needing a seperate crate to
/// share the function.
pub(crate) fn read_from_dir<P: AsRef<Path>>(path: P) -> Vec<String> {
    let (tx, rx) = flume::unbounded();

    #[cfg(not(any(feature = "ignore", feature = "walkdir",)))]
    compile_error!("one of the features `ignore` or `walkdir` must be enabled.");

    #[cfg(feature = "ignore")]
    ignore::WalkBuilder::new(path)
        .follow_links(true)
        .build_parallel()
        .run(|| {
            let tx = tx.clone();
            Box::new(move |result| {
                if let Ok(entry) = result {
                    if entry.file_type().as_ref().map_or(false, |e| e.is_file())
                        && entry.path().extension().map_or(false, |e| e == "ftl")
                    {
                        tx.send(entry.path().display().to_string()).unwrap();
                    }
                }

                ignore::WalkState::Continue
            })
        });

    #[cfg(all(not(feature = "ignore"), feature = "walkdir"))]
    walkdir::WalkDir::new(path)
        .follow_links(true)
        .into_iter()
        .filter_map(|e| e.ok())
        .filter(|e| e.file_type().is_file())
        .filter(|e| e.path().extension().map_or(false, |e| e == "ftl"))
        .for_each(|e| {
            if let Ok(string) = std::fs::read_to_string(e.path()) {
                let _ = tx.send(string);
            } else {
                log::warn!("Couldn't read {}", e.path().display());
            }
        });

    rx.drain().collect::<Vec<_>>()
}

/// Loads all of your fluent resources at compile time as `&'static str`s and
/// and creates a new `StaticLoader` static variable that you can use in your
/// program. This allows you to easily ship your localisations as part of a
/// single binary.
///
/// ### Example
/// ```no_compile
/// fluent_templates::static_loader! {
///     // Declare our `StaticLoader` named `LOCALES`.
///     static LOCALES = {
///         // The directory of localisations and fluent resources.
///         locales: "./tests/locales",
///         // The language to falback on if something is not present.
///         fallback_language: "en-US",
///         // Optional: A shared fluent resource
///         core_locales: "./tests/locales/core.ftl",
///         // Optional: A function that is run over each fluent bundle.
///         customise: |bundle| {},
///     };
/// }
/// ```
#[proc_macro]
#[allow(non_snake_case)]
pub fn static_loader(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
    let StaticLoader {
        core_locales,
        customise,
        fallback_language,
        locales_directory,
        name,
        vis,
        ..
    } = parse_macro_input!(input as StaticLoader);
    let CRATE_NAME: TokenStream = quote!(fluent_templates);
    let LAZY: TokenStream = quote!(#CRATE_NAME::once_cell::sync::Lazy);
    let LANGUAGE_IDENTIFIER: TokenStream = quote!(#CRATE_NAME::loader::LanguageIdentifier);
    let FLUENT_BUNDLE: TokenStream = quote!(#CRATE_NAME::FluentBundle);
    let FLUENT_RESOURCE: TokenStream = quote!(#CRATE_NAME::fluent_bundle::FluentResource);
    let HASHMAP: TokenStream = quote!(std::collections::HashMap);

    let core_resource = if let Some(core_locales) = &core_locales {
        let core_locales = core_locales.display().to_string();
        quote!(
            Some(
                #CRATE_NAME::fs::resource_from_str(include_str!(#core_locales))
                    .expect("Couldn't load core resources")
            )
        )
    } else {
        quote!(None)
    };

    let mut insert_resources: Vec<_> = build_resources(locales_directory).into_iter().collect();

    if !insert_resources
        .iter()
        .any(|(lang, _)| *lang == fallback_language.value())
    {
        return syn::Error::new(
            fallback_language.span(),
            "Fallback language not found in locales directory",
        )
        .to_compile_error()
        .into();
    }

    // Make the output `TokenStream` only depend on the filenames and the file contents,
    // not hashmap/filesystem iteration order.
    insert_resources.sort();

    let insert_resources = insert_resources
        .into_iter()
        .map(|(locale, resources)| {
            quote!(
                resources.insert(
                    #locale.parse().unwrap(),
                    vec![#(#CRATE_NAME::fs::resource_from_str(include_str!(#resources)).unwrap(),)*]
                );
            )
        })
        .collect::<TokenStream>();

    let customise = customise.map_or(quote!(|_| ()), |c| quote!(#c));

    let resource_map = quote! {
        let mut resources = #HASHMAP::new();
        #insert_resources
        resources
    };

    let quote = quote! {
        #vis static #name : #LAZY<#CRATE_NAME::StaticLoader> = #LAZY::new(|| {
            static CORE_RESOURCE:
                #LAZY<Option<#FLUENT_RESOURCE>> =
                #LAZY::new(|| { #core_resource });

            static RESOURCES:
                #LAZY<#HASHMAP<#LANGUAGE_IDENTIFIER, Vec<#FLUENT_RESOURCE>>> =
                #LAZY::new(|| { #resource_map });

            static BUNDLES:
                #LAZY<
                    #HASHMAP<
                        #LANGUAGE_IDENTIFIER,
                        #FLUENT_BUNDLE<&'static #FLUENT_RESOURCE>
                    >
                > =
                #LAZY::new(||  {
                    #CRATE_NAME::loader::build_bundles(
                        &*RESOURCES,
                        CORE_RESOURCE.as_ref(),
                        #customise
                    )
                });

            static LOCALES:
                #LAZY<Vec<#LANGUAGE_IDENTIFIER>> =
                #LAZY::new(|| RESOURCES.keys().cloned().collect());

            static FALLBACKS:
                #LAZY<#HASHMAP<#LANGUAGE_IDENTIFIER, Vec<#LANGUAGE_IDENTIFIER>>> =
                #LAZY::new(|| #CRATE_NAME::loader::build_fallbacks(&*LOCALES));

            #CRATE_NAME::StaticLoader::new(
                &BUNDLES,
                &FALLBACKS,
                #fallback_language.parse().expect("invalid fallback language")
            )
        });
    };

    // println!("{}", quote);

    proc_macro::TokenStream::from(quote)
}