axum_embed_files_macros/
lib.rs

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
use std::path::Path;

use proc_macro::TokenStream;
use quote::quote;

use axum_embed_files_core::{content_type, etag, last_modified};

#[derive(Debug)]
struct Input {
    filename: String,
    dir: Option<String>,
}

impl syn::parse::Parse for Input {
    fn parse(input: syn::parse::ParseStream) -> syn::parse::Result<Self> {
        let first_str: syn::LitStr = input.parse()?;
        if input.is_empty() {
            return Ok(Input {
                filename: first_str.value(),
                dir: None,
            });
        }

        let _: syn::Token![,] = input.parse()?;

        let filename: syn::LitStr = input.parse()?;

        Ok(Input {
            filename: filename.value(),
            dir: Some(first_str.value()),
        })
    }
}

#[cfg(not(feature = "serve-from-fs"))]
fn render_bytes(file_path: &Path) -> proc_macro2::TokenStream {
    let file_path_str = file_path.display().to_string(); // TODO: non-unicode
    quote!(include_bytes!(#file_path_str))
}

#[cfg(feature = "serve-from-fs")]
fn render_bytes(_file_path: &Path) -> proc_macro2::TokenStream {
    let empty = syn::LitByteStr::new(&[], proc_macro2::Span::call_site());
    quote!(#empty)
}

/// Embed a local file in a tower Service.
///
/// ```nobuild
/// embed_file!(path: &str) -> EmbeddedFileService;
/// embed_file!(dir: &str, path: &str) -> EmbeddedFileService;
/// ```
///
/// This macro loads the specified file from the file system and embeds it
/// within an [EmbeddedFileService](service/struct.EmbeddedFileService.html).
/// The path is relative to your crate's `CARGO_MANIFEST_DIR`.  The modified
/// timestamp is taken from the file's metadata.
#[proc_macro]
pub fn embed_file(params: TokenStream) -> TokenStream {
    let input: Input = syn::parse(params).unwrap();

    let file_dir = {
        let mut p = std::env::current_dir().unwrap();
        if let Some(next) = &input.dir {
            p = p.join(next);
        }
        p
    };

    let file_path = file_dir.join(&input.filename);

    let contents = std::fs::read(&file_path).unwrap();

    let directory = file_dir.display().to_string();
    let filename = &input.filename;
    let bytes = render_bytes(&file_path);
    let etag = etag::generate(&contents[..]);
    let last_modified = last_modified::of_path(&file_path).unwrap();
    let content_type = match content_type::guess_from_path(&file_path) {
        None => quote!(None),
        Some(t) => quote!(Some(#t)),
    };

    quote! {
        ::axum_embed_files::service::EmbeddedFileService {
            directory: #directory,
            filename: #filename,
            bytes: #bytes,
            etag: #etag,
            last_modified: #last_modified,
            content_type: #content_type,
        }
    }.into()
}