implemented mime mode

- required for picoserve
- moved testdata
This commit is contained in:
Niklas Kapelle 2025-07-30 00:53:05 +02:00
parent 685c0071f6
commit 659ab3f447
Signed by: niklas
GPG Key ID: 4EB651B36D841D16
14 changed files with 183 additions and 10 deletions

24
Cargo.lock generated
View File

@ -24,6 +24,8 @@ checksum = "2a2330da5de22e8a3cb63252ce2abb30116bf5265e89c0e01bc17015ce30a476"
name = "dir-embed"
version = "0.1.1"
dependencies = [
"mime",
"mime_guess",
"picoserve",
"proc-macro2",
"quote",
@ -95,6 +97,22 @@ version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "744a4c881f502e98c2241d2e5f50040ac73b30194d64452bb6260393b53f0dc9"
[[package]]
name = "mime"
version = "0.3.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6877bb514081ee2a7ff5ef9de3281f14a4dd4bceac4c09388074a6b5df8a139a"
[[package]]
name = "mime_guess"
version = "2.0.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f7c44f8e672c00fe5308fa235f821cb4198414e1c77935c1ab6948d3fd78550e"
dependencies = [
"mime",
"unicase",
]
[[package]]
name = "picoserve"
version = "0.16.0"
@ -229,6 +247,12 @@ dependencies = [
"syn",
]
[[package]]
name = "unicase"
version = "2.8.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "75b844d17643ee918803943289730bec8aac480150456169e647ed0b576ba539"
[[package]]
name = "unicode-ident"
version = "1.0.18"

View File

@ -16,6 +16,8 @@ default = []
picoserve = ["dep:picoserve"]
[dependencies]
mime = { version = "0.3.17" }
mime_guess = "2.0.5"
picoserve = {version = "0.16.0", optional = true }
proc-macro2 = "1"
quote = "1"

View File

@ -78,6 +78,8 @@ checksum = "2a2330da5de22e8a3cb63252ce2abb30116bf5265e89c0e01bc17015ce30a476"
name = "dir-embed"
version = "0.1.1"
dependencies = [
"mime",
"mime_guess",
"picoserve 0.16.0",
"proc-macro2",
"quote",
@ -188,6 +190,22 @@ version = "2.7.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "32a282da65faaf38286cf3be983213fcf1d2e2a58700e808f83f4ea9a4804bc0"
[[package]]
name = "mime"
version = "0.3.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6877bb514081ee2a7ff5ef9de3281f14a4dd4bceac4c09388074a6b5df8a139a"
[[package]]
name = "mime_guess"
version = "2.0.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f7c44f8e672c00fe5308fa235f821cb4198414e1c77935c1ab6948d3fd78550e"
dependencies = [
"mime",
"unicase",
]
[[package]]
name = "miniz_oxide"
version = "0.8.9"
@ -467,6 +485,12 @@ dependencies = [
"syn",
]
[[package]]
name = "unicase"
version = "2.8.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "75b844d17643ee918803943289730bec8aac480150456169e647ed0b576ba539"
[[package]]
name = "unicode-ident"
version = "1.0.18"

View File

@ -4,8 +4,10 @@ use picoserve::routing::get;
#[derive(Embed)]
#[dir = "../static"]
#[mode = "mime"]
struct StaticStuff;
#[tokio::main]
async fn main() {
let port = 8000;

View File

@ -1,3 +1,4 @@
use mime::{Mime};
use proc_macro::TokenStream;
use quote::quote;
use syn::spanned::Spanned;
@ -10,6 +11,7 @@ use std::path::{Path, PathBuf};
enum EmbedMode {
Bytes,
Str,
BytesMime,
}
#[proc_macro_derive(Embed, attributes(dir, mode))]
@ -44,6 +46,7 @@ pub fn embed(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
for entry in collect_files(&absolue_path) {
let rel_path = entry
.0
.strip_prefix(&absolue_path)
.unwrap()
.to_str()
@ -56,6 +59,11 @@ pub fn embed(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
let arm = match mode {
EmbedMode::Bytes => generate_byte_arm(&rel_path, include_string),
EmbedMode::Str => generate_str_arm(&rel_path, include_string),
EmbedMode::BytesMime => generate_mime_arm(
&rel_path,
include_string,
entry.1.unwrap_or(mime::APPLICATION_OCTET_STREAM),
),
};
match_arms.push(arm);
@ -65,26 +73,33 @@ pub fn embed(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
let mut expanded = match mode {
EmbedMode::Bytes => generate_byte_impl(struct_name, match_arms),
EmbedMode::Str => generate_str_impl(struct_name, match_arms),
EmbedMode::BytesMime => generate_mime_impl(struct_name, match_arms),
};
#[cfg(feature = "picoserve")]
{
expanded.extend(generate_picoserv_impl(struct_name));
if matches!(mode,EmbedMode::BytesMime) {
expanded.extend(generate_picoserv_impl(struct_name));
}
}
proc_macro::TokenStream::from(expanded)
}
fn collect_files(dir: &Path) -> Vec<PathBuf> {
fn collect_files(dir: &Path) -> Vec<(PathBuf, Option<Mime>)> {
let mut files = Vec::new();
for entry in fs::read_dir(dir).unwrap() {
let path = entry.unwrap().path();
if path.is_file() {
files.push(path);
let mime_type = mime_guess::from_path(&path);
files.push((path, mime_type.first()));
} else if path.is_dir() {
files.extend(collect_files(&path));
}
}
files
}
@ -108,21 +123,22 @@ fn extract_dir_path(attr: &Attribute) -> String {
fn extract_mode(attr: &Attribute) -> EmbedMode {
let meta = match &attr.meta {
Meta::NameValue(meta) => meta,
_ => panic!("Expected #[mode = \"bytes\"|\"str\"] as a name-value attribute."),
_ => panic!("Expected #[mode = \"bytes\"|\"str\"|\"mime\"] as a name-value attribute."),
};
let expr_lit = match &meta.value {
Expr::Lit(expr_lit) => expr_lit,
_ => panic!("Expected #[mode = \"bytes\"|\"str\"] with a string literal."),
_ => panic!("Expected #[mode = \"bytes\"|\"str\"|\"mime\"] with a string literal."),
};
match &expr_lit.lit {
Lit::Str(str) => match str.value().as_str() {
"bytes" => EmbedMode::Bytes,
"str" => EmbedMode::Str,
other => panic!("Unknown mode: {other}. Use `bytes` or `str`."),
"mime" => EmbedMode::BytesMime,
other => panic!("Unknown mode: {other}. Use `bytes`,`str` or `mime`."),
},
_ => panic!("Expected #[mode = \"bytes\"|\"str\"] to be a string."),
_ => panic!("Expected #[mode = \"bytes\"|\"str\"|\"mime\"] to be a string."),
}
}
@ -170,6 +186,29 @@ fn generate_str_impl(
}
}
fn generate_mime_arm(rel: &str, include: &str, mime_type: Mime) -> proc_macro2::TokenStream {
let mime_str = mime_type.essence_str();
quote! {
#rel => Some((include_bytes!(#include),#mime_str)),
}
}
fn generate_mime_impl(
struct_name: &Ident,
match_arms: Vec<proc_macro2::TokenStream>,
) -> proc_macro2::TokenStream {
quote! {
impl #struct_name {
pub fn get(name: &str) -> Option<(&'static [u8],&'static str)> {
match name {
#(#match_arms)*
_ => None,
}
}
}
}
}
#[cfg(feature = "picoserve")]
fn generate_picoserv_impl(struct_name: &Ident) -> proc_macro2::TokenStream {
quote! {
@ -198,7 +237,7 @@ fn generate_picoserv_impl(struct_name: &Ident) -> proc_macro2::TokenStream {
match requested_file {
Some(content) => {
let response =
picoserve::response::Response::new(picoserve::response::StatusCode::OK, content);
picoserve::response::Response::new(picoserve::response::StatusCode::OK, content.0);
response_writer

1
testdata/bytes/bin vendored Normal file
View File

@ -0,0 +1 @@
蕲撅

12
testdata/bytes/index.html vendored Normal file
View File

@ -0,0 +1,12 @@
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
</head>
<body>
<h1>This is a Heading</h1>
<p>This is a paragraph.</p>
</body>
</html>

1
testdata/str/file1.txt vendored Normal file
View File

@ -0,0 +1 @@
file1

1
testdata/str/sub/file2.txt vendored Normal file
View File

@ -0,0 +1 @@
file2

View File

@ -1,7 +1,7 @@
use dir_embed::Embed;
#[derive(Embed)]
#[dir = "./../testdata/"]
#[dir = "./../testdata/bytes/"]
#[mode = "bytes"]
pub struct Assets;
@ -41,3 +41,9 @@ fn byte_sub_directories_content() {
let content_is = Assets::get("sub/file2.txt").unwrap();
assert_eq!(*content_is, *content_should);
}
#[test]
fn byte_read_bin() {
let file = Assets::get("bin").unwrap();
assert_eq!(file, [0xDE, 0xAD, 0xBE, 0xEF]);
}

61
tests/mime.rs Normal file
View File

@ -0,0 +1,61 @@
use dir_embed::Embed;
#[derive(Embed)]
#[dir = "./../testdata/bytes/"]
#[mode = "mime"]
pub struct Assets;
#[test]
fn mime_get() {
assert!(Assets::get("file1.txt").is_some());
}
#[test]
fn mime_get_missing() {
assert!(Assets::get("missing.txt").is_none());
}
#[test]
fn mime_read_content() {
let content_should = "file1".as_bytes();
let content_is = Assets::get("file1.txt").unwrap();
assert_eq!(*content_is.0, *content_should);
}
#[test]
fn mime_parse_string() {
let file: &[u8] = Assets::get("file1.txt").expect("Can't find file").0;
let string = str::from_utf8(file).expect("Failed to parse file");
assert_eq!(string, "file1");
}
#[test]
fn mime_sub_directories_get() {
assert!(Assets::get("sub/file2.txt").is_some());
}
#[test]
fn mime_sub_directories_content() {
let content_should = "file2".as_bytes();
let content_is = Assets::get("sub/file2.txt").unwrap();
assert_eq!(*content_is.0, *content_should);
}
#[test]
fn mime_type_html() {
let file = Assets::get("index.html").unwrap();
assert_eq!(file.1, "text/html");
}
#[test]
fn mime_type_plain() {
let file = Assets::get("file1.txt").unwrap();
assert_eq!(file.1, "text/plain");
}
#[test]
fn mime_default_type() {
let file = Assets::get("bin").unwrap();
assert_eq!(file.1, "application/octet-stream");
}

View File

@ -1,7 +1,7 @@
use dir_embed::Embed;
#[derive(Embed)]
#[dir = "./../testdata/"]
#[dir = "./../testdata/str/"]
#[mode = "str"]
pub struct Assets;