11use crate :: db:: { AsyncPoolClient , Pool } ;
22use anyhow:: Context as _;
33use axum:: {
4- async_trait,
5- extract:: { Extension , FromRequestParts } ,
4+ extract:: { Extension , FromRequestParts , OptionalFromRequestParts } ,
65 http:: request:: Parts ,
76 RequestPartsExt ,
87} ;
@@ -20,7 +19,6 @@ use super::error::AxumNope;
2019#[ derive( Debug ) ]
2120pub ( crate ) struct DbConnection ( AsyncPoolClient ) ;
2221
23- #[ async_trait]
2422impl < S > FromRequestParts < S > for DbConnection
2523where
2624 S : Send + Sync ,
@@ -55,11 +53,33 @@ impl DerefMut for DbConnection {
5553/// as error response instead of a plain text "bad request"
5654#[ allow( clippy:: disallowed_types) ]
5755mod path_impl {
56+ use serde:: de:: DeserializeOwned ;
57+
5858 use super :: * ;
5959
6060 #[ derive( FromRequestParts ) ]
6161 #[ from_request( via( axum:: extract:: Path ) , rejection( AxumNope ) ) ]
6262 pub ( crate ) struct Path < T > ( pub T ) ;
63+
64+ impl < T , S > OptionalFromRequestParts < S > for Path < T >
65+ where
66+ T : DeserializeOwned + Send + ' static ,
67+ S : Send + Sync ,
68+ {
69+ type Rejection = AxumNope ;
70+
71+ async fn from_request_parts (
72+ parts : & mut Parts ,
73+ _state : & S ,
74+ ) -> Result < Option < Self > , Self :: Rejection > {
75+ <axum:: extract:: Path < T > as OptionalFromRequestParts < S > >:: from_request_parts (
76+ parts, _state,
77+ )
78+ . await
79+ . map ( |path| path. map ( |obj| Path ( obj. 0 ) ) )
80+ . map_err ( |err| AxumNope :: BadRequest ( err. into ( ) ) )
81+ }
82+ }
6383}
6484
6585pub ( crate ) use path_impl:: Path ;
0 commit comments