File tree Expand file tree Collapse file tree 1 file changed +12
-9
lines changed
Expand file tree Collapse file tree 1 file changed +12
-9
lines changed Original file line number Diff line number Diff line change 11mod scopes;
22
3+ use bon:: Builder ;
34use chrono:: NaiveDateTime ;
45use diesel:: dsl:: now;
56use diesel:: prelude:: * ;
@@ -12,11 +13,13 @@ use crate::schema::api_tokens;
1213use crate :: util:: rfc3339;
1314use crate :: util:: token:: { HashedToken , PlainToken } ;
1415
15- #[ derive( Debug , Insertable ) ]
16+ #[ derive( Debug , Insertable , Builder ) ]
1617#[ diesel( table_name = api_tokens, check_for_backend( diesel:: pg:: Pg ) ) ]
1718pub struct NewApiToken {
1819 pub user_id : i32 ,
20+ #[ builder( into) ]
1921 pub name : String ,
22+ #[ builder( default = PlainToken :: generate( ) . hashed( ) ) ]
2023 pub token : HashedToken ,
2124 /// `None` or a list of crate scope patterns (see RFC #2947)
2225 pub crate_scopes : Option < Vec < CrateScope > > ,
@@ -77,14 +80,14 @@ impl ApiToken {
7780 ) -> QueryResult < CreatedApiToken > {
7881 let token = PlainToken :: generate ( ) ;
7982
80- let new_token = NewApiToken {
81- user_id,
82- name : name . to_string ( ) ,
83- token : token. hashed ( ) ,
84- crate_scopes,
85- endpoint_scopes,
86- expired_at,
87- } ;
83+ let new_token = NewApiToken :: builder ( )
84+ . user_id ( user_id )
85+ . name ( name )
86+ . token ( token. hashed ( ) )
87+ . maybe_crate_scopes ( crate_scopes)
88+ . maybe_endpoint_scopes ( endpoint_scopes)
89+ . maybe_expired_at ( expired_at)
90+ . build ( ) ;
8891
8992 Ok ( CreatedApiToken {
9093 plaintext : token,
You can’t perform that action at this time.
0 commit comments