diff --git a/Cargo.lock b/Cargo.lock index e5d0ec7..e16f27c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,15 +2,141 @@ # It is not intended for manual editing. version = 4 +[[package]] +name = "byteorder" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b" + +[[package]] +name = "const-sha1" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0d8a42181e0652c2997ae4d217f25b63c5337a52fd2279736e97b832fa0a3cff" + +[[package]] +name = "data-encoding" +version = "2.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2a2330da5de22e8a3cb63252ce2abb30116bf5265e89c0e01bc17015ce30a476" + [[package]] name = "dir-embed" version = "0.1.1" +dependencies = [ + "picoserve", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "embedded-io" +version = "0.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "edd0f118536f44f5ccd48bcb8b111bdc3de888b58c74639dfb034a357d0f206d" + +[[package]] +name = "embedded-io-async" +version = "0.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3ff09972d4073aa8c299395be75161d582e7629cd663171d62af73c8d50dba3f" +dependencies = [ + "embedded-io", +] + +[[package]] +name = "futures-core" +version = "0.3.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "05f29059c0c2090612e8d742178b0580d2dc940c837851ad723096f87af6663e" + +[[package]] +name = "futures-task" +version = "0.3.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f90f7dce0722e95104fcb095585910c0977252f286e354b5e3bd38902cd99988" + +[[package]] +name = "futures-util" +version = "0.3.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9fa08315bb612088cc391249efdc3bc77536f16c91f6cf495e6fbe85b20a4a81" +dependencies = [ + "futures-core", + "futures-task", + "pin-project-lite", + "pin-utils", +] + +[[package]] +name = "hash32" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "47d60b12902ba28e2730cd37e95b8c9223af2808df9e902d4df49588d1470606" +dependencies = [ + "byteorder", +] + +[[package]] +name = "heapless" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0bfb9eb618601c89945a70e254898da93b13be0388091d42117462b265bb3fad" +dependencies = [ + "hash32", + "serde", + "stable_deref_trait", +] + +[[package]] +name = "lhash" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "744a4c881f502e98c2241d2e5f50040ac73b30194d64452bb6260393b53f0dc9" + +[[package]] +name = "picoserve" +version = "0.16.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "33c13ee221d37df77538b033b831099235067c6a3fdcd2b80ef46c3d5b96e3b2" +dependencies = [ + "const-sha1", + "data-encoding", + "embedded-io-async", + "futures-util", + "heapless", + "lhash", + "picoserve_derive", + "ryu", + "serde", + "serde-json-core", + "thiserror", +] + +[[package]] +name = "picoserve_derive" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "82a7350bdbef1ef80e4f058b89ca974dcc6526b04ac4d2095763b69760abf7ea" dependencies = [ "proc-macro2", "quote", "syn", ] +[[package]] +name = "pin-project-lite" +version = "0.2.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3b3cff922bd51709b605d9ead9aa71031d81447142d828eb4a6eba76fe619f9b" + +[[package]] +name = "pin-utils" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" + [[package]] name = "proc-macro2" version = "1.0.95" @@ -29,6 +155,49 @@ dependencies = [ "proc-macro2", ] +[[package]] +name = "ryu" +version = "1.0.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "28d3b2b1366ec20994f1fd18c3c594f05c5dd4bc44d8bb0c1c632c8d6829481f" + +[[package]] +name = "serde" +version = "1.0.219" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5f0e2c6ed6606019b4e29e69dbaba95b11854410e5347d525002456dbbb786b6" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde-json-core" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5b81787e655bd59cecadc91f7b6b8651330b2be6c33246039a65e5cd6f4e0828" +dependencies = [ + "heapless", + "ryu", + "serde", +] + +[[package]] +name = "serde_derive" +version = "1.0.219" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5b0276cf7f2c73365f7157c8123c21cd9a50fbbd844757af28ca1f5925fc2a00" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "stable_deref_trait" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a8f112729512f8e442d81f95a8a7ddf2b7c6b8a1a6f509a95864142b30cab2d3" + [[package]] name = "syn" version = "2.0.104" @@ -40,6 +209,26 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "thiserror" +version = "2.0.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "567b8a2dae586314f7be2a752ec7474332959c6460e02bde30d702a66d488708" +dependencies = [ + "thiserror-impl", +] + +[[package]] +name = "thiserror-impl" +version = "2.0.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7f7cf42b4507d8ea322120659672cf1b9dbb93f8f2d4ecfd6e51350ff5b17a1d" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "unicode-ident" version = "1.0.18" diff --git a/Cargo.toml b/Cargo.toml index 1700b72..98afb55 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,7 +11,12 @@ readme = "README.md" [lib] proc-macro = true +[features] +default = [] +picoserve = ["dep:picoserve"] + [dependencies] +picoserve = {version = "0.16.0", optional = true } proc-macro2 = "1" quote = "1" syn = { version = "2", features = ["full"] } diff --git a/src/lib.rs b/src/lib.rs index 4c15494..a415930 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -61,11 +61,17 @@ pub fn embed(input: proc_macro::TokenStream) -> proc_macro::TokenStream { match_arms.push(arm); } - let expanded = match mode { + #[allow(unused_mut)] + let mut expanded = match mode { EmbedMode::Bytes => generate_byte_impl(struct_name, match_arms), EmbedMode::Str => generate_str_impl(struct_name, match_arms), }; + #[cfg(feature = "picoserve")] + { + expanded.extend(generate_picoserv_impl(struct_name)); + } + proc_macro::TokenStream::from(expanded) } @@ -163,3 +169,48 @@ fn generate_str_impl( } } } + +#[cfg(feature = "picoserve")] +fn generate_picoserv_impl(struct_name: &Ident) -> proc_macro2::TokenStream { + quote! { + impl picoserve::routing::PathRouterService for #struct_name { + async fn call_request_handler_service< + R: picoserve::io::embedded_io_async::Read, + W: picoserve::response::ResponseWriter, + >( + &self, + state: &State, + current_path_parameters: CurrentPathParameters, + path: picoserve::request::Path<'_>, + request: picoserve::request::Request<'_, R>, + response_writer: W, + ) -> Result { + let requested_path = path.encoded(); + + let requested_file = if requested_path.is_empty() { + Self::get("index.html") + } else if let Some(striped_path) = requested_path.strip_prefix("/") { + Self::get(striped_path) + } else { + None + }; + + match requested_file { + Some(content) => { + let response = + picoserve::response::Response::new(picoserve::response::StatusCode::OK, content); + + + response_writer + .write_response(request.body_connection.finalize().await.unwrap(), response) + .await + } + None => { + use picoserve::routing::PathRouter; + picoserve::routing::NotFound.call_path_router(state,current_path_parameters,path,request,response_writer).await + } + } + } + } + } +}