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
//! Funciones útiles.

use crate::Handle;

// *************************************************************************************************
// FUNCIONES ÚTILES.
// *************************************************************************************************

// https://stackoverflow.com/a/71464396
#[doc(hidden)]
pub const fn handle(
    module_path: &'static str,
    file: &'static str,
    line: u32,
    column: u32,
) -> Handle {
    let mut hash = 0xcbf29ce484222325;
    let prime = 0x00000100000001B3;

    let mut bytes = module_path.as_bytes();
    let mut i = 0;

    while i < bytes.len() {
        hash ^= bytes[i] as u64;
        hash = hash.wrapping_mul(prime);
        i += 1;
    }

    bytes = file.as_bytes();
    i = 0;
    while i < bytes.len() {
        hash ^= bytes[i] as u64;
        hash = hash.wrapping_mul(prime);
        i += 1;
    }

    hash ^= line as u64;
    hash = hash.wrapping_mul(prime);
    hash ^= column as u64;
    hash = hash.wrapping_mul(prime);
    hash
}

pub fn partial_type_name(type_name: &'static str, last: usize) -> &'static str {
    if last == 0 {
        return type_name;
    }
    let positions: Vec<_> = type_name.rmatch_indices("::").collect();
    if positions.len() < last {
        return type_name;
    }
    &type_name[(positions[last - 1].0 + 2)..]
}

pub fn single_type_name<T: ?Sized>() -> &'static str {
    partial_type_name(std::any::type_name::<T>(), 1)
}

// *************************************************************************************************
// MACROS DECLARATIVAS.
// *************************************************************************************************

#[macro_export]
/// Macro para construir grupos de pares clave-valor.
///
/// ```rust#ignore
/// let args = kv![
///     "userName" => "Roberto",
///     "photoCount" => 3,
///     "userGender" => "male",
/// ];
/// ```
macro_rules! kv {
    ( $($key:expr => $value:expr),* $(,)? ) => {{
        let mut a = std::collections::HashMap::new();
        $(
            a.insert($key.into(), $value.into());
        )*
        a
    }};
}

#[macro_export]
macro_rules! use_handle {
    ( $($HANDLE:ident),* $(,)? ) => {
        $(
            /// Public constant handle to represent a unique PageTop building element.
            pub const $HANDLE: $crate::Handle =
                $crate::util::handle(module_path!(), file!(), line!(), column!());
        )*
    };
}

#[macro_export]
/// Define un conjunto de elementos de localización y funciones locales de traducción.
macro_rules! use_locale {
    ( $LOCALES:ident $(, $core_locales:literal)? ) => {
        use $crate::locale::*;

        fluent_templates::static_loader! {
            static $LOCALES = {
                locales: "src/locale",
                $( core_locales: $core_locales, )?
                fallback_language: "en-US",

                // Elimina las marcas Unicode que delimitan los argumentos.
                customise: |bundle| bundle.set_use_isolating(false),
            };
        }
    };
    ( $LOCALES:ident[$dir_locales:literal] $(, $core_locales:literal)? ) => {
        use $crate::locale::*;

        fluent_templates::static_loader! {
            static $LOCALES = {
                locales: $dir_locales,
                $( core_locales: $core_locales, )?
                fallback_language: "en-US",

                // Elimina las marcas Unicode que delimitan los argumentos.
                customise: |bundle| bundle.set_use_isolating(false),
            };
        }
    };
}

#[macro_export]
macro_rules! use_static {
    ( $bundle:ident ) => {
        mod static_bundle {
            include!(concat!(env!("OUT_DIR"), "/", stringify!($bundle), ".rs"));
        }
    };
    ( $bundle:ident => $STATIC:ident ) => {
        mod static_bundle {
            include!(concat!(env!("OUT_DIR"), "/", stringify!($bundle), ".rs"));
        }
        static $STATIC: LazyStatic<HashMapResources> = LazyStatic::new(static_bundle::$bundle);
    };
}

#[macro_export]
macro_rules! serve_static_files {
    ( $cfg:ident, $path:expr, $bundle:ident ) => {{
        let static_files = &$crate::config::SETTINGS.dev.static_files;
        if static_files.is_empty() {
            $cfg.service($crate::service::ResourceFiles::new(
                $path,
                static_bundle::$bundle(),
            ));
        } else {
            $cfg.service(
                $crate::service::ActixFiles::new(
                    $path,
                    $crate::concat_string!(static_files, $path),
                )
                .show_files_listing(),
            );
        }
    }};
}