#chitey #routing #macro #body #path

过程宏 chitey-codegen

Chitey的路由宏

15个不稳定版本 (3个破坏性更改)

0.4.0 2023年10月8日
0.3.0 2023年10月2日
0.2.19 2023年9月30日
0.1.0 2023年9月26日

#1636过程宏

每月37次下载
用于 chitey

MIT 许可证

28KB
542

备注

宏应用后

cargo install cargo-expand cargo expand

fn main() {
    let body = async {
        {
            ::std::io::_print(format_args!("Hello, world!\n"));
        };
    };
    #[allow(clippy::expect_used, clippy::diverging_sub_expression)]
    {
        return tokio::runtime::Builder::new_multi_thread()
            .enable_all()
            .build()
            .expect("Failed building the Runtime")
            .block_on(body);
    }
}
#[allow(non_camel_case_types, missing_docs)]
pub struct greet;
impl ::chitey::HttpServiceFactory for greet {
    fn register(&self) -> ::chitey::Resource {
        async fn greet(name: String) -> Responder {
            {
                ::std::io::_print(format_args!("Hello {0}!\n", name));
            };
            let builder = Response::builder();
            let ret = Bytes::copy_from_slice(b"source");
            Ok((builder, ret))
        }
        let __resource = ::chitey::Resource::new("/hello/{name}").regist(greet);
        return __resource;
    }
}
#[allow(non_camel_case_types, missing_docs)]
pub struct doubb;
impl ::chitey::HttpServiceFactory for doubb {
    fn register(&self) -> ::chitey::Resource {
        async fn doubb((name, id): (u32, String)) -> Responder {
            {
                let res = ::alloc::fmt::format(
                    format_args!("Hello {0}! id:{1}", name, id),
                );
                res
            };
            let builder = Response::builder();
            let ret = Bytes::copy_from_slice(b"source");
            Ok((builder, ret))
        }
        let __resource = ::chitey::Resource::new("/{id}/{name}").regist(doubb);
        return __resource;
    }
}
#![feature(prelude_import)]
#[prelude_import]
use std::prelude::rust_2021::*;
#[macro_use]
extern crate std;
use actix_web::{get, web, App, HttpServer, Responder};
#[allow(non_camel_case_types, missing_docs)]
pub struct greet;
impl ::actix_web::dev::HttpServiceFactory for greet {
    fn register(self, __config: &mut actix_web::dev::AppService) {
        async fn greet(req: web::Path<(u32, String)>) -> impl Responder {
            let (name, id) = req.to_owned();
            {
                let res = ::alloc::fmt::format(
                    format_args!("Hello {0}! id:{1}", name, id),
                );
                res
            }
        }
        let __resource = ::actix_web::Resource::new("/{id}/{name}")
            .name("greet")
            .guard(::actix_web::guard::Get())
            .to(greet);
        ::actix_web::dev::HttpServiceFactory::register(__resource, __config);
    }
}
fn main() -> std::io::Result<()> {
    <::actix_web::rt::System>::new()
        .block_on(async move {
            {
                HttpServer::new(|| { App::new().service(greet) })
                    .bind("127.0.0.1:8080")?
                    .run()
                    .await
            }
        })
}

依赖

~265–720KB
~17K SLoC