diff --git a/crates/core/src/http/response.rs b/crates/core/src/http/response.rs index 1a1703ab5..5a41e85d5 100644 --- a/crates/core/src/http/response.rs +++ b/crates/core/src/http/response.rs @@ -489,6 +489,8 @@ impl Response { /// Useful when wanting to stream chunks from another thread. /// /// # Example + /// + /// ``` /// use salvo_core::prelude::*; /// #[handler] /// async fn hello(res: &mut Response) { @@ -498,6 +500,7 @@ impl Response { /// tx.send_data("Hello world").await.unwrap(); /// }); /// } + /// ``` #[inline] pub fn channel(&mut self) -> BodySender { let (sender, body) = ResBody::channel(); diff --git a/crates/craft-macros/src/craft.rs b/crates/craft-macros/src/craft.rs index f9ff3b39e..08ca77b52 100644 --- a/crates/craft-macros/src/craft.rs +++ b/crates/craft-macros/src/craft.rs @@ -28,7 +28,7 @@ fn take_method_macro(item_fn: &mut ImplItemFn) -> syn::Result> let mut new_attr: Option = None; for (idx, attr) in &mut item_fn.attrs.iter().enumerate() { if !(match attr.path().segments.last() { - Some(segment) => segment.ident.to_string() == "craft", + Some(segment) => segment.ident == "craft", None => false, }) { continue; @@ -63,24 +63,24 @@ fn take_method_macro(item_fn: &mut ImplItemFn) -> syn::Result> Ok(None) } -enum MethodStyle { - NoSelf, - RefSelf, - ArcSelf, +enum FnReceiver { + None, + Ref, + Arc, } -impl MethodStyle { +impl FnReceiver { fn from_method(method: &ImplItemFn) -> syn::Result { let Some(recv) = method.sig.receiver() else { - return Ok(Self::NoSelf); + return Ok(Self::None); }; let ty = recv.ty.to_token_stream().to_string().replace(" ", ""); match ty.as_str() { - "&Self" => Ok(Self::RefSelf), - "Arc" | "&Arc" => Ok(Self::ArcSelf), + "&Self" => Ok(Self::Ref), + "Arc" | "&Arc" => Ok(Self::Arc), _ => { if ty.ends_with("::Arc") { - Ok(Self::ArcSelf) + Ok(Self::Arc) } else { Err(syn::Error::new_spanned( method, @@ -102,8 +102,8 @@ fn rewrite_method(self_ty: Box, method: &mut ImplItemFn) -> syn::Result<() let method_name = method.sig.ident.clone(); let vis = method.vis.clone(); let mut attrs = method.attrs.clone(); - let mut new_method: ImplItemFn = match MethodStyle::from_method(method)? { - MethodStyle::NoSelf => { + let mut new_method: ImplItemFn = match FnReceiver::from_method(method)? { + FnReceiver::None => { method.attrs.push(macro_attr); parse_quote! { #vis fn #method_name() -> impl #handler { @@ -116,12 +116,8 @@ fn rewrite_method(self_ty: Box, method: &mut ImplItemFn) -> syn::Result<() } style => { let (receiver, output) = match style { - MethodStyle::RefSelf => { - (quote!(&self), quote!(::std::sync::Arc::new(self.clone()))) - } - MethodStyle::ArcSelf => { - (quote!(self: &::std::sync::Arc), quote!(self.clone())) - } + FnReceiver::Ref => (quote!(&self), quote!(::std::sync::Arc::new(self.clone()))), + FnReceiver::Arc => (quote!(self: &::std::sync::Arc), quote!(self.clone())), _ => unreachable!(), }; method.sig.inputs[0] = FnArg::Receiver(parse_quote!(&self)); diff --git a/crates/salvo/Cargo.toml b/crates/salvo/Cargo.toml index e50cfc459..8ffb44e54 100644 --- a/crates/salvo/Cargo.toml +++ b/crates/salvo/Cargo.toml @@ -25,7 +25,7 @@ path = "src/lib.rs" [features] default = ["cookie", "fix-http1-request-uri", "server", "server-handle", "http1", "http2", "ring"] -full = ["cookie", "fix-http1-request-uri", "server", "server-handle", "http1", "http2", "http2-cleartext", "quinn", "rustls", "native-tls", "openssl", "unix", "acme", "socket2", "tower-compat", "anyhow", "eyre", "test", "affix-state", "basic-auth", "force-https", "jwt-auth", "catch-panic", "compression", "logging", "proxy", "concurrency-limiter", "rate-limiter", "sse", "trailing-slash", "timeout", "websocket", "request-id", "caching-headers", "cache", "cors", "csrf", "flash", "rate-limiter", "session", "serve-static", "otel", "oapi", "ring"] +full = ["cookie", "fix-http1-request-uri", "server", "server-handle", "http1", "http2", "http2-cleartext", "quinn", "rustls", "native-tls", "openssl", "unix", "acme", "socket2", "tower-compat", "anyhow", "eyre", "test", "affix-state", "basic-auth", "craft", "force-https", "jwt-auth", "catch-panic", "compression", "logging", "proxy", "concurrency-limiter", "rate-limiter", "sse", "trailing-slash", "timeout", "websocket", "request-id", "caching-headers", "cache", "cors", "csrf", "flash", "rate-limiter", "session", "serve-static", "otel", "oapi", "ring"] cookie = ["salvo_core/cookie"] fix-http1-request-uri = ["salvo_core/fix-http1-request-uri"] server = ["salvo_core/server"] @@ -45,6 +45,7 @@ eyre = ["salvo_core/eyre"] test = ["salvo_core/test"] affix-state = ["salvo_extra/affix-state"] basic-auth = ["salvo_extra/basic-auth"] +craft = ["dep:salvo-craft"] force-https = ["salvo_extra/force-https"] jwt-auth = ["dep:salvo-jwt-auth"] catch-panic = ["salvo_extra/catch-panic"] @@ -87,7 +88,7 @@ salvo-serve-static = { workspace = true, features = ["full"], optional = true } salvo-proxy = { workspace = true, features = ["full"], optional = true } salvo-otel = { workspace = true, optional = true } salvo-oapi = { workspace = true, features = ["full"], optional = true } -salvo-craft = { workspace = true } +salvo-craft = { workspace = true, optional = true } [lints] workspace = true \ No newline at end of file diff --git a/crates/salvo/src/lib.rs b/crates/salvo/src/lib.rs index 70d2f351c..4a635c0cc 100644 --- a/crates/salvo/src/lib.rs +++ b/crates/salvo/src/lib.rs @@ -23,6 +23,7 @@ //! | `anyhow` | Integrate with the [`anyhow`](https://crates.io/crates/anyhow) crate | ❌ | //! | `eyre` | Integrate with the [`eyre`](https://crates.io/crates/eyre) crate | ❌ | //! | `affix-state` | Middleware for adding prefix and suffix to the request path | ❌ | +//! | `craft` | Generate handlers or endpoints with shared data | ❌ | //! | `basic-auth` | Middleware for basic authentication | ❌ | //! | `caching-headers` | Middleware for setting caching headers | ❌ | //! | `catch-panic` | Middleware for catching panics | ❌ | @@ -127,6 +128,11 @@ cfg_feature! { #[doc(no_inline)] pub use salvo_cors as cors; } +cfg_feature! { + #![feature ="craft"] + // #[doc(no_inline)] + pub use salvo_craft as craft; +} cfg_feature! { #![feature ="csrf"] #[doc(no_inline)] @@ -196,6 +202,11 @@ pub mod prelude { #![feature ="compression"] pub use salvo_compression::{Compression, CompressionAlgo, CompressionLevel}; } + cfg_feature! { + #![feature ="craft"] + // #[doc(no_inline)] + pub use salvo_craft::craft; + } cfg_feature! { #![feature ="csrf"] pub use salvo_csrf::CsrfDepotExt; @@ -264,5 +275,4 @@ pub mod prelude { pub use crate::oapi::redoc::ReDoc; pub use crate::oapi::scalar::Scalar; } - pub use salvo_craft::*; }