From 05304f116aff18d49d090bef4ba33be0ecc706f2 Mon Sep 17 00:00:00 2001 From: Manuel Cillero Date: Wed, 19 Jul 2023 23:58:23 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Mark=20async=20main=20function=20wi?= =?UTF-8?q?th=20#[pagetop::main]?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Also, use #[pagetop::test] for test functions. Add actix-web to Cargo.toml only when using Actix Web macros for service configuration. --- drust/Cargo.toml | 1 - drust/src/main.rs | 2 +- examples/basics/hello-name/src/main.rs | 2 +- examples/basics/hello-world/Cargo.toml | 1 - examples/basics/hello-world/src/main.rs | 2 +- pagetop-macros/src/lib.rs | 40 +++++++++++++++++++++++++ pagetop/STARTER.bin.Cargo.toml | 9 +++--- pagetop/STARTER.lib.Cargo.toml | 8 ++--- pagetop/src/lib.rs | 7 ++--- pagetop/src/prelude.rs | 2 +- pagetop/src/service.rs | 2 +- tests/Cargo.toml | 3 +- 12 files changed, 58 insertions(+), 21 deletions(-) diff --git a/drust/Cargo.toml b/drust/Cargo.toml index efd28cef..c0de9ab6 100644 --- a/drust/Cargo.toml +++ b/drust/Cargo.toml @@ -14,7 +14,6 @@ repository = "https://github.com/manuelcillero/pagetop" license = "Apache-2.0 OR MIT" [dependencies] -actix-web = "4" pagetop = { version = "0.0", path = "../pagetop", features = ["mysql"], default-features = false } # Themes. pagetop-aliner = { version = "0.0", path = "../pagetop-aliner" } diff --git a/drust/src/main.rs b/drust/src/main.rs index 25c5ed88..0eb6cef6 100644 --- a/drust/src/main.rs +++ b/drust/src/main.rs @@ -30,7 +30,7 @@ impl ModuleTrait for Drust { } } -#[actix_web::main] +#[pagetop::main] async fn main() -> std::io::Result<()> { Application::prepare(&Drust).unwrap().run()?.await } diff --git a/examples/basics/hello-name/src/main.rs b/examples/basics/hello-name/src/main.rs index cc0c8cd8..b95494e4 100644 --- a/examples/basics/hello-name/src/main.rs +++ b/examples/basics/hello-name/src/main.rs @@ -25,7 +25,7 @@ async fn hello_name( .render() } -#[actix_web::main] +#[pagetop::main] async fn main() -> std::io::Result<()> { Application::prepare(&HelloName).unwrap().run()?.await } diff --git a/examples/basics/hello-world/Cargo.toml b/examples/basics/hello-world/Cargo.toml index d70336aa..517f6a10 100644 --- a/examples/basics/hello-world/Cargo.toml +++ b/examples/basics/hello-world/Cargo.toml @@ -5,5 +5,4 @@ edition = "2021" publish = false [dependencies] -actix-web = "4" pagetop = { version = "0.0", path = "../../../pagetop" } diff --git a/examples/basics/hello-world/src/main.rs b/examples/basics/hello-world/src/main.rs index 14a1f5a7..c61d5b47 100644 --- a/examples/basics/hello-world/src/main.rs +++ b/examples/basics/hello-world/src/main.rs @@ -20,7 +20,7 @@ async fn hello_world(request: service::HttpRequest) -> ResultPage std::io::Result<()> { Application::prepare(&HelloWorld).unwrap().run()?.await } diff --git a/pagetop-macros/src/lib.rs b/pagetop-macros/src/lib.rs index 60a7fd74..b6acc4b8 100644 --- a/pagetop-macros/src/lib.rs +++ b/pagetop-macros/src/lib.rs @@ -54,3 +54,43 @@ pub fn fn_builder(_attr: TokenStream, item: TokenStream) -> TokenStream { }; expanded.into() } + +/// Marks async main function as the PageTop entry-point. +/// +/// # Examples +/// ``` +/// #[pagetop::main] +/// async fn main() { +/// async { println!("Hello world"); }.await +/// } +/// ``` +#[proc_macro_attribute] +pub fn main(_: TokenStream, item: TokenStream) -> TokenStream { + let mut output: TokenStream = (quote! { + #[::pagetop::service::rt::main(system = "::pagetop::service::rt::System")] + }) + .into(); + + output.extend(item); + output +} + +/// Marks async test functions to use the PageTop entry-point. +/// +/// # Examples +/// ``` +/// #[pagetop::test] +/// async fn test() { +/// assert_eq!(async { "Hello world" }.await, "Hello world"); +/// } +/// ``` +#[proc_macro_attribute] +pub fn test(_: TokenStream, item: TokenStream) -> TokenStream { + let mut output: TokenStream = (quote! { + #[::pagetop::service::rt::test(system = "::pagetop::service::rt::System")] + }) + .into(); + + output.extend(item); + output +} diff --git a/pagetop/STARTER.bin.Cargo.toml b/pagetop/STARTER.bin.Cargo.toml index 228385f9..18314711 100644 --- a/pagetop/STARTER.bin.Cargo.toml +++ b/pagetop/STARTER.bin.Cargo.toml @@ -7,16 +7,17 @@ edition = "2021" # https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +# Opcional. Para configurar servicios usando las macros de Actix Web: actix-web = "4" -# Si requiere acceso a base de datos (mysql, postgres y/o sqlite): -pagetop = { version = "0.0", features = ["mysql"], default-features = false } -# pagetop = "0.0" (en otro caso) - # Opcional. Para usar archivos y recursos binarios contenidos en el ejecutable: static-files = "0.2.3" # Opcional. Para serializar estructuras de datos: serde = { version = "1.0", features = ["derive"] } +# Si requiere acceso a base de datos (mysql, postgres y/o sqlite): +pagetop = { version = "0.0", features = ["mysql"], default-features = false } +# pagetop = "0.0" (en otro caso) + [build-dependencies] # Opcional. Para incluir archivos y recursos binarios en el ejecutable: pagetop-build = "0.0" diff --git a/pagetop/STARTER.lib.Cargo.toml b/pagetop/STARTER.lib.Cargo.toml index 26c4debe..7572d68d 100644 --- a/pagetop/STARTER.lib.Cargo.toml +++ b/pagetop/STARTER.lib.Cargo.toml @@ -7,15 +7,15 @@ edition = "2021" # https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -# Si requiere acceso a base de datos: -pagetop = { version = "0.0", features = ["database"], default-features = false } -# pagetop = "0.0" (en otro caso) - # Opcional. Para usar archivos y recursos binarios contenidos en la librería: static-files = "0.2.3" # Opcional. Para serializar estructuras de datos: serde = { version = "1.0", features = ["derive"] } +# Si requiere acceso a base de datos: +pagetop = { version = "0.0", features = ["database"], default-features = false } +# pagetop = "0.0" (en otro caso) + [build-dependencies] # Opcional. Para incluir archivos y recursos binarios en la propia librería: pagetop-build = "0.0" diff --git a/pagetop/src/lib.rs b/pagetop/src/lib.rs index 8557aaee..e5d36925 100644 --- a/pagetop/src/lib.rs +++ b/pagetop/src/lib.rs @@ -52,18 +52,17 @@ //! } //! //! fn configure_service(&self, cfg: &mut service::web::ServiceConfig) { -//! cfg.service(hello_world); +//! cfg.route("/", service::web::get().to(hello_world)); //! } //! } //! -//! #[service::get("/")] //! async fn hello_world(request: service::HttpRequest) -> ResultPage { //! Page::new(request) //! .with_in("content", Html::with(html! { h1 { "Hello World!" } })) //! .render() //! } //! -//! #[actix_web::main] +//! #[pagetop::main] //! async fn main() -> std::io::Result<()> { //! Application::prepare(&HelloWorld).unwrap().run()?.await //! } @@ -106,7 +105,7 @@ pub use concat_string::concat_string; /// Enables flexible identifier concatenation in macros, allowing new items with pasted identifiers. pub use paste::paste; -pub use pagetop_macros::fn_builder; +pub use pagetop_macros::{fn_builder, main, test}; // ************************************************************************************************* // GLOBAL. diff --git a/pagetop/src/prelude.rs b/pagetop/src/prelude.rs index a51eb062..e61d2df8 100644 --- a/pagetop/src/prelude.rs +++ b/pagetop/src/prelude.rs @@ -1,7 +1,7 @@ //! The PageTop Prelude. // Re-exported macros. -pub use crate::{concat_string, fn_builder, paste}; +pub use crate::{concat_string, fn_builder, main, paste, test}; // Global. pub use crate::{Handle, HashMapResources, LazyStatic, ResultExt, Weight}; diff --git a/pagetop/src/service.rs b/pagetop/src/service.rs index 2d518e25..3b12c807 100644 --- a/pagetop/src/service.rs +++ b/pagetop/src/service.rs @@ -2,7 +2,7 @@ pub use actix_session::Session; pub use actix_web::{ - cookie, get, http, web, App, HttpMessage, HttpRequest, HttpResponse, HttpServer, Responder, + cookie, get, http, rt, web, App, HttpMessage, HttpRequest, HttpResponse, HttpServer, Responder, }; pub use actix_web_files::Files as ActixFiles; diff --git a/tests/Cargo.toml b/tests/Cargo.toml index 29ac8465..cf54f760 100644 --- a/tests/Cargo.toml +++ b/tests/Cargo.toml @@ -7,12 +7,11 @@ authors = [ "Manuel Cillero " ] description = """\ - Test for PageTop.\ + Tests for PageTop.\ """ homepage = "https://pagetop.cillero.es" repository = "https://github.com/manuelcillero/pagetop" license = "Apache-2.0 OR MIT" [dependencies] -actix-web = "4" pagetop = { version = "0.0", path = "../pagetop", features = ["mysql"], default-features = false }