1- use cargo_metadata:: { Dependency , DependencyBuilder , DependencyKind } ;
1+ use cargo_metadata:: { Dependency , DependencyKind } ;
22use derive_more:: Deref ;
33use semver:: VersionReq ;
44use serde:: { Deserialize , Serialize } ;
55
6- /// The three possible representations of a dependency in our internal JSON format
7- /// in the `releases.dependencies` column.
8- #[ derive( Debug , Clone , Serialize , Deserialize ) ]
9- #[ serde( untagged) ]
10- enum Dep {
11- Two ( ( String , VersionReq ) ) ,
12- Three ( ( String , VersionReq , DependencyKind ) ) ,
13- Four ( ( String , VersionReq , DependencyKind , bool ) ) ,
6+ /// A crate dependency in our internal representation for releases.dependencies json.
7+ #[ derive( Debug , Clone , PartialEq ) ]
8+ pub ( crate ) struct ReleaseDependency {
9+ pub ( crate ) name : String ,
10+ pub ( crate ) req : VersionReq ,
11+ pub ( crate ) kind : DependencyKind ,
12+ pub ( crate ) optional : bool ,
1413}
1514
16- // FIXME: open question: should we use `cargo_metadata::Dependency` here? We would insert
17- // many default values like default-features, registry, rename, etc,
18- // that we don't have in the database.
19-
20- /// A crate dependency in our internal representation for releases.dependencies json.
21- #[ derive( Debug , Clone , Serialize , Deserialize , PartialEq , Deref ) ]
22- #[ serde( from = "Dep" , into = "Dep" ) ]
23- pub ( crate ) struct ReleaseDependency ( Dependency ) ;
15+ impl < ' de > Deserialize < ' de > for ReleaseDependency {
16+ fn deserialize < D > ( deserializer : D ) -> Result < Self , D :: Error >
17+ where
18+ D : serde:: Deserializer < ' de > ,
19+ {
20+ /// The three possible representations of a dependency in our internal JSON format
21+ /// in the `releases.dependencies` column.
22+ #[ derive( Serialize , Deserialize ) ]
23+ #[ serde( untagged) ]
24+ enum Repr {
25+ /// just [name, version]``
26+ Basic ( ( String , VersionReq ) ) ,
27+ /// [name, version, kind]
28+ WithKind ( ( String , VersionReq , DependencyKind ) ) ,
29+ /// [name, version, kind, optional]
30+ Full ( ( String , VersionReq , DependencyKind , bool ) ) ,
31+ }
2432
25- impl From < Dep > for ReleaseDependency {
26- fn from ( src : Dep ) -> Self {
33+ let src = Repr :: deserialize ( deserializer) ?;
2734 let ( name, req, kind, optional) = match src {
28- Dep :: Two ( ( name, req) ) => ( name, req, DependencyKind :: default ( ) , false ) ,
29- Dep :: Three ( ( name, req, kind) ) => ( name, req, kind, false ) ,
30- Dep :: Four ( ( name, req, kind, optional) ) => ( name, req, kind, optional) ,
35+ Repr :: Basic ( ( name, req) ) => ( name, req, DependencyKind :: default ( ) , false ) ,
36+ Repr :: WithKind ( ( name, req, kind) ) => ( name, req, kind, false ) ,
37+ Repr :: Full ( ( name, req, kind, optional) ) => ( name, req, kind, optional) ,
3138 } ;
3239
33- ReleaseDependency (
34- DependencyBuilder :: default ( )
35- . name ( name)
36- . source ( None )
37- . req ( req)
38- . kind ( kind)
39- . optional ( optional)
40- . uses_default_features ( false )
41- . features ( vec ! [ ] )
42- . target ( None )
43- . rename ( None )
44- . registry ( None )
45- . path ( None )
46- . build ( )
47- . expect ( "we know the data is correct" ) ,
48- )
40+ Ok ( ReleaseDependency {
41+ name,
42+ req,
43+ kind,
44+ optional,
45+ } )
4946 }
5047}
5148
52- impl From < ReleaseDependency > for Dep {
53- // dependency serialization for new releases.
54- fn from ( rd : ReleaseDependency ) -> Self {
55- let d = rd. 0 ;
56- Dep :: Four ( ( d. name , d. req , d. kind , d. optional ) )
49+ impl Serialize for ReleaseDependency {
50+ fn serialize < S > ( & self , serializer : S ) -> Result < S :: Ok , S :: Error >
51+ where
52+ S : serde:: Serializer ,
53+ {
54+ ( self . name . as_str ( ) , & self . req , & self . kind , self . optional ) . serialize ( serializer)
5755 }
5856}
5957
@@ -66,15 +64,24 @@ where
6664 I : IntoIterator < Item = Dependency > ,
6765{
6866 fn from ( deps : I ) -> Self {
69- Self ( deps. into_iter ( ) . map ( ReleaseDependency ) . collect ( ) )
67+ Self (
68+ deps. into_iter ( )
69+ . map ( |dep| ReleaseDependency {
70+ name : dep. name ,
71+ req : dep. req ,
72+ kind : dep. kind ,
73+ optional : dep. optional ,
74+ } )
75+ . collect ( ) ,
76+ )
7077 }
7178}
7279
73- impl ReleaseDependencyList {
74- pub ( crate ) fn into_iter_dependencies ( self ) -> impl Iterator < Item = Dependency > {
75- self . 0 . into_iter ( ) . map ( |rd| rd. 0 )
76- }
77- }
80+ // impl ReleaseDependencyList {
81+ // pub(crate) fn into_iter_dependencies(self) -> impl Iterator<Item = Dependency> {
82+ // self.0.into_iter().map(|rd| rd.0)
83+ // }
84+ // }
7885
7986#[ cfg( test) ]
8087mod tests {
0 commit comments