derive变更为macro

This commit is contained in:
李运家 2024-10-02 09:08:31 +08:00
parent 91aec74a56
commit a488c82067
9 changed files with 54 additions and 20 deletions

30
Cargo.lock generated
View File

@ -484,19 +484,6 @@ dependencies = [
"powerfmt", "powerfmt",
] ]
[[package]]
name = "derive"
version = "0.1.0"
dependencies = [
"axum",
"hyper",
"proc-macro2",
"quote",
"serde",
"serde_json",
"syn 2.0.77",
]
[[package]] [[package]]
name = "digest" name = "digest"
version = "0.10.7" version = "0.10.7"
@ -515,9 +502,9 @@ version = "0.1.0"
dependencies = [ dependencies = [
"axum", "axum",
"chrono", "chrono",
"derive",
"hyper", "hyper",
"i18n", "i18n",
"macro",
"serde", "serde",
"serde_json", "serde_json",
"sqlx", "sqlx",
@ -1191,6 +1178,19 @@ version = "0.4.22"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a7a70ba024b9dc04c27ea2f0c0548feb474ec5c54bba33a7f72f873a39d07b24" checksum = "a7a70ba024b9dc04c27ea2f0c0548feb474ec5c54bba33a7f72f873a39d07b24"
[[package]]
name = "macro"
version = "0.1.0"
dependencies = [
"axum",
"hyper",
"proc-macro2",
"quote",
"serde",
"serde_json",
"syn 2.0.77",
]
[[package]] [[package]]
name = "matchit" name = "matchit"
version = "0.7.3" version = "0.7.3"
@ -2046,13 +2046,13 @@ dependencies = [
"axum", "axum",
"axum-extra", "axum-extra",
"chrono", "chrono",
"derive",
"domain", "domain",
"error-stack", "error-stack",
"futures-executor", "futures-executor",
"i18n", "i18n",
"lazy_static", "lazy_static",
"library", "library",
"macro",
"moka", "moka",
"reqwest", "reqwest",
"sqlx", "sqlx",

View File

@ -4,7 +4,7 @@ version = "0.1.0"
edition = "2021" edition = "2021"
[workspace] [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 # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

View File

@ -18,4 +18,4 @@ hyper = { workspace = true }
axum = { workspace = true } axum = { workspace = true }
i18n = { path = "../i18n" } i18n = { path = "../i18n" }
derive = { path = "../derive" } macro = { path = "../macro" }

View File

@ -11,3 +11,4 @@ pub mod task;
pub mod utils; pub mod utils;
pub mod extractor; pub mod extractor;
pub mod router; pub mod router;
pub mod typed_router;

View 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)
}
}

View File

@ -1,5 +1,5 @@
[package] [package]
name = "derive" name = "macro"
version = "0.1.0" version = "0.1.0"
edition = "2021" edition = "2021"

View File

@ -25,4 +25,4 @@ tower = { workspace = true }
library = { path = "../library" } library = { path = "../library" }
domain = { path = "../domain" } domain = { path = "../domain" }
i18n = { path = "../i18n" } i18n = { path = "../i18n" }
derive = { path = "../derive" } macro = { path = "../macro" }