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" license = "Apache-2.0 OR MIT"
[dependencies] [dependencies]
actix-web = "4"
pagetop = { version = "0.0", path = "../pagetop", features = ["mysql"], default-features = false } pagetop = { version = "0.0", path = "../pagetop", features = ["mysql"], default-features = false }
# Themes. # Themes.
pagetop-aliner = { version = "0.0", path = "../pagetop-aliner" } 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<()> { async fn main() -> std::io::Result<()> {
Application::prepare(&Drust).unwrap().run()?.await Application::prepare(&Drust).unwrap().run()?.await
} }

View file

@ -25,7 +25,7 @@ async fn hello_name(
.render() .render()
} }
#[actix_web::main] #[pagetop::main]
async fn main() -> std::io::Result<()> { async fn main() -> std::io::Result<()> {
Application::prepare(&HelloName).unwrap().run()?.await Application::prepare(&HelloName).unwrap().run()?.await
} }

View file

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

View file

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

View file

@ -54,3 +54,43 @@ pub fn fn_builder(_attr: TokenStream, item: TokenStream) -> TokenStream {
}; };
expanded.into() 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 # https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
# Opcional. Para configurar servicios usando las macros de Actix Web:
actix-web = "4" 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: # Opcional. Para usar archivos y recursos binarios contenidos en el ejecutable:
static-files = "0.2.3" static-files = "0.2.3"
# Opcional. Para serializar estructuras de datos: # Opcional. Para serializar estructuras de datos:
serde = { version = "1.0", features = ["derive"] } 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] [build-dependencies]
# Opcional. Para incluir archivos y recursos binarios en el ejecutable: # Opcional. Para incluir archivos y recursos binarios en el ejecutable:
pagetop-build = "0.0" pagetop-build = "0.0"

View file

@ -7,15 +7,15 @@ edition = "2021"
# https://doc.rust-lang.org/cargo/reference/manifest.html # https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [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: # Opcional. Para usar archivos y recursos binarios contenidos en la librería:
static-files = "0.2.3" static-files = "0.2.3"
# Opcional. Para serializar estructuras de datos: # Opcional. Para serializar estructuras de datos:
serde = { version = "1.0", features = ["derive"] } 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] [build-dependencies]
# Opcional. Para incluir archivos y recursos binarios en la propia librería: # Opcional. Para incluir archivos y recursos binarios en la propia librería:
pagetop-build = "0.0" pagetop-build = "0.0"

View file

@ -52,18 +52,17 @@
//! } //! }
//! //!
//! fn configure_service(&self, cfg: &mut service::web::ServiceConfig) { //! 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> { //! async fn hello_world(request: service::HttpRequest) -> ResultPage<Markup, FatalError> {
//! Page::new(request) //! Page::new(request)
//! .with_in("content", Html::with(html! { h1 { "Hello World!" } })) //! .with_in("content", Html::with(html! { h1 { "Hello World!" } }))
//! .render() //! .render()
//! } //! }
//! //!
//! #[actix_web::main] //! #[pagetop::main]
//! async fn main() -> std::io::Result<()> { //! async fn main() -> std::io::Result<()> {
//! Application::prepare(&HelloWorld).unwrap().run()?.await //! 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. /// Enables flexible identifier concatenation in macros, allowing new items with pasted identifiers.
pub use paste::paste; pub use paste::paste;
pub use pagetop_macros::fn_builder; pub use pagetop_macros::{fn_builder, main, test};
// ************************************************************************************************* // *************************************************************************************************
// GLOBAL. // GLOBAL.

View file

@ -1,7 +1,7 @@
//! The PageTop Prelude. //! The PageTop Prelude.
// Re-exported macros. // Re-exported macros.
pub use crate::{concat_string, fn_builder, paste}; pub use crate::{concat_string, fn_builder, main, paste, test};
// Global. // Global.
pub use crate::{Handle, HashMapResources, LazyStatic, ResultExt, Weight}; pub use crate::{Handle, HashMapResources, LazyStatic, ResultExt, Weight};

View file

@ -2,7 +2,7 @@
pub use actix_session::Session; pub use actix_session::Session;
pub use actix_web::{ 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; pub use actix_web_files::Files as ActixFiles;

View file

@ -7,12 +7,11 @@ authors = [
"Manuel Cillero <manuel@cillero.es>" "Manuel Cillero <manuel@cillero.es>"
] ]
description = """\ description = """\
Test for PageTop.\ Tests for PageTop.\
""" """
homepage = "https://pagetop.cillero.es" homepage = "https://pagetop.cillero.es"
repository = "https://github.com/manuelcillero/pagetop" repository = "https://github.com/manuelcillero/pagetop"
license = "Apache-2.0 OR MIT" license = "Apache-2.0 OR MIT"
[dependencies] [dependencies]
actix-web = "4"
pagetop = { version = "0.0", path = "../pagetop", features = ["mysql"], default-features = false } pagetop = { version = "0.0", path = "../pagetop", features = ["mysql"], default-features = false }