tide_serve_dir_macro/
lib.rs

1#![doc = include_str!("../README.md")]
2use input::{StaticFileMacroInput, IncludeFileMacroInput};
3use macro_function::macro_fn;
4use proc_macro::TokenStream;
5use syn::parse_macro_input;
6
7mod dir;
8mod input;
9mod mime;
10mod macro_function;
11
12/// Serves all files in the given directory using the `serve_file` function.
13/// Files that are not available during compile time won't be served, files that were available at compile time but not during runtime will cause a panic
14#[proc_macro]
15pub fn serve_dir(input: TokenStream) -> TokenStream {
16    let minput = parse_macro_input!(input as StaticFileMacroInput);
17    let minput = IncludeFileMacroInput{ app_ident: minput.app_ident, path: minput.path, directory: minput.directory, max_file_size: None };
18    macro_fn(minput, true)
19}
20
21/// Serves all files in the given directory by directly integrating them in the application binary using the `include_str!` macro of the standard library.
22/// Greatly increases performance but also obviously increases binary file size and memory usage. Addtionally files can't be changed at compile time.
23/// Takes an optional 4th parameter to set the maximum file size in bytes before the file is going to be served dynamically instead of being included in the binary.
24#[proc_macro]
25pub fn include_dir(input: TokenStream) -> TokenStream {
26    let minput = parse_macro_input!(input as IncludeFileMacroInput);
27    macro_fn(minput, false)
28}
29
30/// Selects how it should serve the directory using the build profile.
31/// In debug it uses `serve_dir!`, in release it uses `include_dir!`.
32/// Takes an optional 4th parameter to set the maximum file size in bytes before the file is going to be served dynamically instead of being included in the binary.
33#[proc_macro]
34pub fn auto_serve_dir(input: TokenStream) -> TokenStream {
35    let minput = parse_macro_input!(input as IncludeFileMacroInput);
36    #[cfg(debug_assertions)]
37    return macro_fn(minput, true);
38    #[cfg(not(debug_assertions))]
39    return macro_fn(minput, false);
40}