@@ -20,6 +20,7 @@ pub use self::middleware::{Middleware, RequestUser};
20
20
21
21
pub mod middleware;
22
22
23
+ /// The model representing a row in the `users` database table.
23
24
#[ derive( Clone , Debug , PartialEq , Eq ) ]
24
25
pub struct User {
25
26
pub id : i32 ,
@@ -31,6 +32,7 @@ pub struct User {
31
32
pub api_token : String ,
32
33
}
33
34
35
+ /// The serialization format for the `User` model.
34
36
#[ derive( RustcDecodable , RustcEncodable ) ]
35
37
pub struct EncodableUser {
36
38
pub id : i32 ,
@@ -41,6 +43,7 @@ pub struct EncodableUser {
41
43
}
42
44
43
45
impl User {
46
+ /// Queries the database for a user with a certain `gh_login` value.
44
47
pub fn find_by_login ( conn : & GenericConnection ,
45
48
login : & str ) -> CargoResult < User > {
46
49
let stmt = try!( conn. prepare ( "SELECT * FROM users
@@ -52,6 +55,7 @@ impl User {
52
55
Ok ( Model :: from_row ( & row) )
53
56
}
54
57
58
+ /// Queries the database for a user with a certain `api_token` value.
55
59
pub fn find_by_api_token ( conn : & GenericConnection ,
56
60
token : & str ) -> CargoResult < User > {
57
61
let stmt = try!( conn. prepare ( "SELECT * FROM users \
@@ -62,6 +66,7 @@ impl User {
62
66
} )
63
67
}
64
68
69
+ /// Updates a user or inserts a new user into the database.
65
70
pub fn find_or_insert ( conn : & GenericConnection ,
66
71
login : & str ,
67
72
email : Option < & str > ,
@@ -102,10 +107,12 @@ impl User {
102
107
} ) ) ) )
103
108
}
104
109
110
+ /// Generates a new crates.io API token.
105
111
pub fn new_api_token ( ) -> String {
106
112
thread_rng ( ) . gen_ascii_chars ( ) . take ( 32 ) . collect ( )
107
113
}
108
114
115
+ /// Converts this `User` model into an `EncodableUser` for JSON serialization.
109
116
pub fn encodable ( self ) -> EncodableUser {
110
117
let User { id, email, api_token : _, gh_access_token : _,
111
118
name, gh_login, avatar } = self ;
0 commit comments