Crates.io | modi_macros |
lib.rs | modi_macros |
version | 0.1.1 |
source | src |
created_at | 2024-09-23 14:20:09.901663 |
updated_at | 2024-09-23 19:12:04.650194 |
description | Code generation for the modi dependency injection framework |
homepage | https://github.com/andeya/modi |
repository | https://github.com/andeya/modi |
max_upload_size | |
id | 1384132 |
size | 53,233 |
An out-of-the-box modular dependency injection web application framework.
Run the following Cargo command in your project directory:
cargo add modi
Or add the following line to your Cargo.toml:
modi = "0.1.0"
#![allow(missing_docs)]
use modi::prelude::*;
use std::sync::Arc;
#[tokio::main]
async fn main() {
new_app![Calculate]
.with_component_parameters::<Calculate>(CalculateParameters { state: 1 })
.run()
.await;
}
pub trait Service: Interface {
fn add(&self, left: i64, right: i64) -> i64;
}
#[derive(Component)]
#[modi(interface = Service)]
#[derive(Clone)]
pub struct Calculate {
state: i64,
}
impl Interface for Calculate {
fn manifest(&self) -> Manifest {
Manifest::default()
.with_name("Calculate".to_owned())
.with_summary("Calculate. For example, addition.".to_owned())
}
fn route(self: Arc<Self>, blank_router: Router) -> Router {
blank_router
.path("calculate")
.push(Router::with_path("add1").get(self.add1()))
.push(Router::with_path("add2").get(self.add2()))
.push(Router::with_path("add3").get(Self::add3()))
}
}
impl Service for Calculate {
fn add(&self, left: i64, right: i64) -> i64 {
{ self.state + left + right }
.instrument(info_span!("add"))
.into_inner()
}
}
#[craft]
impl Calculate {
/// doc line 1
/// doc line 2
#[craft(handler)]
fn add1(&self, left: QueryParam<i64, true>, right: QueryParam<i64, true>) -> String {
self.add(*left, *right).to_string()
}
/// doc line 3
/// doc line 4
#[craft(endpoint)]
pub(crate) fn add2(
self: ::std::sync::Arc<Self>,
left: QueryParam<i64, true>,
right: QueryParam<i64, true>,
) -> String {
self.add(*left, *right).to_string()
}
/// doc line 5
/// doc line 6
#[craft(endpoint(responses((status_code = 400, description = "Wrong request parameters."))))]
pub fn add3(left: QueryParam<i64, true>, right: QueryParam<i64, true>) -> String {
(*left + *right).to_string()
}
}
Browse http://localhost:8888/swagger-ui
and open the OpenAPI online test.