lazy_static_include/
macro_include_array.rs

1#[cfg(debug_assertions)]
2/// Includes a file containing a rust array.
3///
4/// The file is located relative to the directory containing the manifest of your package.
5#[macro_export]
6macro_rules! lazy_static_include_array {
7    ( @i $name:ident: [$t:ident; $s:expr], $path:expr ) => {
8        {
9            let path = $crate::manifest_dir_macros::not_directory_path!($path);
10
11            let text = ::std::fs::read_to_string(path).unwrap();
12
13            let s = text.trim();
14
15            let mut result = [0 as $t; $s];
16
17            if let Ok($crate::syn::Expr::Array(array)) = $crate::syn::parse_str(s) {
18                for (i, l) in array.elems.into_iter().enumerate() {
19                    if i >= $s {
20                        panic!("incorrect length, bigger than {}, file: {}", $s, path);
21                    }
22
23                    let mut neg = false;
24
25                    let exp = match l {
26                        $crate::syn::Expr::Lit(exp) => exp,
27                        $crate::syn::Expr::Unary(exp) => {
28                            neg = true;
29
30                            match exp.expr.as_ref() {
31                                $crate::syn::Expr::Lit(exp) => exp.clone(),
32                                _ => {
33                                    panic!("incorrect element type, index = {}, file: {}", i, path);
34                                }
35                            }
36                        }
37                        _ => {
38                            panic!("incorrect element type, index = {}, file: {}", i, path);
39                        }
40                    };
41
42                    let accept_suffix = stringify!($t);
43
44                    match exp.lit {
45                        $crate::syn::Lit::Int(n) => {
46                            let suffix = n.suffix();
47
48                            if !suffix.is_empty() && suffix != accept_suffix {
49                                panic!("incorrect element type, index = {}, file: {}", i, path);
50                            }
51
52                            let n: $t = n.base10_parse().unwrap();
53
54                            result[i] = if neg {
55                                -n
56                            } else {
57                                n
58                            };
59                        }
60                        _ => {
61                            panic!("incorrect element type, index = {}, file: {}", i, path);
62                        }
63                    }
64                }
65
66                result
67            } else {
68                panic!("incorrect array, file: {}", path);
69            }
70        }
71    };
72    ( @u $name:ident: [$t:ident; $s:expr], $path:expr ) => {
73        {
74            let path = $crate::manifest_dir_macros::not_directory_path!($path);
75
76            let text = ::std::fs::read_to_string(path).unwrap();
77
78            let s = text.trim();
79
80            let mut result = [0 as $t; $s];
81
82            if let Ok($crate::syn::Expr::Array(array)) = $crate::syn::parse_str(s) {
83                for (i, l) in array.elems.into_iter().enumerate() {
84                    if i >= $s {
85                        panic!("incorrect length, bigger than {}, file: {}", $s, path);
86                    }
87
88                    let mut neg = false;
89
90                    let exp = match l {
91                        $crate::syn::Expr::Lit(exp) => exp,
92                        _ => {
93                            panic!("incorrect element type, index = {}, file: {}", i, path);
94                        }
95                    };
96
97                    let accept_suffix = stringify!($t);
98
99                    match exp.lit {
100                        $crate::syn::Lit::Int(n) => {
101                            let suffix = n.suffix();
102
103                            if !suffix.is_empty() && suffix != accept_suffix {
104                                panic!("incorrect element type, index = {}, file: {}", i, path);
105                            }
106
107                            result[i] = n.base10_parse().unwrap();
108                        }
109                        _ => {
110                            panic!("incorrect element type, index = {}, file: {}", i, path);
111                        }
112                    }
113                }
114
115                result
116            } else {
117                panic!("incorrect array, file: {}", path);
118            }
119        }
120    };
121    ( @f $name:ident: [$t:ident; $s:expr], $path:expr ) => {
122        {
123            let path = $crate::manifest_dir_macros::not_directory_path!($path);
124
125            let text = ::std::fs::read_to_string(path).unwrap();
126
127            let s = text.trim();
128
129            let mut result = [0 as $t; $s];
130
131            if let Ok($crate::syn::Expr::Array(array)) = $crate::syn::parse_str(s) {
132                for (i, l) in array.elems.into_iter().enumerate() {
133                    if i >= $s {
134                        panic!("incorrect length, bigger than {}, file: {}", $s, path);
135                    }
136
137                    let mut neg = false;
138
139                    let exp = match l {
140                        $crate::syn::Expr::Lit(exp) => exp,
141                        $crate::syn::Expr::Unary(exp) => {
142                            neg = true;
143
144                            match exp.expr.as_ref() {
145                                $crate::syn::Expr::Lit(exp) => exp.clone(),
146                                _ => {
147                                    panic!("incorrect element type, index = {}, file: {}", i, path);
148                                }
149                            }
150                        }
151                        _ => {
152                            panic!("incorrect element type, index = {}, file: {}", i, path);
153                        }
154                    };
155
156                    let accept_suffix = stringify!($t);
157
158                    match exp.lit {
159                        $crate::syn::Lit::Float(f) => {
160                            let suffix = f.suffix();
161
162                            if !suffix.is_empty() && suffix != accept_suffix {
163                                panic!("incorrect element type, index = {}, file: {}", i, path);
164                            }
165
166                            let f: $t = f.base10_parse().unwrap();
167
168                            result[i] = if neg {
169                                -f
170                            } else {
171                                f
172                            };
173                        }
174                        $crate::syn::Lit::Int(n) => {
175                            let suffix = n.suffix();
176
177                            if suffix != accept_suffix {
178                                panic!("incorrect element type, index = {}, file: {}", i, path);
179                            }
180
181                            let n: $t = n.base10_parse().unwrap();
182
183                            result[i] = if neg {
184                                -n
185                            } else {
186                                n
187                            };
188                        }
189                        _ => {
190                            panic!("incorrect element type, index = {}, file: {}", i, path);
191                        }
192                    }
193                }
194
195                result
196            } else {
197                panic!("incorrect array, file: {}", path);
198            }
199        }
200    };
201    ( @c $name:ident: [$t:ident; $s:expr], $path:expr ) => {
202        {
203            let path = $crate::manifest_dir_macros::not_directory_path!($path);
204
205            let text = ::std::fs::read_to_string(path).unwrap();
206
207            let s = text.trim();
208
209            let mut result = ['\0'; $s];
210
211            if let Ok($crate::syn::Expr::Array(array)) = $crate::syn::parse_str(s) {
212                for (i, l) in array.elems.into_iter().enumerate() {
213                    if i >= $s {
214                        panic!("incorrect length, bigger than {}, file: {}", $s, path);
215                    }
216
217                    if let $crate::syn::Expr::Lit(exp) = l {
218                        match exp.lit {
219                            $crate::syn::Lit::Char(c) => {
220                                result[i] = c.value();
221                            }
222                            _ => {
223                                panic!("incorrect element type, index = {}, file: {}", i, path);
224                            }
225                        }
226                    } else {
227                        panic!("incorrect element type, index = {}, file: {}", i, path);
228                    }
229                }
230
231                result
232            } else {
233                panic!("incorrect array, file: {}", path);
234            }
235        }
236    };
237    ( @b $name:ident: [$t:ident; $s:expr], $path:expr ) => {
238        {
239            let path = $crate::manifest_dir_macros::not_directory_path!($path);
240
241            let text = ::std::fs::read_to_string(path).unwrap();
242
243            let s = text.trim();
244
245            let mut result = [false; $s];
246
247            if let Ok($crate::syn::Expr::Array(array)) = $crate::syn::parse_str(s) {
248                for (i, l) in array.elems.into_iter().enumerate() {
249                    if i >= $s {
250                        panic!("incorrect length, bigger than {}, file: {}", $s, path);
251                    }
252
253                    if let $crate::syn::Expr::Lit(exp) = l {
254                        match exp.lit {
255                            $crate::syn::Lit::Bool(b) => {
256                                result[i] = b.value;
257                            }
258                            _ => {
259                                panic!("incorrect element type, index = {}, file: {}", i, path);
260                            }
261                        }
262                    } else {
263                        panic!("incorrect element type, index = {}, file: {}", i, path);
264                    }
265                }
266
267                result
268            } else {
269                panic!("incorrect array, file: {}", path);
270            }
271        }
272    };
273    ( @s $name:ident: [$t:ident; $s:expr], $path:expr ) => {
274        {
275            use ::std::mem::{forget, transmute};
276
277            let path = $crate::manifest_dir_macros::not_directory_path!($path);
278
279            let text = ::std::fs::read_to_string(path).unwrap();
280
281            let s = text.trim();
282
283            let mut result = Vec::with_capacity($s);
284
285            if let Ok($crate::syn::Expr::Array(array)) = $crate::syn::parse_str(s) {
286                for (i, l) in array.elems.into_iter().enumerate() {
287                    if i >= $s {
288                        panic!("incorrect length, bigger than {}, file: {}", $s, path);
289                    }
290
291                    if let $crate::syn::Expr::Lit(exp) = l {
292                        match exp.lit {
293                            $crate::syn::Lit::Str(s) => {
294                                result.push(s.value());
295                            }
296                            _ => {
297                                panic!("incorrect element type, index = {}, file: {}", i, path);
298                            }
299                        }
300                    } else {
301                        panic!("incorrect element type, index = {}, file: {}", i, path);
302                    }
303                }
304
305                let mut result_str = [""; $s];
306
307                for (i, s) in result.iter().enumerate() {
308                    result_str[i] = unsafe {
309                        let ret = transmute(s.as_str());
310                        ret
311                    };
312                }
313
314                unsafe {
315                    forget(result);
316                };
317
318                result_str
319            } else {
320                panic!("incorrect array, file: {}", path);
321            }
322        }
323    };
324    ( @type $name:ident: [isize; $s:expr], $path:expr ) => {
325        $crate::lazy_static_include_array!(@i $name: [isize; $s], $path);
326    };
327    ( @type $name:ident: [i8; $s:expr], $path:expr ) => {
328        $crate::lazy_static_include_array!(@i $name: [i8; $s], $path);
329    };
330    ( @type $name:ident: [i16; $s:expr], $path:expr ) => {
331        $crate::lazy_static_include_array!(@i $name: [i16; $s], $path);
332    };
333    ( @type $name:ident: [i32; $s:expr], $path:expr ) => {
334        $crate::lazy_static_include_array!(@i $name: [i32; $s], $path);
335    };
336    ( @type $name:ident: [i64; $s:expr], $path:expr ) => {
337        $crate::lazy_static_include_array!(@i $name: [i64; $s], $path);
338    };
339    ( @type $name:ident: [i128; $s:expr], $path:expr ) => {
340        $crate::lazy_static_include_array!(@i $name: [i128; $s], $path);
341    };
342    ( @type $name:ident: [usize; $s:expr], $path:expr ) => {
343        $crate::lazy_static_include_array!(@u $name: [usize; $s], $path);
344    };
345    ( @type $name:ident: [u8; $s:expr], $path:expr ) => {
346        $crate::lazy_static_include_array!(@u $name: [u8; $s], $path);
347    };
348    ( @type $name:ident: [u16; $s:expr], $path:expr ) => {
349        $crate::lazy_static_include_array!(@u $name: [u16; $s], $path);
350    };
351    ( @type $name:ident: [u32; $s:expr], $path:expr ) => {
352        $crate::lazy_static_include_array!(@u $name: [u32; $s], $path);
353    };
354    ( @type $name:ident: [u64; $s:expr], $path:expr ) => {
355        $crate::lazy_static_include_array!(@u $name: [u64; $s], $path);
356    };
357    ( @type $name:ident: [u128; $s:expr], $path:expr ) => {
358        $crate::lazy_static_include_array!(@u $name: [u128; $s], $path);
359    };
360    ( @type $name:ident: [f32; $s:expr], $path:expr ) => {
361        $crate::lazy_static_include_array!(@f $name: [f32; $s], $path);
362    };
363    ( @type $name:ident: [f64; $s:expr], $path:expr ) => {
364        $crate::lazy_static_include_array!(@f $name: [f64; $s], $path);
365    };
366    ( @type $name:ident: [char; $s:expr], $path:expr ) => {
367        $crate::lazy_static_include_array!(@c $name: [char; $s], $path);
368    };
369    ( @type $name:ident: [bool; $s:expr], $path:expr ) => {
370        $crate::lazy_static_include_array!(@b $name: [bool; $s], $path);
371    };
372    ( @type $name:ident: [&'static str; $s:expr], $path:expr ) => {
373        $crate::lazy_static_include_array!(@s $name: [bool; $s], $path);
374    };
375    ( @unit $(#[$attr: meta])* $name:ident: [$(& $lt:lifetime)? $t:ident; $s:expr] => $path:expr ) => {
376        $crate::lazy_static::lazy_static! {
377            $(#[$attr])*
378            static ref $name: [$(& $lt)? $t; $s] = $crate::lazy_static_include_array!(@type $name: [$(& $lt)? $t; $s], $path);
379        }
380    };
381    ( @unit $(#[$attr: meta])* pub$(($($v:tt)+))? $name:ident: [$(& $lt:lifetime)? $t:ident; $s:expr] => $path:expr ) => {
382        $crate::lazy_static::lazy_static! {
383            $(#[$attr])*
384            pub$(($($v)+))? static ref $name: [$(& $lt)? $t; $s] = $crate::lazy_static_include_array!(@type $name: [$(& $lt)? $t; $s], $path);
385        }
386    };
387    ( $($(#[$attr: meta])* $name:ident: [$(& $lt:lifetime)? $t:ident; $s:expr] => $path:expr),* $(,)* ) => {
388        $(
389            $crate::lazy_static_include_array! {
390                @unit
391                $(#[$attr])*
392                $name: [$(& $lt)? $t; $s] => $path
393            }
394        )*
395    };
396    ( $($(#[$attr: meta])* pub$(($($v:tt)+))? $name:ident: [$(& $lt:lifetime)? $t:ident; $s:expr] => $path:expr),* $(,)* ) => {
397        $(
398            $crate::lazy_static_include_array! {
399                @unit
400                $(#[$attr])*
401                pub$(($($v)+))? $name: [$(& $lt)? $t; $s] => $path
402            }
403        )*
404    };
405}
406
407#[cfg(not(debug_assertions))]
408/// Includes a file containing a rust array.
409///
410/// The file is located relative to the directory containing the manifest of your package.
411#[macro_export]
412macro_rules! lazy_static_include_array {
413    ( @unit $(#[$attr: meta])* $name:ident: [$(& $lt:lifetime)? $t:ident; $s:expr] => $path:expr ) => {
414        $crate::lazy_static::lazy_static! {
415            $(#[$attr])*
416            static ref $name: [$(& $lt)? $t; $s] = include!($crate::manifest_dir_macros::path!($path));
417        }
418    };
419    ( @unit $(#[$attr: meta])* pub$(($($v:tt)+))? $name:ident: [$(& $lt:lifetime)? $t:ident; $s:expr] => $path:expr ) => {
420        $crate::lazy_static::lazy_static! {
421            $(#[$attr])*
422            pub$(($($v)+))? static ref $name: [$(& $lt)? $t; $s] = include!($crate::manifest_dir_macros::path!($path));
423        }
424    };
425    ( $($(#[$attr: meta])* $name:ident: [$(& $lt:lifetime)? $t:ident; $s:expr] => $path:expr),* $(,)* ) => {
426        $(
427            $crate::lazy_static_include_array! {
428                @unit
429                $(#[$attr])*
430                $name: [$(& $lt)? $t; $s] => $path
431            }
432        )*
433    };
434    ( $($(#[$attr: meta])* pub$(($($v:tt)+))? $name:ident: [$(& $lt:lifetime)? $t:ident; $s:expr] => $path:expr),* $(,)* ) => {
435        $(
436            $crate::lazy_static_include_array! {
437                @unit
438                $(#[$attr])*
439                pub$(($($v)+))? $name: [$(& $lt)? $t; $s] => $path
440            }
441        )*
442    };
443}