1+ use bon:: Builder ;
12use diesel:: pg:: Pg ;
23use diesel:: prelude:: * ;
34
5+ use self :: crate_owner_builder:: { SetOwnerId , SetOwnerKind } ;
46use crate :: models:: { Crate , CrateOwnerInvitation , Team , User } ;
57use crate :: schema:: crate_owners;
68use crates_io_diesel_helpers:: pg_enum;
79
8- #[ derive( Insertable , Associations , Identifiable , Debug , Clone , Copy ) ]
10+ #[ derive( Insertable , Associations , Identifiable , Debug , Clone , Copy , Builder ) ]
911#[ diesel(
1012 table_name = crate_owners,
1113 check_for_backend( diesel:: pg:: Pg ) ,
@@ -16,12 +18,33 @@ use crates_io_diesel_helpers::pg_enum;
1618) ]
1719pub struct CrateOwner {
1820 pub crate_id : i32 ,
21+ #[ builder( setters( vis = "pub(self)" ) ) ]
1922 pub owner_id : i32 ,
2023 pub created_by : i32 ,
24+ #[ builder( setters( vis = "pub(self)" ) ) ]
2125 pub owner_kind : OwnerKind ,
26+ #[ builder( default = true ) ]
2227 pub email_notifications : bool ,
2328}
2429
30+ impl < S : crate_owner_builder:: State > CrateOwnerBuilder < S > {
31+ pub fn team_id ( self , team_id : i32 ) -> CrateOwnerBuilder < SetOwnerId < SetOwnerKind < S > > >
32+ where
33+ S :: OwnerId : crate_owner_builder:: IsUnset ,
34+ S :: OwnerKind : crate_owner_builder:: IsUnset ,
35+ {
36+ self . owner_kind ( OwnerKind :: Team ) . owner_id ( team_id)
37+ }
38+
39+ pub fn user_id ( self , user_id : i32 ) -> CrateOwnerBuilder < SetOwnerId < SetOwnerKind < S > > >
40+ where
41+ S :: OwnerId : crate_owner_builder:: IsUnset ,
42+ S :: OwnerKind : crate_owner_builder:: IsUnset ,
43+ {
44+ self . owner_kind ( OwnerKind :: User ) . owner_id ( user_id)
45+ }
46+ }
47+
2548type BoxedQuery < ' a > = crate_owners:: BoxedQuery < ' a , Pg , crate_owners:: SqlType > ;
2649
2750impl CrateOwner {
@@ -35,13 +58,11 @@ impl CrateOwner {
3558 }
3659
3760 pub fn from_invite ( invite : & CrateOwnerInvitation ) -> Self {
38- Self {
39- crate_id : invite. crate_id ,
40- owner_id : invite. invited_user_id ,
41- created_by : invite. invited_by_user_id ,
42- owner_kind : OwnerKind :: User ,
43- email_notifications : true ,
44- }
61+ CrateOwner :: builder ( )
62+ . crate_id ( invite. crate_id )
63+ . user_id ( invite. invited_user_id )
64+ . created_by ( invite. invited_by_user_id )
65+ . build ( )
4566 }
4667}
4768
0 commit comments