@@ -4,13 +4,13 @@ use crate::{
44 error:: Result ,
55 registry_api:: { CrateData , CrateOwner , ReleaseData } ,
66 storage:: CompressionAlgorithm ,
7- utils:: { MetadataPackage , rustc_version:: parse_rustc_date} ,
7+ utils:: { Dependency , MetadataPackage , rustc_version:: parse_rustc_date} ,
88 web:: crate_details:: { latest_release, releases_for_crate} ,
99} ;
1010use anyhow:: { Context , anyhow} ;
11- use derive_more:: Display ;
11+ use derive_more:: { Deref , Display } ;
1212use futures_util:: stream:: TryStreamExt ;
13- use serde:: Serialize ;
13+ use serde:: { Deserialize , Serialize } ;
1414use serde_json:: Value ;
1515use slug:: slugify;
1616use std:: {
@@ -33,6 +33,44 @@ pub struct ReleaseId(pub i32);
3333#[ sqlx( transparent) ]
3434pub struct BuildId ( pub i32 ) ;
3535
36+ type DepOut = ( String , String , String , bool ) ;
37+ type DepIn = ( String , String , Option < String > , Option < bool > ) ;
38+
39+ /// A crate dependency in our internal representation for releases.dependencies json.
40+ #[ derive( Debug , Clone , Serialize , Deserialize , PartialEq , Deref ) ]
41+ #[ serde( from = "DepIn" , into = "DepOut" ) ]
42+ pub ( crate ) struct ReleaseDependency ( Dependency ) ;
43+
44+ impl ReleaseDependency {
45+ pub ( crate ) fn into_inner ( self ) -> Dependency {
46+ self . 0
47+ }
48+ }
49+
50+ impl From < DepIn > for ReleaseDependency {
51+ fn from ( ( name, req, kind, optional) : DepIn ) -> Self {
52+ ReleaseDependency ( Dependency {
53+ name,
54+ req,
55+ kind,
56+ optional : optional. unwrap_or ( false ) ,
57+ rename : None ,
58+ } )
59+ }
60+ }
61+
62+ impl From < ReleaseDependency > for DepOut {
63+ fn from ( rd : ReleaseDependency ) -> Self {
64+ let d = rd. 0 ;
65+ (
66+ d. name ,
67+ d. req . to_string ( ) ,
68+ d. kind . unwrap_or_else ( || "normal" . into ( ) ) ,
69+ d. optional ,
70+ )
71+ }
72+ }
73+
3674/// Adds a package into database.
3775///
3876/// Package must be built first.
@@ -59,7 +97,7 @@ pub(crate) async fn finish_release(
5997 source_size : u64 ,
6098) -> Result < ( ) > {
6199 debug ! ( "updating release data" ) ;
62- let dependencies = convert_dependencies ( metadata_pkg) ;
100+ let dependencies = convert_dependencies ( metadata_pkg) ? ;
63101 let rustdoc = get_rustdoc ( metadata_pkg, source_dir) . unwrap_or ( None ) ;
64102 let readme = get_readme ( metadata_pkg, source_dir) . unwrap_or ( None ) ;
65103 let features = get_features ( metadata_pkg) ;
@@ -94,7 +132,7 @@ pub(crate) async fn finish_release(
94132 WHERE id = $1"# ,
95133 release_id. 0 ,
96134 registry_data. release_time,
97- serde_json :: to_value ( dependencies) ? ,
135+ dependencies,
98136 metadata_pkg. package_name( ) ,
99137 registry_data. yanked,
100138 has_docs,
@@ -393,20 +431,14 @@ pub(crate) async fn initialize_build(
393431 Ok ( build_id)
394432}
395433
396- /// Convert dependencies into Vec<(String, String, String, bool)>
397- fn convert_dependencies ( pkg : & MetadataPackage ) -> Vec < ( String , String , String , bool ) > {
398- pkg. dependencies
434+ /// Convert dependencies into our own internal JSON representation
435+ fn convert_dependencies ( pkg : & MetadataPackage ) -> Result < serde_json:: Value > {
436+ let dependencies: Vec < _ > = pkg
437+ . dependencies
399438 . iter ( )
400- . map ( |dependency| {
401- let name = dependency. name . clone ( ) ;
402- let version = dependency. req . clone ( ) ;
403- let kind = dependency
404- . kind
405- . clone ( )
406- . unwrap_or_else ( || "normal" . to_string ( ) ) ;
407- ( name, version, kind, dependency. optional )
408- } )
409- . collect ( )
439+ . map ( |dependency| ReleaseDependency ( dependency. clone ( ) ) )
440+ . collect :: < Vec < _ > > ( ) ;
441+ Ok ( serde_json:: to_value ( dependencies) ?)
410442}
411443
412444/// Reads features and converts them to Vec<Feature> with default being first
0 commit comments