6
6
7
7
use crate :: app:: AppState ;
8
8
use crate :: controllers:: helpers:: pagination:: PaginationOptions ;
9
+ use crate :: controllers:: krate:: CratePath ;
10
+ use crate :: controllers:: version:: CrateVersionPath ;
9
11
use crate :: models:: {
10
12
Category , Crate , CrateCategory , CrateKeyword , CrateName , Keyword , RecentCrateDownloads , User ,
11
13
Version , VersionOwnerAction ,
@@ -16,7 +18,6 @@ use crate::util::{redirect, RequestUtils};
16
18
use crate :: views:: {
17
19
EncodableCategory , EncodableCrate , EncodableDependency , EncodableKeyword , EncodableVersion ,
18
20
} ;
19
- use axum:: extract:: Path ;
20
21
use axum:: response:: { IntoResponse , Response } ;
21
22
use axum_extra:: json;
22
23
use axum_extra:: response:: ErasedJson ;
@@ -37,21 +38,19 @@ use std::str::FromStr;
37
38
responses( ( status = 200 , description = "Successful Response" ) ) ,
38
39
) ]
39
40
pub 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
41
43
}
42
44
43
45
/// Get crate metadata.
44
46
#[ utoipa:: path(
45
47
get,
46
48
path = "/api/v1/crates/{name}" ,
49
+ params( CratePath ) ,
47
50
tag = "crates" ,
48
51
responses( ( status = 200 , description = "Successful Response" ) ) ,
49
52
) ]
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 > {
55
54
let mut conn = app. db_read ( ) . await ?;
56
55
57
56
let include = req
@@ -62,7 +61,7 @@ pub async fn find_crate(
62
61
. unwrap_or_default ( ) ;
63
62
64
63
let ( krate, downloads, default_version, yanked) : ( Crate , i64 , Option < String > , Option < bool > ) =
65
- Crate :: by_name ( & name)
64
+ Crate :: by_name ( & path . name )
66
65
. inner_join ( crate_downloads:: table)
67
66
. left_join ( default_versions:: table)
68
67
. left_join ( versions:: table. on ( default_versions:: version_id. eq ( versions:: id) ) )
@@ -75,7 +74,7 @@ pub async fn find_crate(
75
74
. first ( & mut conn)
76
75
. await
77
76
. optional ( ) ?
78
- . ok_or_else ( || crate_not_found ( & name) ) ?;
77
+ . ok_or_else ( || crate_not_found ( & path . name ) ) ?;
79
78
80
79
let versions_publishers_and_audit_actions = if include. versions {
81
80
let mut versions_and_publishers: Vec < ( Version , Option < User > ) > =
@@ -250,15 +249,12 @@ impl FromStr for ShowIncludeMode {
250
249
#[ utoipa:: path(
251
250
get,
252
251
path = "/api/v1/crates/{name}/{version}/readme" ,
252
+ params( CrateVersionPath ) ,
253
253
tag = "versions" ,
254
254
responses( ( status = 200 , description = "Successful Response" ) ) ,
255
255
) ]
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 ) ;
262
258
if req. wants_json ( ) {
263
259
json ! ( { "url" : redirect_url } ) . into_response ( )
264
260
} else {
@@ -270,23 +266,20 @@ pub async fn get_version_readme(
270
266
#[ utoipa:: path(
271
267
get,
272
268
path = "/api/v1/crates/{name}/reverse_dependencies" ,
269
+ params( CratePath ) ,
273
270
tag = "crates" ,
274
271
responses( ( status = 200 , description = "Successful Response" ) ) ,
275
272
) ]
276
273
pub async fn list_reverse_dependencies (
277
274
app : AppState ,
278
- Path ( name ) : Path < String > ,
275
+ path : CratePath ,
279
276
req : Parts ,
280
277
) -> AppResult < ErasedJson > {
281
278
let mut conn = app. db_read ( ) . await ?;
282
279
283
280
let pagination_options = PaginationOptions :: builder ( ) . gather ( & req) ?;
284
281
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 ?;
290
283
291
284
let ( rev_deps, total) = krate
292
285
. reverse_dependencies ( & mut conn, pagination_options)
0 commit comments