salvo_macro_ext

Crates.iosalvo_macro_ext
lib.rssalvo_macro_ext
version0.1.3
sourcesrc
created_at2024-09-22 08:25:21.714349
updated_at2024-09-22 13:09:26.94171
descriptionUnofficial extension macros for the salvo web framework.
homepagehttps://github.com/andeya/salvo_macro_ext
repositoryhttps://github.com/andeya/salvo_macro_ext
max_upload_size
id1382804
size83,675
Andeya (andeya)

documentation

https://docs.rs/salvo_macro_ext

README

salvo_macro_ext

Unofficial extension macros for the Salvo web framework.

Crates.io Documentation License

Features

Example

[dependencies]
salvo_macro_ext = "0.1"
salvo = { version = "0.72", features = ["oapi"] }
tokio = "1"

#[module]

#[module] is an attribute macro used to batch convert methods in an impl block into Salvo's Handler.

use salvo::oapi::extract::*;
use salvo::prelude::*;
use salvo_macro_ext::module;
use std::sync::Arc;

#[tokio::main]
async fn main() {
    let service = Arc::new(Service::new(1));
    let router = Router::new()
        .push(Router::with_path("add1").get(service.add1()))
        .push(Router::with_path("add2").get(service.add2()))
        .push(Router::with_path("add3").get(Service::add3()));
    let acceptor = TcpListener::new("127.0.0.1:5800").bind().await;
    Server::new(acceptor).serve(router).await;
}

#[derive(Clone)]
pub struct Service {
    state: i64,
}

#[module]
impl Service {
    fn new(state: i64) -> Self {
        Self { state }
    }
    /// doc line 1
    /// doc line 2
    #[salvo_macro_ext::module(handler)]
    fn add1(&self, left: QueryParam<i64, true>, right: QueryParam<i64, true>) -> String {
        (self.state + *left + *right).to_string()
    }
    /// doc line 3
    /// doc line 4
    #[module(handler)]
    pub(crate) fn add2(
        self: ::std::sync::Arc<Self>,
        left: QueryParam<i64, true>,
        right: QueryParam<i64, true>,
    ) -> String {
        (self.state + *left + *right).to_string()
    }
    /// doc line 5
    /// doc line 6
    #[module(handler)]
    pub fn add3(left: QueryParam<i64, true>, right: QueryParam<i64, true>) -> String {
        (*left + *right).to_string()
    }
}

Sure, you can also replace #[module(handler)] with #[module(endpoint(...))].

NOTE: If the receiver of a method is &self, you need to implement the Clone trait for the type.

Commit count: 13

cargo fmt