derive变更为macro
This commit is contained in:
parent
91aec74a56
commit
a488c82067
30
Cargo.lock
generated
30
Cargo.lock
generated
@ -484,19 +484,6 @@ dependencies = [
|
||||
"powerfmt",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "derive"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"axum",
|
||||
"hyper",
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"serde",
|
||||
"serde_json",
|
||||
"syn 2.0.77",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "digest"
|
||||
version = "0.10.7"
|
||||
@ -515,9 +502,9 @@ version = "0.1.0"
|
||||
dependencies = [
|
||||
"axum",
|
||||
"chrono",
|
||||
"derive",
|
||||
"hyper",
|
||||
"i18n",
|
||||
"macro",
|
||||
"serde",
|
||||
"serde_json",
|
||||
"sqlx",
|
||||
@ -1191,6 +1178,19 @@ version = "0.4.22"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a7a70ba024b9dc04c27ea2f0c0548feb474ec5c54bba33a7f72f873a39d07b24"
|
||||
|
||||
[[package]]
|
||||
name = "macro"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"axum",
|
||||
"hyper",
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"serde",
|
||||
"serde_json",
|
||||
"syn 2.0.77",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "matchit"
|
||||
version = "0.7.3"
|
||||
@ -2046,13 +2046,13 @@ dependencies = [
|
||||
"axum",
|
||||
"axum-extra",
|
||||
"chrono",
|
||||
"derive",
|
||||
"domain",
|
||||
"error-stack",
|
||||
"futures-executor",
|
||||
"i18n",
|
||||
"lazy_static",
|
||||
"library",
|
||||
"macro",
|
||||
"moka",
|
||||
"reqwest",
|
||||
"sqlx",
|
||||
|
@ -4,7 +4,7 @@ version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[workspace]
|
||||
members = [".", "server", "domain", "i18n","library", "derive"]
|
||||
members = [".", "server", "domain", "i18n","library", "macro"]
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
|
@ -18,4 +18,4 @@ hyper = { workspace = true }
|
||||
axum = { workspace = true }
|
||||
|
||||
i18n = { path = "../i18n" }
|
||||
derive = { path = "../derive" }
|
||||
macro = { path = "../macro" }
|
@ -10,4 +10,5 @@ pub mod context;
|
||||
pub mod task;
|
||||
pub mod utils;
|
||||
pub mod extractor;
|
||||
pub mod router;
|
||||
pub mod router;
|
||||
pub mod typed_router;
|
33
library/src/typed_router.rs
Normal file
33
library/src/typed_router.rs
Normal file
@ -0,0 +1,33 @@
|
||||
use axum::routing::MethodRouter;
|
||||
|
||||
type TypedHandler<S = ()> = fn() -> (&'static str, MethodRouter<S>);
|
||||
|
||||
/// A trait that allows typed routes, created with the [`route`] macro to
|
||||
/// be added to an axum router.
|
||||
///
|
||||
/// Typed handlers are of the form `fn() -> (&'static str, MethodRouter<S>)`, where
|
||||
/// `S` is the state type. The first element of the tuple is the path, and the second
|
||||
/// is the method router.
|
||||
pub trait TypedRouter: Sized {
|
||||
/// The state type of the router.
|
||||
type State: Clone + Send + Sync + 'static;
|
||||
|
||||
/// Add a typed route to the router, usually created with the [`route`] macro.
|
||||
///
|
||||
/// Typed handlers are of the form `fn() -> (&'static str, MethodRouter<S>)`, where
|
||||
/// `S` is the state type. The first element of the tuple is the path, and the second
|
||||
/// is the method router.
|
||||
fn typed_route(self, handler: TypedHandler<Self::State>) -> Self;
|
||||
}
|
||||
|
||||
impl<S> TypedRouter for axum::Router<S>
|
||||
where
|
||||
S: Send + Sync + Clone + 'static,
|
||||
{
|
||||
type State = S;
|
||||
|
||||
fn typed_route(self, handler: TypedHandler<Self::State>) -> Self {
|
||||
let (path, method_router) = handler();
|
||||
self.route(path, method_router)
|
||||
}
|
||||
}
|
@ -1,5 +1,5 @@
|
||||
[package]
|
||||
name = "derive"
|
||||
name = "macro"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
@ -25,4 +25,4 @@ tower = { workspace = true }
|
||||
library = { path = "../library" }
|
||||
domain = { path = "../domain" }
|
||||
i18n = { path = "../i18n" }
|
||||
derive = { path = "../derive" }
|
||||
macro = { path = "../macro" }
|
Loading…
Reference in New Issue
Block a user