salvo-craft-macros

Crates.iosalvo-craft-macros
lib.rssalvo-craft-macros
version
sourcesrc
created_at2024-09-23 03:21:13.745111+00
updated_at2025-03-05 00:53:59.390041+00
descriptionSalvo Handler modular craft macros.
homepagehttps://salvo.rs
repositoryhttps://github.com/salvo-rs/salvo
max_upload_size
id1383493
Cargo.toml error:TOML parse error at line 19, column 1 | 19 | autolib = false | ^^^^^^^ unknown field `autolib`, expected one of `name`, `version`, `edition`, `authors`, `description`, `readme`, `license`, `repository`, `homepage`, `documentation`, `build`, `resolver`, `links`, `default-run`, `default_dash_run`, `rust-version`, `rust_dash_version`, `rust_version`, `license-file`, `license_dash_file`, `license_file`, `licenseFile`, `license_capital_file`, `forced-target`, `forced_dash_target`, `autobins`, `autotests`, `autoexamples`, `autobenches`, `publish`, `metadata`, `keywords`, `categories`, `exclude`, `include`
size0
Maintainers (github:salvo-rs:maintainers)

documentation

https://docs.rs/salvo-craft-macros

README

Salvo

English   简体中文   繁體中文

build status build status build status
crates.io Documentation unsafe forbidden Rust Version
Website codecov Download License

Salvo is an extremely simple and powerful Rust web backend framework. Only basic Rust knowledge is required to develop backend services.

salvo-craft-macros

Salvo Handler modular craft macros.

#[craft]

#[craft] is an attribute macro that converts methods in an impl block into Salvo's Handler implementations.

use salvo::oapi::extract::*;
use salvo::prelude::*;
use salvo_craft_macros::craft;
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,
}

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

Note: #[craft(handler)] can be replaced with #[craft(endpoint(...))] for more configuration options.

NOTE: When using &self as the method receiver, the containing type must implement the Clone trait.

Documentation & Resources

☕ Donate

Salvo is an open source project. If you want to support Salvo, you can ☕ buy me a coffee here.

⚠️ License

Salvo is licensed under either of

Commit count: 3219

cargo fmt