66
77use crate :: app:: AppState ;
88use crate :: controllers:: helpers:: pagination:: PaginationOptions ;
9+ use crate :: controllers:: krate:: CratePath ;
10+ use crate :: controllers:: version:: CrateVersionPath ;
911use crate :: models:: {
1012 Category , Crate , CrateCategory , CrateKeyword , CrateName , Keyword , RecentCrateDownloads , User ,
1113 Version , VersionOwnerAction ,
@@ -16,7 +18,6 @@ use crate::util::{redirect, RequestUtils};
1618use crate :: views:: {
1719 EncodableCategory , EncodableCrate , EncodableDependency , EncodableKeyword , EncodableVersion ,
1820} ;
19- use axum:: extract:: Path ;
2021use axum:: response:: { IntoResponse , Response } ;
2122use axum_extra:: json;
2223use axum_extra:: response:: ErasedJson ;
@@ -37,21 +38,19 @@ use std::str::FromStr;
3738 responses( ( status = 200 , description = "Successful Response" ) ) ,
3839) ]
3940pub async fn find_new_crate ( app : AppState , req : Parts ) -> AppResult < ErasedJson > {
40- find_crate ( app, Path ( "new" . to_string ( ) ) , req) . await
41+ let name = "new" . to_string ( ) ;
42+ find_crate ( app, CratePath { name } , req) . await
4143}
4244
4345/// Get crate metadata.
4446#[ utoipa:: path(
4547 get,
4648 path = "/api/v1/crates/{name}" ,
49+ params( CratePath ) ,
4750 tag = "crates" ,
4851 responses( ( status = 200 , description = "Successful Response" ) ) ,
4952) ]
50- pub async fn find_crate (
51- app : AppState ,
52- Path ( name) : Path < String > ,
53- req : Parts ,
54- ) -> AppResult < ErasedJson > {
53+ pub async fn find_crate ( app : AppState , path : CratePath , req : Parts ) -> AppResult < ErasedJson > {
5554 let mut conn = app. db_read ( ) . await ?;
5655
5756 let include = req
@@ -62,7 +61,7 @@ pub async fn find_crate(
6261 . unwrap_or_default ( ) ;
6362
6463 let ( krate, downloads, default_version, yanked) : ( Crate , i64 , Option < String > , Option < bool > ) =
65- Crate :: by_name ( & name)
64+ Crate :: by_name ( & path . name )
6665 . inner_join ( crate_downloads:: table)
6766 . left_join ( default_versions:: table)
6867 . left_join ( versions:: table. on ( default_versions:: version_id. eq ( versions:: id) ) )
@@ -75,7 +74,7 @@ pub async fn find_crate(
7574 . first ( & mut conn)
7675 . await
7776 . optional ( ) ?
78- . ok_or_else ( || crate_not_found ( & name) ) ?;
77+ . ok_or_else ( || crate_not_found ( & path . name ) ) ?;
7978
8079 let versions_publishers_and_audit_actions = if include. versions {
8180 let mut versions_and_publishers: Vec < ( Version , Option < User > ) > =
@@ -250,15 +249,12 @@ impl FromStr for ShowIncludeMode {
250249#[ utoipa:: path(
251250 get,
252251 path = "/api/v1/crates/{name}/{version}/readme" ,
252+ params( CrateVersionPath ) ,
253253 tag = "versions" ,
254254 responses( ( status = 200 , description = "Successful Response" ) ) ,
255255) ]
256- pub async fn get_version_readme (
257- app : AppState ,
258- Path ( ( crate_name, version) ) : Path < ( String , String ) > ,
259- req : Parts ,
260- ) -> Response {
261- let redirect_url = app. storage . readme_location ( & crate_name, & version) ;
256+ pub async fn get_version_readme ( app : AppState , path : CrateVersionPath , req : Parts ) -> Response {
257+ let redirect_url = app. storage . readme_location ( & path. name , & path. version ) ;
262258 if req. wants_json ( ) {
263259 json ! ( { "url" : redirect_url } ) . into_response ( )
264260 } else {
@@ -270,23 +266,20 @@ pub async fn get_version_readme(
270266#[ utoipa:: path(
271267 get,
272268 path = "/api/v1/crates/{name}/reverse_dependencies" ,
269+ params( CratePath ) ,
273270 tag = "crates" ,
274271 responses( ( status = 200 , description = "Successful Response" ) ) ,
275272) ]
276273pub async fn list_reverse_dependencies (
277274 app : AppState ,
278- Path ( name ) : Path < String > ,
275+ path : CratePath ,
279276 req : Parts ,
280277) -> AppResult < ErasedJson > {
281278 let mut conn = app. db_read ( ) . await ?;
282279
283280 let pagination_options = PaginationOptions :: builder ( ) . gather ( & req) ?;
284281
285- let krate: Crate = Crate :: by_name ( & name)
286- . first ( & mut conn)
287- . await
288- . optional ( ) ?
289- . ok_or_else ( || crate_not_found ( & name) ) ?;
282+ let krate = path. load_crate ( & mut conn) . await ?;
290283
291284 let ( rev_deps, total) = krate
292285 . reverse_dependencies ( & mut conn, pagination_options)
0 commit comments