1
1
#![ deny( warnings) ]
2
2
3
- #[ macro_use] extern crate diesel;
4
- #[ macro_use] extern crate diesel_codegen;
3
+ extern crate diesel;
5
4
extern crate bufstream;
6
5
extern crate cargo_registry;
7
6
extern crate conduit;
@@ -28,6 +27,7 @@ use cargo_registry::app::App;
28
27
use cargo_registry:: category:: NewCategory ;
29
28
use cargo_registry:: db:: { self , RequestTransaction } ;
30
29
use cargo_registry:: dependency:: Kind ;
30
+ use cargo_registry:: keyword:: Keyword ;
31
31
use cargo_registry:: krate:: NewCrate ;
32
32
use cargo_registry:: upload as u;
33
33
use cargo_registry:: user:: NewUser ;
@@ -214,10 +214,110 @@ fn user(login: &str) -> User {
214
214
}
215
215
}
216
216
217
- fn new_crate ( name : & str ) -> NewCrate {
218
- NewCrate {
219
- name : name,
220
- ..NewCrate :: default ( )
217
+ use cargo_registry:: util:: CargoResult ;
218
+
219
+ struct CrateBuilder < ' a > {
220
+ owner_id : i32 ,
221
+ krate : NewCrate < ' a > ,
222
+ license_file : Option < & ' a str > ,
223
+ downloads : Option < i32 > ,
224
+ versions : Vec < semver:: Version > ,
225
+ keywords : Vec < & ' a str > ,
226
+ }
227
+
228
+ impl < ' a > CrateBuilder < ' a > {
229
+ fn new ( name : & str , owner_id : i32 ) -> CrateBuilder {
230
+ CrateBuilder {
231
+ owner_id : owner_id,
232
+ krate : NewCrate {
233
+ name : name,
234
+ ..NewCrate :: default ( )
235
+ } ,
236
+ license_file : None ,
237
+ downloads : None ,
238
+ versions : Vec :: new ( ) ,
239
+ keywords : Vec :: new ( ) ,
240
+ }
241
+ }
242
+
243
+ fn description ( mut self , description : & ' a str ) -> Self {
244
+ self . krate . description = Some ( description) ;
245
+ self
246
+ }
247
+
248
+ fn documentation ( mut self , documentation : & ' a str ) -> Self {
249
+ self . krate . documentation = Some ( documentation) ;
250
+ self
251
+ }
252
+
253
+ fn homepage ( mut self , homepage : & ' a str ) -> Self {
254
+ self . krate . homepage = Some ( homepage) ;
255
+ self
256
+ }
257
+
258
+ fn readme ( mut self , readme : & ' a str ) -> Self {
259
+ self . krate . readme = Some ( readme) ;
260
+ self
261
+ }
262
+
263
+ fn max_upload_size ( mut self , max_upload_size : i32 ) -> Self {
264
+ self . krate . max_upload_size = Some ( max_upload_size) ;
265
+ self
266
+ }
267
+
268
+ fn downloads ( mut self , downloads : i32 ) -> Self {
269
+ self . downloads = Some ( downloads) ;
270
+ self
271
+ }
272
+
273
+ fn version ( mut self , version : & str ) -> Self {
274
+ let version = semver:: Version :: parse ( version) . unwrap_or_else ( |e| {
275
+ panic ! ( "The version {} is not valid: {}" , version, e) ;
276
+ } ) ;
277
+ self . versions . push ( version) ;
278
+ self
279
+ }
280
+
281
+ fn keyword ( mut self , keyword : & ' a str ) -> Self {
282
+ self . keywords . push ( keyword) ;
283
+ self
284
+ }
285
+
286
+ fn build ( mut self , connection : & PgConnection ) -> CargoResult < Crate > {
287
+ use diesel:: update;
288
+
289
+ let mut krate = self . krate
290
+ . create_or_update ( connection, self . license_file , self . owner_id ) ?;
291
+
292
+ // Since we are using `NewCrate`, we can't set all the
293
+ // crate properties in a single DB call.
294
+ if let Some ( downloads) = self . downloads {
295
+ krate. downloads = downloads;
296
+ update ( & krate) . set ( & krate) . execute ( connection) ?;
297
+ }
298
+
299
+ if self . versions . is_empty ( ) {
300
+ self . versions . push ( "0.99.0" . parse ( ) . expect ( "invalid version number" ) ) ;
301
+ }
302
+
303
+ for version_num in & self . versions {
304
+ NewVersion :: new ( krate. id , version_num, & HashMap :: new ( ) ) ?
305
+ . save ( connection, & [ ] ) ?;
306
+ }
307
+
308
+ if !self . keywords . is_empty ( ) {
309
+ Keyword :: update_crate ( connection, & krate, & self . keywords ) ?;
310
+ }
311
+
312
+ Ok ( krate)
313
+ }
314
+
315
+ fn expect_build ( self , connection : & PgConnection ) -> Crate {
316
+ let name = self . krate . name ;
317
+ self . build ( connection)
318
+ . unwrap_or_else ( |e| {
319
+ panic ! ( "Unable to create crate {}: {:?}" , name, e) ;
320
+ } )
221
321
}
222
322
}
223
323
@@ -324,7 +424,7 @@ fn request_with_user_and_mock_crate(
324
424
let conn = app. diesel_database . get ( ) . unwrap ( ) ;
325
425
let user = user. create_or_update ( & conn) . unwrap ( ) ;
326
426
sign_in_as ( & mut req, & user) ;
327
- :: new_crate ( krate) . create_or_update ( & conn , None , user. id ) . unwrap ( ) ;
427
+ :: CrateBuilder :: new ( krate, user. id ) . expect_build ( & conn ) ;
328
428
}
329
429
req
330
430
}
0 commit comments