Mark async main function with #[pagetop::main]

Also, use #[pagetop::test] for test functions. Add actix-web to
Cargo.toml only when using Actix Web macros for service configuration.
This commit is contained in:
Manuel Cillero 2023-07-19 23:58:23 +02:00
parent 4ce59d6520
commit 05304f116a
12 changed files with 58 additions and 21 deletions

View file

@ -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" }

View file

@ -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
}

View file

@ -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
}

View file

@ -5,5 +5,4 @@ edition = "2021"
publish = false
[dependencies]
actix-web = "4"
pagetop = { version = "0.0", path = "../../../pagetop" }

View file

@ -20,7 +20,7 @@ async fn hello_world(request: service::HttpRequest) -> ResultPage<Markup, FatalE
.render()
}
#[actix_web::main]
#[pagetop::main]
async fn main() -> std::io::Result<()> {
Application::prepare(&HelloWorld).unwrap().run()?.await
}

View file

@ -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
}

View file

@ -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"

View file

@ -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"

View file

@ -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<Markup, FatalError> {
//! 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.

View file

@ -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};

View file

@ -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;

View file

@ -7,12 +7,11 @@ authors = [
"Manuel Cillero <manuel@cillero.es>"
]
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 }