From a488c8206735fa583297839bef641b750c8bfd65 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E8=BF=90=E5=AE=B6?= Date: Wed, 2 Oct 2024 09:08:31 +0800 Subject: [PATCH] =?UTF-8?q?derive=E5=8F=98=E6=9B=B4=E4=B8=BAmacro?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Cargo.lock | 30 +++++++++++++++--------------- Cargo.toml | 2 +- domain/Cargo.toml | 2 +- library/src/lib.rs | 3 ++- library/src/typed_router.rs | 33 +++++++++++++++++++++++++++++++++ {derive => macro}/Cargo.toml | 2 +- {derive => macro}/README.MD | 0 {derive => macro}/src/lib.rs | 0 server/Cargo.toml | 2 +- 9 files changed, 54 insertions(+), 20 deletions(-) create mode 100644 library/src/typed_router.rs rename {derive => macro}/Cargo.toml (96%) rename {derive => macro}/README.MD (100%) rename {derive => macro}/src/lib.rs (100%) diff --git a/Cargo.lock b/Cargo.lock index 828bda5..bbb924d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -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", diff --git a/Cargo.toml b/Cargo.toml index 5d01a78..865ee38 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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 diff --git a/domain/Cargo.toml b/domain/Cargo.toml index 2e6d20a..2d29b9f 100644 --- a/domain/Cargo.toml +++ b/domain/Cargo.toml @@ -18,4 +18,4 @@ hyper = { workspace = true } axum = { workspace = true } i18n = { path = "../i18n" } -derive = { path = "../derive" } \ No newline at end of file +macro = { path = "../macro" } \ No newline at end of file diff --git a/library/src/lib.rs b/library/src/lib.rs index 9d7bb66..67c672f 100644 --- a/library/src/lib.rs +++ b/library/src/lib.rs @@ -10,4 +10,5 @@ pub mod context; pub mod task; pub mod utils; pub mod extractor; -pub mod router; \ No newline at end of file +pub mod router; +pub mod typed_router; \ No newline at end of file diff --git a/library/src/typed_router.rs b/library/src/typed_router.rs new file mode 100644 index 0000000..fd35d8f --- /dev/null +++ b/library/src/typed_router.rs @@ -0,0 +1,33 @@ +use axum::routing::MethodRouter; + +type TypedHandler = fn() -> (&'static str, MethodRouter); + +/// 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)`, 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)`, 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; +} + +impl TypedRouter for axum::Router +where + S: Send + Sync + Clone + 'static, +{ + type State = S; + + fn typed_route(self, handler: TypedHandler) -> Self { + let (path, method_router) = handler(); + self.route(path, method_router) + } +} \ No newline at end of file diff --git a/derive/Cargo.toml b/macro/Cargo.toml similarity index 96% rename from derive/Cargo.toml rename to macro/Cargo.toml index f570c6f..eeb47eb 100644 --- a/derive/Cargo.toml +++ b/macro/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "derive" +name = "macro" version = "0.1.0" edition = "2021" diff --git a/derive/README.MD b/macro/README.MD similarity index 100% rename from derive/README.MD rename to macro/README.MD diff --git a/derive/src/lib.rs b/macro/src/lib.rs similarity index 100% rename from derive/src/lib.rs rename to macro/src/lib.rs diff --git a/server/Cargo.toml b/server/Cargo.toml index b608e53..bdebb34 100644 --- a/server/Cargo.toml +++ b/server/Cargo.toml @@ -25,4 +25,4 @@ tower = { workspace = true } library = { path = "../library" } domain = { path = "../domain" } i18n = { path = "../i18n" } -derive = { path = "../derive" } \ No newline at end of file +macro = { path = "../macro" } \ No newline at end of file