translatable_proc/lib.rs
1//! Macro declarations for the `translatable` crate.
2//!
3//! This crate shouldn't be used by itself,
4//! since the macros generate code with the context
5//! of the `translatable` library.
6//!
7//! The `translatable` library re-exports the macros
8//! declared in this crate.
9
10use proc_macro::TokenStream;
11use syn::parse_macro_input;
12
13use crate::macro_generation::translation::translation_macro;
14use crate::macro_input::translation::TranslationMacroArgs;
15
16mod data;
17mod macro_generation;
18mod macro_input;
19
20/// **translation obtention macro.**
21///
22/// This macro generates the way to obtain a translation
23/// from the translation files in the directory defined
24/// in the `translatable.toml` file.
25///
26/// **Parameters**
27/// * `language` - A string literal for static inference or an instance of
28/// `translatable::Language` for dynamic inference.
29/// * `path` - A pat prefixed with `static` for static inference or a `Vec<impl
30/// ToString>`
31/// for dynamic inference.
32/// * `replacements` - Arguments similar to python's `kwargs` for the
33/// translation replacements.
34///
35/// This macro provides optimizations depending on the dynamism
36/// of the parameters while calling the macro.
37///
38/// The optimizations are described the following way
39/// - If path is static, no runtime lookup will be required
40/// - If the path is dynamic, the file structure will be hardcoded.
41///
42/// - If the language is static, the validation will be reported by
43/// `rust-analyzer`.
44/// - If the language is dynamic the validation will be reported in runtime in
45/// the `Err` branch.
46///
47/// - If both are dynamic a single [`String`] will be generated.
48///
49/// Independently of any other parameter, the `replacements` parameter
50/// is always dynamic (context based).
51///
52/// You can shorten it's invocation if a similar identifier is on scope,
53/// for example `x = x` can be shortened with `x`.
54///
55/// Replacement parameters are not validated, if a parameter exists it will be
56/// replaced otherwise it won't.
57///
58/// **Returns**
59/// A `Result` containing either:
60/// * `Ok(String)` - If the invocation is successful.
61/// * `Err(translatable::Error)` - If the invocation fails with a runtime error.
62#[proc_macro]
63pub fn translation(input: TokenStream) -> TokenStream {
64 translation_macro(parse_macro_input!(input as TranslationMacroArgs).into()).into()
65}