@@ -38,7 +38,7 @@ pub struct CrateDetails {
38
38
keywords : Option < Json > ,
39
39
have_examples : bool , // need to check this manually
40
40
pub target_name : String ,
41
- pub versions : Vec < String > ,
41
+ releases : Vec < Release > ,
42
42
github : bool , // is crate hosted in github
43
43
github_stars : Option < i32 > ,
44
44
github_forks : Option < i32 > ,
@@ -77,7 +77,7 @@ impl ToJson for CrateDetails {
77
77
m. insert ( "keywords" . to_string ( ) , self . keywords . to_json ( ) ) ;
78
78
m. insert ( "have_examples" . to_string ( ) , self . have_examples . to_json ( ) ) ;
79
79
m. insert ( "target_name" . to_string ( ) , self . target_name . to_json ( ) ) ;
80
- m. insert ( "versions " . to_string ( ) , self . versions . to_json ( ) ) ;
80
+ m. insert ( "releases " . to_string ( ) , self . releases . to_json ( ) ) ;
81
81
m. insert ( "github" . to_string ( ) , self . github . to_json ( ) ) ;
82
82
m. insert ( "github_stars" . to_string ( ) , self . github_stars . to_json ( ) ) ;
83
83
m. insert ( "github_forks" . to_string ( ) , self . github_forks . to_json ( ) ) ;
@@ -92,6 +92,23 @@ impl ToJson for CrateDetails {
92
92
}
93
93
94
94
95
+ #[ derive( Debug , Eq , PartialEq ) ]
96
+ struct Release {
97
+ pub version : String ,
98
+ pub build_status : bool ,
99
+ }
100
+
101
+
102
+ impl ToJson for Release {
103
+ fn to_json ( & self ) -> Json {
104
+ let mut m: BTreeMap < String , Json > = BTreeMap :: new ( ) ;
105
+ m. insert ( "version" . to_string ( ) , self . version . to_json ( ) ) ;
106
+ m. insert ( "build_status" . to_string ( ) , self . build_status . to_json ( ) ) ;
107
+ m. to_json ( )
108
+ }
109
+ }
110
+
111
+
95
112
impl CrateDetails {
96
113
pub fn new ( conn : & Connection , name : & str , version : & str ) -> Option < CrateDetails > {
97
114
@@ -135,7 +152,7 @@ impl CrateDetails {
135
152
let release_id: i32 = rows. get ( 0 ) . get ( 1 ) ;
136
153
137
154
// sort versions with semver
138
- let versions = {
155
+ let releases = {
139
156
let mut versions: Vec < semver:: Version > = Vec :: new ( ) ;
140
157
let versions_from_db: Json = rows. get ( 0 ) . get ( 17 ) ;
141
158
@@ -151,7 +168,7 @@ impl CrateDetails {
151
168
152
169
versions. sort ( ) ;
153
170
versions. reverse ( ) ;
154
- versions. iter ( ) . map ( |semver| format ! ( "{}" , semver ) ) . collect ( )
171
+ versions. iter ( ) . map ( |version| map_to_release ( & conn , crate_id , version . to_string ( ) ) ) . collect ( )
155
172
} ;
156
173
157
174
let metadata = MetaData {
@@ -181,7 +198,7 @@ impl CrateDetails {
181
198
keywords : rows. get ( 0 ) . get ( 14 ) ,
182
199
have_examples : rows. get ( 0 ) . get ( 15 ) ,
183
200
target_name : rows. get ( 0 ) . get ( 16 ) ,
184
- versions : versions ,
201
+ releases ,
185
202
github : false ,
186
203
github_stars : rows. get ( 0 ) . get ( 18 ) ,
187
204
github_forks : rows. get ( 0 ) . get ( 19 ) ,
@@ -237,6 +254,31 @@ impl CrateDetails {
237
254
238
255
Some ( crate_details)
239
256
}
257
+
258
+ /// Returns all versions of this crate.
259
+ pub fn versions ( & self ) -> Vec < String > {
260
+ self . releases . iter ( )
261
+ . map ( |release| release. version . clone ( ) )
262
+ . collect ( )
263
+ }
264
+ }
265
+
266
+
267
+ fn map_to_release ( conn : & Connection , crate_id : i32 , version : String ) -> Release {
268
+ let rows = conn. query (
269
+ "SELECT build_status
270
+ FROM releases
271
+ WHERE releases.crate_id = $1 and releases.version = $2;" ,
272
+ & [ & crate_id, & version] ,
273
+ ) . unwrap ( ) ;
274
+
275
+ let build_status = if !rows. is_empty ( ) {
276
+ rows. get ( 0 ) . get ( 0 )
277
+ } else {
278
+ false
279
+ } ;
280
+
281
+ Release { version, build_status }
240
282
}
241
283
242
284
@@ -355,7 +397,13 @@ mod tests {
355
397
356
398
let details = CrateDetails :: new ( & db. conn ( ) , "foo" , "0.0.2" ) . unwrap ( ) ;
357
399
358
- assert_eq ! ( details. versions, vec![ "1.0.0" , "0.0.3" , "0.0.2" , "0.0.1" ] ) ;
400
+ assert_eq ! ( details. versions( ) , vec![ "1.0.0" , "0.0.3" , "0.0.2" , "0.0.1" ] ) ;
401
+ assert_eq ! ( details. releases, vec![
402
+ Release { version: "1.0.0" . to_string( ) , build_status: true } ,
403
+ Release { version: "0.0.3" . to_string( ) , build_status: false } ,
404
+ Release { version: "0.0.2" . to_string( ) , build_status: true } ,
405
+ Release { version: "0.0.1" . to_string( ) , build_status: true } ,
406
+ ] ) ;
359
407
360
408
Ok ( ( ) )
361
409
} ) ;
0 commit comments