@@ -46,8 +46,8 @@ use flate2::Compression;
46
46
use cargo_registry:: { models, schema, views} ;
47
47
use util:: { Bad , RequestHelper , TestApp } ;
48
48
49
- use models:: { Crate , CrateDownload , CrateOwner , Dependency , Keyword , Team , User , Version } ;
50
- use models:: { NewCategory , NewCrate , NewTeam , NewUser , NewVersion } ;
49
+ use models:: { Crate , CrateOwner , Dependency , Team , User , Version } ;
50
+ use models:: { NewCategory , NewTeam , NewUser , NewVersion } ;
51
51
use schema:: * ;
52
52
use views:: krate_publish as u;
53
53
use views:: { EncodableCrate , EncodableKeyword , EncodableVersion } ;
@@ -82,6 +82,7 @@ macro_rules! bad_resp {
82
82
}
83
83
84
84
mod badge;
85
+ mod builders;
85
86
mod categories;
86
87
mod category;
87
88
mod git;
@@ -266,231 +267,6 @@ fn add_team_to_crate(t: &Team, krate: &Crate, u: &User, conn: &PgConnection) ->
266
267
267
268
use cargo_registry:: util:: CargoResult ;
268
269
269
- struct VersionBuilder < ' a > {
270
- num : semver:: Version ,
271
- license : Option < & ' a str > ,
272
- license_file : Option < & ' a str > ,
273
- features : HashMap < String , Vec < String > > ,
274
- dependencies : Vec < ( i32 , Option < & ' static str > ) > ,
275
- yanked : bool ,
276
- }
277
-
278
- impl < ' a > VersionBuilder < ' a > {
279
- fn new ( num : & str ) -> Self {
280
- let num = semver:: Version :: parse ( num) . unwrap_or_else ( |e| {
281
- panic ! ( "The version {} is not valid: {}" , num, e) ;
282
- } ) ;
283
-
284
- VersionBuilder {
285
- num,
286
- license : None ,
287
- license_file : None ,
288
- features : HashMap :: new ( ) ,
289
- dependencies : Vec :: new ( ) ,
290
- yanked : false ,
291
- }
292
- }
293
-
294
- fn license ( mut self , license : Option < & ' a str > ) -> Self {
295
- self . license = license;
296
- self
297
- }
298
-
299
- fn dependency ( mut self , dependency : & Crate , target : Option < & ' static str > ) -> Self {
300
- self . dependencies . push ( ( dependency. id , target) ) ;
301
- self
302
- }
303
-
304
- fn yanked ( self , yanked : bool ) -> Self {
305
- Self { yanked, ..self }
306
- }
307
-
308
- fn build ( self , crate_id : i32 , connection : & PgConnection ) -> CargoResult < Version > {
309
- use diesel:: { insert_into, update} ;
310
-
311
- let license = match self . license {
312
- Some ( license) => Some ( license. to_owned ( ) ) ,
313
- None => None ,
314
- } ;
315
-
316
- let mut vers = NewVersion :: new (
317
- crate_id,
318
- & self . num ,
319
- & self . features ,
320
- license,
321
- self . license_file ,
322
- None ,
323
- ) ?. save ( connection, & [ ] ) ?;
324
-
325
- if self . yanked {
326
- vers = update ( & vers)
327
- . set ( versions:: yanked. eq ( true ) )
328
- . get_result ( connection) ?;
329
- }
330
-
331
- let new_deps = self
332
- . dependencies
333
- . into_iter ( )
334
- . map ( |( crate_id, target) | {
335
- (
336
- dependencies:: version_id. eq ( vers. id ) ,
337
- dependencies:: req. eq ( ">= 0" ) ,
338
- dependencies:: crate_id. eq ( crate_id) ,
339
- dependencies:: target. eq ( target) ,
340
- dependencies:: optional. eq ( false ) ,
341
- dependencies:: default_features. eq ( false ) ,
342
- dependencies:: features. eq ( Vec :: < String > :: new ( ) ) ,
343
- )
344
- } ) . collect :: < Vec < _ > > ( ) ;
345
- insert_into ( dependencies:: table)
346
- . values ( & new_deps)
347
- . execute ( connection) ?;
348
-
349
- Ok ( vers)
350
- }
351
- }
352
-
353
- impl < ' a > From < & ' a str > for VersionBuilder < ' a > {
354
- fn from ( num : & ' a str ) -> Self {
355
- VersionBuilder :: new ( num)
356
- }
357
- }
358
-
359
- struct CrateBuilder < ' a > {
360
- owner_id : i32 ,
361
- krate : NewCrate < ' a > ,
362
- downloads : Option < i32 > ,
363
- recent_downloads : Option < i32 > ,
364
- versions : Vec < VersionBuilder < ' a > > ,
365
- keywords : Vec < & ' a str > ,
366
- }
367
-
368
- impl < ' a > CrateBuilder < ' a > {
369
- fn new ( name : & str , owner_id : i32 ) -> CrateBuilder < ' _ > {
370
- CrateBuilder {
371
- owner_id,
372
- krate : NewCrate {
373
- name,
374
- ..NewCrate :: default ( )
375
- } ,
376
- downloads : None ,
377
- recent_downloads : None ,
378
- versions : Vec :: new ( ) ,
379
- keywords : Vec :: new ( ) ,
380
- }
381
- }
382
-
383
- fn description ( mut self , description : & ' a str ) -> Self {
384
- self . krate . description = Some ( description) ;
385
- self
386
- }
387
-
388
- fn documentation ( mut self , documentation : & ' a str ) -> Self {
389
- self . krate . documentation = Some ( documentation) ;
390
- self
391
- }
392
-
393
- fn homepage ( mut self , homepage : & ' a str ) -> Self {
394
- self . krate . homepage = Some ( homepage) ;
395
- self
396
- }
397
-
398
- fn readme ( mut self , readme : & ' a str ) -> Self {
399
- self . krate . readme = Some ( readme) ;
400
- self
401
- }
402
-
403
- fn max_upload_size ( mut self , max_upload_size : i32 ) -> Self {
404
- self . krate . max_upload_size = Some ( max_upload_size) ;
405
- self
406
- }
407
-
408
- fn downloads ( mut self , downloads : i32 ) -> Self {
409
- self . downloads = Some ( downloads) ;
410
- self
411
- }
412
-
413
- fn recent_downloads ( mut self , recent_downloads : i32 ) -> Self {
414
- self . recent_downloads = Some ( recent_downloads) ;
415
- self
416
- }
417
-
418
- fn version < T : Into < VersionBuilder < ' a > > > ( mut self , version : T ) -> Self {
419
- self . versions . push ( version. into ( ) ) ;
420
- self
421
- }
422
-
423
- fn keyword ( mut self , keyword : & ' a str ) -> Self {
424
- self . keywords . push ( keyword) ;
425
- self
426
- }
427
-
428
- fn build ( mut self , connection : & PgConnection ) -> CargoResult < Crate > {
429
- use diesel:: { insert_into, select, update} ;
430
-
431
- let mut krate = self
432
- . krate
433
- . create_or_update ( connection, None , self . owner_id ) ?;
434
-
435
- // Since we are using `NewCrate`, we can't set all the
436
- // crate properties in a single DB call.
437
-
438
- let old_downloads = self . downloads . unwrap_or ( 0 ) - self . recent_downloads . unwrap_or ( 0 ) ;
439
- let now = Utc :: now ( ) ;
440
- let old_date = now. naive_utc ( ) . date ( ) - chrono:: Duration :: days ( 91 ) ;
441
-
442
- if let Some ( downloads) = self . downloads {
443
- let crate_download = CrateDownload {
444
- crate_id : krate. id ,
445
- downloads : old_downloads,
446
- date : old_date,
447
- } ;
448
-
449
- insert_into ( crate_downloads:: table)
450
- . values ( & crate_download)
451
- . execute ( connection) ?;
452
- krate. downloads = downloads;
453
- update ( & krate) . set ( & krate) . execute ( connection) ?;
454
- }
455
-
456
- if self . recent_downloads . is_some ( ) {
457
- let crate_download = CrateDownload {
458
- crate_id : krate. id ,
459
- downloads : self . recent_downloads . unwrap ( ) ,
460
- date : now. naive_utc ( ) . date ( ) ,
461
- } ;
462
-
463
- insert_into ( crate_downloads:: table)
464
- . values ( & crate_download)
465
- . execute ( connection) ?;
466
-
467
- no_arg_sql_function ! ( refresh_recent_crate_downloads, ( ) ) ;
468
- select ( refresh_recent_crate_downloads) . execute ( connection) ?;
469
- }
470
-
471
- if self . versions . is_empty ( ) {
472
- self . versions . push ( VersionBuilder :: new ( "0.99.0" ) ) ;
473
- }
474
-
475
- for version_builder in self . versions {
476
- version_builder. build ( krate. id , connection) ?;
477
- }
478
-
479
- if !self . keywords . is_empty ( ) {
480
- Keyword :: update_crate ( connection, & krate, & self . keywords ) ?;
481
- }
482
-
483
- Ok ( krate)
484
- }
485
-
486
- fn expect_build ( self , connection : & PgConnection ) -> Crate {
487
- let name = self . krate . name ;
488
- self . build ( connection) . unwrap_or_else ( |e| {
489
- panic ! ( "Unable to create crate {}: {:?}" , name, e) ;
490
- } )
491
- }
492
- }
493
-
494
270
fn new_version ( crate_id : i32 , num : & str , crate_size : Option < i32 > ) -> NewVersion {
495
271
let num = semver:: Version :: parse ( num) . unwrap ( ) ;
496
272
NewVersion :: new ( crate_id, & num, & HashMap :: new ( ) , None , None , crate_size) . unwrap ( )
@@ -745,95 +521,3 @@ fn new_crate_to_body_with_tarball(new_crate: &u::NewCrate, tarball: &[u8]) -> Ve
745
521
body. extend ( tarball) ;
746
522
body
747
523
}
748
-
749
- lazy_static ! {
750
- static ref EMPTY_TARBALL_BYTES : Vec <u8 > = {
751
- let mut empty_tarball = vec![ ] ;
752
- {
753
- let mut ar =
754
- tar:: Builder :: new( GzEncoder :: new( & mut empty_tarball, Compression :: default ( ) ) ) ;
755
- t!( ar. finish( ) ) ;
756
- }
757
- empty_tarball
758
- } ;
759
- }
760
-
761
- /// A builder for constructing a crate for the purposes of testing publishing. If you only need
762
- /// a crate to exist and don't need to test behavior caused by the publish request, inserting
763
- /// a crate into the database directly by using CrateBuilder will be faster.
764
- pub struct PublishBuilder {
765
- pub krate_name : String ,
766
- version : semver:: Version ,
767
- tarball : Vec < u8 > ,
768
- }
769
-
770
- impl PublishBuilder {
771
- /// Create a request to publish a crate with the given name, version 1.0.0, and no files
772
- /// in its tarball.
773
- fn new ( krate_name : & str ) -> Self {
774
- PublishBuilder {
775
- krate_name : krate_name. into ( ) ,
776
- version : semver:: Version :: parse ( "1.0.0" ) . unwrap ( ) ,
777
- tarball : EMPTY_TARBALL_BYTES . to_vec ( ) ,
778
- }
779
- }
780
-
781
- /// Set the version of the crate being published to something other than the default of 1.0.0.
782
- fn version ( mut self , version : & str ) -> Self {
783
- self . version = semver:: Version :: parse ( version) . unwrap ( ) ;
784
- self
785
- }
786
-
787
- /// Set the files in the crate's tarball.
788
- fn files ( mut self , files : & [ ( & str , & [ u8 ] ) ] ) -> Self {
789
- let mut slices = files. iter ( ) . map ( |p| p. 1 ) . collect :: < Vec < _ > > ( ) ;
790
- let files = files
791
- . iter ( )
792
- . zip ( & mut slices)
793
- . map ( |( & ( name, _) , data) | {
794
- let len = data. len ( ) as u64 ;
795
- ( name, data as & mut Read , len)
796
- } ) . collect :: < Vec < _ > > ( ) ;
797
-
798
- let mut tarball = Vec :: new ( ) ;
799
- {
800
- let mut ar = tar:: Builder :: new ( GzEncoder :: new ( & mut tarball, Compression :: default ( ) ) ) ;
801
- for ( name, ref mut data, size) in files {
802
- let mut header = tar:: Header :: new_gnu ( ) ;
803
- t ! ( header. set_path( name) ) ;
804
- header. set_size ( size) ;
805
- header. set_cksum ( ) ;
806
- t ! ( ar. append( & header, data) ) ;
807
- }
808
- t ! ( ar. finish( ) ) ;
809
- }
810
-
811
- self . tarball = tarball;
812
- self
813
- }
814
-
815
- /// Consume this builder to make the Put request body
816
- fn body ( self ) -> Vec < u8 > {
817
- let new_crate = u:: NewCrate {
818
- name : u:: CrateName ( self . krate_name . clone ( ) ) ,
819
- vers : u:: CrateVersion ( self . version ) ,
820
- features : HashMap :: new ( ) ,
821
- deps : Vec :: new ( ) ,
822
- authors : vec ! [ "foo" . to_string( ) ] ,
823
- description : Some ( "description" . to_string ( ) ) ,
824
- homepage : None ,
825
- documentation : None ,
826
- readme : None ,
827
- readme_file : None ,
828
- keywords : Some ( u:: KeywordList ( Vec :: new ( ) ) ) ,
829
- categories : Some ( u:: CategoryList ( Vec :: new ( ) ) ) ,
830
- license : Some ( "MIT" . to_string ( ) ) ,
831
- license_file : None ,
832
- repository : None ,
833
- badges : Some ( HashMap :: new ( ) ) ,
834
- links : None ,
835
- } ;
836
-
837
- :: new_crate_to_body_with_tarball ( & new_crate, & self . tarball )
838
- }
839
- }
0 commit comments